Skip to content
Snippets Groups Projects
Commit f7e65c38 authored by Odin Johan Vatne's avatar Odin Johan Vatne
Browse files

Add profile pages

parent 76a7499d
No related branches found
No related tags found
No related merge requests found
{% extends 'pasapp/templates/base_template.html' %}
{% block title %}{{profileUser.first_name}} {{profileUser.last_name}}{% endblock %}
{% block imports %}
{% load static %}
<script src={% static 'pasapp/js/tag_edit.js' %}></script>
<script>
{% for userTag in userTags %}
tagSet.add({{userTag.id}});
{% endfor %}
</script>
{% endblock %}
{% block onload %} {% endblock %}
{% block content %}
{% load custom_filters %}
{% load html_components %}
<h2>{{profileUser.first_name}} {{profileUser.last_name}}</h2>
{% if profileUser|is_professor %}
<div class='professor-label'>Professor</div>
{% else %}
<div class='student-label'>Student</div>
{% endif %}
<p>Email: <a href="mailto:{{profileUser.email}}">{{profileUser.email}}</a></p>
{% if profileUser|is_student %}
{% tagbox_categorized userTags|academic id='tagBox-fixed' %}
<br>
{% endif %}
{% if user.id == profileUser.id %}
<form action="/user/{{profileUser.username}}/" method="post">
{% csrf_token %}
<input type="hidden" name="tags" id="id_tags" value="">
{% if user|is_student %}
{% tags_dropdown tags|academic:False %}
<br>
{% tagbox_categorized userTags|academic:False True %}
{% else %}
{% tags_dropdown tags %}
<br>
{% tagbox_categorized userTags True %}
{% endif %}
<br>
<input type="submit" value="Save changes"/>
</form>
{% elif profileUser|is_student %}
{% tagbox_categorized userTags|academic:False %}
{% else %}
{% tagbox_categorized userTags %}
{% endif %}
{% endblock %}
\ No newline at end of file
...@@ -36,9 +36,10 @@ ...@@ -36,9 +36,10 @@
<h1 id="logo">PAS</h1> <h1 id="logo">PAS</h1>
<div> <div>
<a href="/" class="undecorated-link nav-item">Projects</a> <a href="/" class="undecorated-link nav-item">Projects</a>
<a href="/applications/" class="undecorated-link nav-item"> {% if user.username %}
Applications <a href="/applications/" class="undecorated-link nav-item">Applications</a>
</a> <a href="/user/{{user.username}}" class="undecorated-link nav-item">Profile</a>
{% endif %}
</div> </div>
{% if user.username %} {% if user.username %}
<div id="user-status"> <div id="user-status">
......
from django import template from django import template
from pasapp.models import Application
register = template.Library() register = template.Library()
...@@ -14,6 +12,10 @@ def is_professor(user): ...@@ -14,6 +12,10 @@ def is_professor(user):
def is_student(user): def is_student(user):
return user.groups.filter(name='student').exists() return user.groups.filter(name='student').exists()
@register.filter(name='academic')
def filter_academic(tags, academic=True):
return [tag for tag in tags if tag.category.academic == academic]
@register.filter(name='student_status') @register.filter(name='student_status')
def student_status(application): def student_status(application):
return application.student_status_label() return application.student_status_label()
......
...@@ -17,6 +17,10 @@ def tagbubble(tag, onclick=False): ...@@ -17,6 +17,10 @@ def tagbubble(tag, onclick=False):
def tagbox_static(tag_pairs): def tagbox_static(tag_pairs):
return { 'tagPairs': tag_pairs } return { 'tagPairs': tag_pairs }
@register.inclusion_tag('pasapp/components/tagbox_categorized.html')
def tagbox_categorized(tags = [], onclick=False, id="tagBox"):
tags_by_category = tagsByCategory(tags)
return { 'tagsByCategory': tags_by_category, 'onclick': onclick, 'id': id }
@register.inclusion_tag('pasapp/components/tagfilters.html') @register.inclusion_tag('pasapp/components/tagfilters.html')
def tagfilters(all_tags, selected_tags): def tagfilters(all_tags, selected_tags):
......
...@@ -25,6 +25,7 @@ urlpatterns = [ ...@@ -25,6 +25,7 @@ urlpatterns = [
views.update_student_status, name='update_student_status'), views.update_student_status, name='update_student_status'),
path('applications/update_professor_status/', path('applications/update_professor_status/',
views.update_professor_status, name='update_professor_status'), views.update_professor_status, name='update_professor_status'),
path('applications/add_comment', views.add_comment, name='add_comment'), path('applications/add_comment/', views.add_comment, name='add_comment'),
path('user/<str:username>/', views.profile_page, name='profile_page'),
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/pasapp/favicon.ico')), url(r'^favicon\.ico$', RedirectView.as_view(url='/static/pasapp/favicon.ico')),
] ]
from sys import stdout from sys import stdout
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout, get_user_model
from django.contrib.auth.decorators import login_required, user_passes_test from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.http.response import HttpResponseBadRequest, HttpResponseForbidden from django.http.response import HttpResponseBadRequest, HttpResponseForbidden
...@@ -8,7 +8,7 @@ from django.http import HttpResponse ...@@ -8,7 +8,7 @@ from django.http import HttpResponse
from django.db.models import F from django.db.models import F
from pasapp.forms import ApplicationForm, NewProjectForm, NewUserForm,CommentForm from pasapp.forms import ApplicationForm, NewProjectForm, NewUserForm,CommentForm
from pasapp.models import Application, Comment, Project, ProjectTag, Tag from pasapp.models import Application, Comment, Project, ProjectTag, UserTag, Tag
from pasapp.utils import contextWithHeader from pasapp.utils import contextWithHeader
from pasapp.templatetags.custom_filters import is_professor, is_student from pasapp.templatetags.custom_filters import is_professor, is_student
from pasapp.queries import * from pasapp.queries import *
...@@ -389,6 +389,37 @@ def add_comment(request): ...@@ -389,6 +389,37 @@ def add_comment(request):
else: else:
return HttpResponseBadRequest("This endpoint only supports POST requests.") return HttpResponseBadRequest("This endpoint only supports POST requests.")
def profile_page(request, username):
'''
Displays personal tags of a user.
URL: /user/<str:username>/
'''
User = get_user_model()
profile_user = User.objects.get(username=username)
user_tags = UserTag.objects.filter(user=profile_user)
if request.method == 'POST':
if profile_user != request.user:
return HttpResponseForbidden("You may not edit the tags of another user.")
tags = request.POST['tags']
tag_list = tags.split()
user_tag_id_list = [user_tag.tag.id for user_tag in user_tags]
for tag_id_str in tag_list:
tag_id = int(tag_id_str)
if tag_id in user_tag_id_list:
continue
tag = Tag.objects.get(pk=tag_id)
if tag is None:
raise ValueError("Unknown tag found")
user_tag = UserTag(user=request.user, tag=tag)
user_tag.save()
user_tags.exclude(tag__in=[int(tag) for tag in tag_list]).delete()
return redirect(f'/user/{request.user}/')
else:
tags = Tag.objects.all() # TODO: limit by what tags are valid
user_tags = [pair.tag for pair in user_tags]
context = {'profileUser': profile_user, 'userTags': user_tags, 'tags': tags}
return render(request, 'pasapp/pages/profile.html', contextWithHeader(context, request))
def login_view(request): def login_view(request):
''' '''
Form page for logging a user in. Mostly used as a redirect if the user attempts to access Form page for logging a user in. Mostly used as a redirect if the user attempts to access
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment