From e3fd3b81ab45f540e72a62de31f0bb3dd3418ac1 Mon Sep 17 00:00:00 2001
From: klmp200
Date: Mon, 6 Nov 2017 00:22:25 +0100
Subject: [PATCH] Add gift function
---
core/migrations/0027_gift.py | 25 ++++++++++++++
core/models.py | 13 ++++++++
core/templates/core/user_detail.jinja | 35 +++++++++++++++++++-
core/urls.py | 3 ++
core/views/forms.py | 18 +++++++++-
core/views/user.py | 47 +++++++++++++++++++++++++--
sith/settings.py | 5 +++
7 files changed, 141 insertions(+), 5 deletions(-)
create mode 100644 core/migrations/0027_gift.py
diff --git a/core/migrations/0027_gift.py b/core/migrations/0027_gift.py
new file mode 100644
index 00000000..a6c604e8
--- /dev/null
+++ b/core/migrations/0027_gift.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+from django.conf import settings
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('core', '0026_auto_20170926_1512'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Gift',
+ fields=[
+ ('id', models.AutoField(primary_key=True, auto_created=True, verbose_name='ID', serialize=False)),
+ ('label', models.CharField(max_length=255, verbose_name='label')),
+ ('date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date')),
+ ('user', models.ForeignKey(related_name='gifts', to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ ]
diff --git a/core/models.py b/core/models.py
index 6444cb87..da4a709d 100644
--- a/core/models.py
+++ b/core/models.py
@@ -2,6 +2,7 @@
#
# Copyright 2016,2017
# - Skia
+# - Sli
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@@ -1109,3 +1110,15 @@ class Notification(models.Model):
old_notif.save()
return
super(Notification, self).save(*args, **kwargs)
+
+
+class Gift(models.Model):
+ label = models.CharField(_('label'), max_length=255)
+ date = models.DateTimeField(_('date'), default=timezone.now)
+ user = models.ForeignKey(User, related_name='gifts')
+
+ def __str__(self):
+ return "%s - %s" % (self.label, self.date.strftime('%d %b %Y'))
+
+ def is_owned_by(self, user):
+ return user.is_board_member or user.is_root
diff --git a/core/templates/core/user_detail.jinja b/core/templates/core/user_detail.jinja
index aa8b7e23..23266124 100644
--- a/core/templates/core/user_detail.jinja
+++ b/core/templates/core/user_detail.jinja
@@ -66,13 +66,39 @@
{% endif %}
{% else %}
{% trans %}Not subscribed{% endtrans %}
- {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %}
+ {% if user.is_board_member %}
{% trans %}New subscription{% endtrans %}
{% endif %}
{% endif %}
{% endif %}
+{% if user.is_root or user.is_board_member %}
+
+
+ {% if profile.gifts.exists() %}
+
+
+
{% trans %}Last given gift :{% endtrans %} {{ profile.gifts.order_by('-date').first() }}
+
+
+ {% else %}
+ {% trans %}No gift given yet{% endtrans %}
+ {% endif %}
+{% endif %}
+
+
{% endblock %}
{% block script %}
@@ -92,6 +118,13 @@ $( function() {
}
});
} );
+$(function(){
+ $("#drop_gifts").accordion({
+ heightStyle: "content",
+ collapsible: true,
+ active: false
+ });
+});
{% endblock %}
diff --git a/core/urls.py b/core/urls.py
index 216ec064..afbcbf40 100644
--- a/core/urls.py
+++ b/core/urls.py
@@ -2,6 +2,7 @@
#
# Copyright 2016,2017
# - Skia
+# - Sli
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@@ -73,6 +74,8 @@ urlpatterns = [
url(r'^user/(?P[0-9]+)/account$', UserAccountView.as_view(), name='user_account'),
url(r'^user/(?P[0-9]+)/account/(?P[0-9]+)/(?P[0-9]+)$', UserAccountDetailView.as_view(), name='user_account_detail'),
url(r'^user/(?P[0-9]+)/stats$', UserStatsView.as_view(), name='user_stats'),
+ url(r'^user/(?P[0-9]+)/gift/create$', GiftCreateView.as_view(), name='user_gift_create'),
+ url(r'^user/(?P[0-9]+)/gift/delete/(?P[0-9]+)/$', GiftDeleteView.as_view(), name='user_gift_delete'),
# File views
# url(r'^file/add/(?Ppopup)?$', FileCreateView.as_view(), name='file_new'),
diff --git a/core/views/forms.py b/core/views/forms.py
index 8770df79..bff849b9 100644
--- a/core/views/forms.py
+++ b/core/views/forms.py
@@ -2,6 +2,7 @@
#
# Copyright 2016,2017
# - Skia
+# - Sli
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@@ -35,7 +36,7 @@ from ajax_select.fields import AutoCompleteSelectField
import re
-from core.models import User, Page, SithFile
+from core.models import User, Page, SithFile, Gift
from core.utils import resize_image
from io import BytesIO
@@ -283,3 +284,18 @@ class PageForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PageForm, self).__init__(*args, **kwargs)
self.fields['parent'].queryset = self.fields['parent'].queryset.exclude(name=settings.SITH_CLUB_ROOT_PAGE).filter(club=None)
+
+
+class GiftForm(forms.ModelForm):
+ class Meta:
+ model = Gift
+ fields = ['label', 'user']
+
+ label = forms.ChoiceField(choices=settings.SITH_GIFT_LIST)
+
+ def __init__(self, *args, **kwargs):
+ user_id = kwargs.pop('user_id', None)
+ super(GiftForm, self).__init__(*args, **kwargs)
+ if user_id:
+ self.fields['user'].queryset = self.fields['user'].queryset.filter(id=user_id)
+ self.fields['user'].widget = forms.HiddenInput()
diff --git a/core/views/user.py b/core/views/user.py
index 25f7a7d5..c812fd5c 100644
--- a/core/views/user.py
+++ b/core/views/user.py
@@ -2,6 +2,7 @@
#
# Copyright 2016,2017
# - Skia
+# - Sli
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@@ -30,9 +31,10 @@ from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied, ValidationError
from django.http import Http404, HttpResponse
from django.views.generic.edit import UpdateView
-from django.views.generic import ListView, DetailView, TemplateView
+from django.views.generic import ListView, DetailView, TemplateView, CreateView, DeleteView
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
+from django.core.urlresolvers import reverse_lazy
from django.template.response import TemplateResponse
from django.conf import settings
from django.views.generic.dates import YearMixin, MonthMixin
@@ -41,8 +43,8 @@ from datetime import timedelta, date
import logging
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, QuickNotifMixin
-from core.views.forms import RegisteringForm, UserProfileForm, LoginForm, UserGodfathersForm
-from core.models import User, SithFile, Preferences
+from core.views.forms import RegisteringForm, UserProfileForm, LoginForm, UserGodfathersForm, GiftForm
+from core.models import User, SithFile, Preferences, Gift
from subscription.models import Subscription
from trombi.views import UserTrombiForm
@@ -232,6 +234,10 @@ class UserView(UserTabsMixin, CanViewMixin, DetailView):
template_name = "core/user_detail.jinja"
current_tab = 'infos'
+ def get_context_data(self, **kwargs):
+ kwargs = super(UserView, self).get_context_data(**kwargs)
+ kwargs['gift_form'] = GiftForm(user_id=self.object.id, initial={'user': self.object})
+ return kwargs
class UserPicturesView(UserTabsMixin, CanViewMixin, DetailView):
@@ -690,3 +696,38 @@ class UserAccountDetailView(UserAccountBase, YearMixin, MonthMixin):
pass
kwargs['tab'] = "account"
return kwargs
+
+
+class GiftCreateView(CreateView):
+ form_class = GiftForm
+ template_name = 'core/create.jinja'
+
+ def dispatch(self, request, *args, **kwargs):
+ if not (request.user.is_board_member or request.user.is_root):
+ raise PermissionDenied
+ self.user = get_object_or_404(User, pk=kwargs['user_id'])
+ return super(GiftCreateView, self).dispatch(request, *args, **kwargs)
+
+ def get_initial(self):
+ return {'user': self.user}
+
+ def get_form_kwargs(self):
+ kwargs = super(GiftCreateView, self).get_form_kwargs()
+ kwargs['user_id'] = self.user.id
+ return kwargs
+
+ def get_success_url(self):
+ return reverse_lazy('core:user_profile', kwargs={'user_id': self.user.id})
+
+
+class GiftDeleteView(CanEditPropMixin, DeleteView):
+ model = Gift
+ pk_url_kwarg = "gift_id"
+ template_name = 'core/delete_confirm.jinja'
+
+ def dispatch(self, request, *args, **kwargs):
+ self.user = get_object_or_404(User, pk=kwargs['user_id'])
+ return super(GiftDeleteView, self).dispatch(request, *args, **kwargs)
+
+ def get_success_url(self):
+ return reverse_lazy('core:user_profile', kwargs={'user_id': self.user.id})
diff --git a/sith/settings.py b/sith/settings.py
index 43db370d..da718838 100644
--- a/sith/settings.py
+++ b/sith/settings.py
@@ -2,6 +2,7 @@
#
# Copyright 2016,2017
# - Skia
+# - Sli
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@@ -592,6 +593,10 @@ SITH_QUICK_NOTIF = {
SITH_MAILING_DOMAIN = 'utbm.fr'
SITH_MAILING_FETCH_KEY = 'IloveMails'
+SITH_GIFT_LIST = [
+ ('AE Tee-shirt', _("AE tee-shirt"))
+]
+
try:
from .settings_custom import *
print("Custom settings imported", file=sys.stderr)