forked from Tima-Is-My-Association/TIMA
/
profile.py
89 lines (77 loc) · 3.9 KB
/
profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from app.forms import NewsletterForm, UserChangeForm
from app.functions.piwik import track
from app.models import AssociationHistory, Newsletter, Profile
from association.models import Language, Word
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext as _, ungettext
from django.views.decorators.csrf import csrf_protect
@login_required(login_url='/signin/')
def profile(request):
profile = get_object_or_404(Profile, user=request.user)
track(request, 'Profile | TIMA')
return render(request, 'tima/app/profile/profile.html', locals())
@login_required(login_url='/signin/')
def association_history(request):
word = request.GET.get('word')
l = request.GET.get('l')
association_histories_list = AssociationHistory.objects.filter(user=request.user).order_by('-updated_at')
if word:
word = int(word)
association_histories_list = association_histories_list.filter(Q(association__word__id=word) | Q(association__association__id=word))
if l:
association_histories_list = association_histories_list.filter(association__word__language__code=l)
paginator = Paginator(association_histories_list, 50)
page = request.GET.get('page')
try:
association_histories = paginator.page(page)
except PageNotAnInteger:
association_histories = paginator.page(1)
except EmptyPage:
association_histories = paginator.page(paginator.num_pages)
track(request, 'Association history | Profile | TIMA')
return render(request, 'tima/app/profile/association_history.html', locals())
@login_required(login_url='/signin/')
@csrf_protect
def edit(request):
if request.method == 'POST':
form = UserChangeForm(instance=request.user, data=request.POST)
if form.is_valid():
user = form.save()
if form.cleaned_data['cultural_background']:
profile = get_object_or_404(Profile, user=user)
profile.cultural_background = form.cleaned_data['cultural_background']
profile.save()
messages.success(request, _('Your profile has been successfully updated.'))
else:
profile = get_object_or_404(Profile, user=request.user)
form = UserChangeForm(instance=request.user, initial={'cultural_background': profile.cultural_background})
track(request, 'edit | Profile | TIMA')
return render(request, 'tima/app/profile/form.html', locals())
@login_required(login_url='/signin/')
@csrf_protect
def newsletter(request):
remove = request.GET.get('remove')
languages = Language.objects.all()
newsletter, created = Newsletter.objects.get_or_create(user=request.user)
if not newsletter.user.email:
messages.error(request, _('Without specifying an email-address, you can not receive a newsletter.'))
if remove:
newsletter.words.remove(get_object_or_404(Word, id=remove))
newsletter.save()
messages.success(request, _('You removed %(word)s successfully from your newsletter.') % {'word': get_object_or_404(Word, id=remove)})
if request.method == 'POST':
form = NewsletterForm(request.POST)
if form.is_valid():
for word in form.cleaned_data['words']:
newsletter.words.add(word)
newsletter.save()
messages.success(request, ungettext('Added %(words)s to your newsletter.', 'Added %(words)s to your newsletter.', len(form.cleaned_data['words'])) % {'words': ', '.join(['%s (%s)' % (word, word.language) for word in form.cleaned_data['words']])})
form = NewsletterForm()
else:
form = NewsletterForm()
track(request, 'Newsletter | Profile | TIMA')
return render(request, 'tima/app/profile/newsletter.html', locals())