Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribe user from shifts on user account deletion #516

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 91 additions & 45 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# coding: utf-8
import random
import string

from django.contrib.admin.models import DELETION, LogEntry
from django.contrib.auth import logout
from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.views.generic.edit import UpdateView
from django.urls import reverse_lazy
from django.contrib.auth import models

from datetime import date, timedelta
from datetime import date, datetime, timedelta

from volunteer_planner.utils import LoginRequiredMixin
from scheduler.models import ShiftHelper
Expand All @@ -22,10 +24,11 @@ def user_account_detail(request):
Just shows the user profile.

:param request: http request
:return: return value can be used in urls.py: url(r'^', user_account_detail, name="account_detail")
:return: return value can be used in urls.py:
url(r'^', user_account_detail, name="account_detail")
"""
user = request.user
return render(request, 'user_detail.html', {'user': user})
return render(request, "user_detail.html", {"user": user})


def random_string(length=30):
Expand All @@ -34,48 +37,77 @@ def random_string(length=30):

:param length: (optional, default is 30) length of the string to be created
"""
return u''.join(random.choice(string.ascii_letters) for x in range(length))
return "".join(random.choice(string.ascii_letters) for x in range(length))


@transaction.atomic
def unsub_user_from_future_shifts(user):
subscribed_shifts = ShiftHelper.objects.filter(
user_account=user, shift__starting_time__gt=datetime.now()
)
for sh in subscribed_shifts:
LogEntry.objects.log_action(
user_id=user.id,
content_type_id=ContentType.objects.get_for_model(ShiftHelper).id,
object_id=sh.id,
object_repr=f"User '{user}' @ shift '{sh.shift}'",
action_flag=DELETION,
change_message=f"Initially joined: {sh.joined_shift_at.isoformat()}",
)
sh.delete()


@login_required()
@transaction.atomic
def account_delete_final(request):
"""
This randomizes/anonymises the user profile. The account is set inactive.
(regarding to django documentation setting inactive is preferred to deleting an account.)
(regarding to django documentation setting inactive is preferred to deleting an
account.)

:param request: http request
:return http response of user_detail_deleted-template that confirms deletion.
"""
user = models.User.objects.get_by_natural_key(request.user.username)

unsub_user_from_future_shifts(user.account)

user.username = random_string()
user.first_name = "Deleted"
user.last_name = "User"
user.email = random_string(24)+"@yy.yy"
user.email = random_string(24) + "@yy.yy"
user.password = random_string(20)
user.is_active = False
user.is_staff = False
user.is_superuser = False
user.user_permissions.clear()
user.groups.clear()
user.save()

logout(request)
return render(request, 'user_detail_deleted.html')
return render(request, "user_detail_deleted.html")


class AccountUpdateView(LoginRequiredMixin, UpdateView):
"""
Allows a user to update his/her profile.
"""
fields = ['first_name', 'last_name', 'username']

fields = ["first_name", "last_name", "username"]
template_name = "user_account_edit.html"
success_url = reverse_lazy('account_detail')
success_url = reverse_lazy("account_detail")

def get_object(self, queryset=None):
return self.request.user


class AccountDeleteView(LoginRequiredMixin, UpdateView):
"""
Allows a user to confirm he/she wants to delete the profile. This offers the last warning.
Allows a user to confirm he/she wants to delete the profile. This offers the last
warning.
"""
fields = ['first_name', 'last_name', 'username']

fields = ["first_name", "last_name", "username"]
template_name = "user_account_delete.html"

def get_object(self, queryset=None):
Expand All @@ -93,47 +125,61 @@ def shift_list_active(request):
shifts_further_future.
"""
user = request.user
shifthelper = ShiftHelper.objects.filter(user_account=UserAccount.objects.get(user=user))
shifts_today = shifthelper \
.filter(shift__starting_time__day=date.today().day,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year) \
.order_by("shift__starting_time")
shifts_tomorrow = shifthelper \
.filter(shift__starting_time__day=date.today().day + 1,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year) \
.order_by("shift__starting_time")
shifts_day_after_tomorrow = shifthelper \
.filter(shift__starting_time__day=date.today().day + 2,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year) \
.order_by("shift__starting_time")
shifts_further_future = shifthelper \
.filter(shift__starting_time__gt=date.today() + timedelta(days=3)) \
.order_by("shift__starting_time")

return render(request, 'shift_list.html', {'user': user,
'shifts_today': shifts_today,
'shifts_tomorrow': shifts_tomorrow,
'shifts_day_after_tomorrow': shifts_day_after_tomorrow,
'shifts_further_future': shifts_further_future})
shifthelper = ShiftHelper.objects.filter(
user_account=UserAccount.objects.get(user=user)
)
shifts_today = shifthelper.filter(
shift__starting_time__day=date.today().day,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year,
).order_by("shift__starting_time")
shifts_tomorrow = shifthelper.filter(
shift__starting_time__day=date.today().day + 1,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year,
).order_by("shift__starting_time")
shifts_day_after_tomorrow = shifthelper.filter(
shift__starting_time__day=date.today().day + 2,
shift__starting_time__month=date.today().month,
shift__starting_time__year=date.today().year,
).order_by("shift__starting_time")
shifts_further_future = shifthelper.filter(
shift__starting_time__gt=date.today() + timedelta(days=3)
).order_by("shift__starting_time")

return render(
request,
"shift_list.html",
{
"user": user,
"shifts_today": shifts_today,
"shifts_tomorrow": shifts_tomorrow,
"shifts_day_after_tomorrow": shifts_day_after_tomorrow,
"shifts_further_future": shifts_further_future,
},
)


@login_required()
def shift_list_done(request):
"""
Delivers the list of shifts, a user has signed up in the past (starting from yesterday).
Delivers the list of shifts, a user has signed up in the past (starting from
yesterday).

:param request: http request
:return: http response of rendered shift_list_done-template and user-date,
ie.: user and shifts_past.
ie.: user and shifts_past.
"""
user = request.user
shifthelper = ShiftHelper.objects.filter(user_account=UserAccount.objects.get(user=user))
shifts_past = shifthelper.filter(shift__ending_time__lt=date.today()).order_by("shift__starting_time").reverse()

return render(request, 'shift_list_done.html', {'user': user,
'shifts_past': shifts_past})

shifthelper = ShiftHelper.objects.filter(
user_account=UserAccount.objects.get(user=user)
)
shifts_past = (
shifthelper.filter(shift__ending_time__lt=date.today())
.order_by("shift__starting_time")
.reverse()
)

return render(
request, "shift_list_done.html", {"user": user, "shifts_past": shifts_past}
)