From 08e20e187516dfe562307c6bca1a2c3813a7189a Mon Sep 17 00:00:00 2001 From: moanos Date: Mon, 18 Nov 2024 23:01:27 +0100 Subject: [PATCH] feat: Add basic user data export --- .../fellchensammlung/details/detail-user.html | 2 +- src/fellchensammlung/urls.py | 1 + src/fellchensammlung/views.py | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html b/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html index 40a05b4..418cd79 100644 --- a/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html +++ b/src/fellchensammlung/templates/fellchensammlung/details/detail-user.html @@ -37,7 +37,7 @@

{% trans 'Profil verwalten' %}

{% translate "Change password" %} - {% translate "Daten exportieren" %} + {% translate "Daten exportieren" %}

diff --git a/src/fellchensammlung/urls.py b/src/fellchensammlung/urls.py index 88806b7..4e33ed1 100644 --- a/src/fellchensammlung/urls.py +++ b/src/fellchensammlung/urls.py @@ -54,6 +54,7 @@ urlpatterns = [ # ex: user/1 path("user//", views.user_detail, name="user-detail"), path("user/me/", views.my_profile, name="user-me"), + path('user/me/export/', views.export_own_profile, name='user-me-export'), path('accounts/register/', RegistrationView.as_view( diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index c596469..88d5309 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -1,12 +1,14 @@ import logging -from django.http import HttpResponseRedirect, JsonResponse +from django.http import HttpResponseRedirect, JsonResponse, HttpResponse from django.shortcuts import render, redirect from django.urls import reverse from django.contrib.auth.decorators import login_required from django.utils import translation from django.core.exceptions import PermissionDenied from django.contrib.auth.decorators import user_passes_test +from django.core.serializers import serialize +import json from .mail import mail_admins_new_report from notfellchen import settings @@ -569,3 +571,15 @@ def external_site_warning(request): def detail_view_rescue_organization(request, rescue_organization_id): org = RescueOrganization.objects.get(pk=rescue_organization_id) return render(request, 'fellchensammlung/details/detail-rescue-organization.html', context={"org": org}) + + +def export_own_profile(request): + user = request.user + ANs = AdoptionNotice.objects.filter(owner=user) + user_as_json = serialize('json', [user]) + user_editable = json.loads(user_as_json) + user_editable[0]["fields"]["password"] = "Password hash redacted for security reasons" + user_as_json = json.dumps(user_editable) + ANs_as_json = serialize('json', ANs) + full_json = f"{user_as_json}, {ANs_as_json}" + return HttpResponse(full_json, content_type="application/json")