diff --git a/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html b/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html index fbd54d3..2bab34c 100644 --- a/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html +++ b/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html @@ -13,8 +13,23 @@

{% translate "Keine bevorzugte Sprache gesetzt." %}

{% endif %} - {% if user.id is request.user.id %} + {% if token %} +
+ {% csrf_token %} +

{% translate "API token:" %} {{ token }}

+ +
+ {% else %} +

{% translate "No token set." %}

+
+ {% csrf_token %} + +
+ {% endif %} + {% if user.id is request.user.id %} +

{% translate "Change password here." %}

+

{% translate "Export your data." %}

{% translate 'Benachrichtigungen' %}

{% include "fellchensammlung/lists/list-notifications.html" %}

{% translate 'Meine Vermittlungen' %}

diff --git a/src/fellchensammlung/templates/fellchensammlung/header.html b/src/fellchensammlung/templates/fellchensammlung/header.html index 8ab5370..b13bf09 100644 --- a/src/fellchensammlung/templates/fellchensammlung/header.html +++ b/src/fellchensammlung/templates/fellchensammlung/header.html @@ -26,7 +26,7 @@ - +
{% csrf_token %} diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 4a04614..88806b7 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -53,6 +53,7 @@ urlpatterns = [ ########### # ex: user/1 path("user//", views.user_detail, name="user-detail"), + path("user/me/", views.my_profile, name="user-me"), path('accounts/register/', RegistrationView.as_view( diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 91b5fa8..c596469 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -23,6 +23,7 @@ from .tools.geo import GeoAPI from .tools.metrics import gather_metrics_data from .tools.admin import clean_locations, get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices from .tasks import add_adoption_notice_location +from rest_framework.authtoken.models import Token def user_is_trust_level_or_above(user, trust_level=User.MODERATOR): @@ -69,7 +70,9 @@ def change_language(request): translation.activate(language_code) response = HttpResponseRedirect(redirect_path) response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language_code) - return response + return response + else: + return render(request, 'fellchensammlung/index.html') def adoption_notice_detail(request, adoption_notice_id): @@ -415,12 +418,31 @@ def report_detail_success(request, report_id): return report_detail(request, report_id, form_complete=True) +def user_detail(request, user, token=None): + context = {"user": user, + "adoption_notices": AdoptionNotice.objects.filter(owner=user), + "notifications": CommentNotification.objects.filter(user=user, read=False)} + if token is not None: + context["token"] = token + return render(request, 'fellchensammlung/details/detail-user.html', context=context) + + @login_required -def user_detail(request, user_id): +def user_by_id(request, user_id): user = User.objects.get(id=user_id) # Only users that are mods or owners of the user are allowed to view fail_if_user_not_owner_or_trust_level(request.user, user) - if request.method == "POST": + return user_detail(request, user) + + +@login_required() +def my_profile(request): + if request.method == 'POST': + if "create_token" in request.POST: + Token.objects.create(user=request.user) + elif "delete_token" in request.POST: + Token.objects.get(user=request.user).delete() + action = request.POST.get("action") if action == "notification_mark_read": notification_id = request.POST.get("notification_id") @@ -432,11 +454,11 @@ def user_detail(request, user_id): for notification in notifications: notification.read = True notification.save() - - context = {"user": user, - "adoption_notices": AdoptionNotice.objects.filter(owner=user), - "notifications": CommentNotification.objects.filter(user=user, read=False)} - return render(request, 'fellchensammlung/details/detail-user.html', context=context) + try: + token = Token.objects.get(user=request.user) + except Token.DoesNotExist: + token = None + return user_detail(request, request.user, token) @user_passes_test(user_is_trust_level_or_above) diff --git a/src/notfellchen/settings.py b/src/notfellchen/settings.py index 1940161..bc0ce77 100644 --- a/src/notfellchen/settings.py +++ b/src/notfellchen/settings.py @@ -169,6 +169,7 @@ INSTALLED_APPS = [ 'crispy_forms', "crispy_bootstrap4", "rest_framework", + 'rest_framework.authtoken' ] MIDDLEWARE = [