From 22ab21e4e16e6629d2da07c1ad5315e4f48a162c Mon Sep 17 00:00:00 2001 From: Skia Date: Sat, 19 Nov 2016 17:19:00 +0100 Subject: [PATCH] Add picture-people relation and ask for removal thing --- .../0008_sithfile_asked_for_removal.py | 19 ++++ core/models.py | 1 + core/views/__init__.py | 6 ++ sas/migrations/0002_auto_20161119_1241.py | 28 ++++++ sas/models.py | 17 ++++ sas/templates/sas/album.jinja | 16 ++-- sas/templates/sas/moderation.jinja | 3 + sas/templates/sas/picture.jinja | 89 ++++++++++++++++++- sas/views.py | 60 +++++++++++-- 9 files changed, 221 insertions(+), 18 deletions(-) create mode 100644 core/migrations/0008_sithfile_asked_for_removal.py create mode 100644 sas/migrations/0002_auto_20161119_1241.py diff --git a/core/migrations/0008_sithfile_asked_for_removal.py b/core/migrations/0008_sithfile_asked_for_removal.py new file mode 100644 index 00000000..a5fa30f7 --- /dev/null +++ b/core/migrations/0008_sithfile_asked_for_removal.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0007_auto_20161108_1703'), + ] + + operations = [ + migrations.AddField( + model_name='sithfile', + name='asked_for_removal', + field=models.BooleanField(default=False, verbose_name='asked for removal'), + ), + ] diff --git a/core/models.py b/core/models.py index 1e1c66e4..901e3e59 100644 --- a/core/models.py +++ b/core/models.py @@ -503,6 +503,7 @@ class SithFile(models.Model): size = models.IntegerField(_("size"), default=0) date = models.DateTimeField(_('date'), auto_now=True) is_moderated = models.BooleanField(_("is moderated"), default=False) + asked_for_removal = models.BooleanField(_("asked for removal"), default=False) class Meta: verbose_name = _("file") diff --git a/core/views/__init__.py b/core/views/__init__.py index 66f27717..aeaceb08 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -58,6 +58,8 @@ class CanEditPropMixin(View): """ def dispatch(self, request, *arg, **kwargs): res = super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs) + if res.__class__.status_code == 302: + return res if hasattr(self, 'object'): obj = self.object elif hasattr(self, 'object_list'): @@ -76,6 +78,8 @@ class CanEditMixin(View): """ def dispatch(self, request, *arg, **kwargs): res = super(CanEditMixin, self).dispatch(request, *arg, **kwargs) + if res.__class__.status_code == 302: + return res if hasattr(self, 'object'): obj = self.object elif hasattr(self, 'object_list'): @@ -94,6 +98,8 @@ class CanViewMixin(View): """ def dispatch(self, request, *arg, **kwargs): res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs) + if res.__class__.status_code == 302: + return res if hasattr(self, 'object'): obj = self.object elif hasattr(self, 'object_list'): diff --git a/sas/migrations/0002_auto_20161119_1241.py b/sas/migrations/0002_auto_20161119_1241.py new file mode 100644 index 00000000..ff51d24c --- /dev/null +++ b/sas/migrations/0002_auto_20161119_1241.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('sas', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='PeoplePictureRelation', + fields=[ + ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)), + ('picture', models.ForeignKey(related_name='people', to='sas.Picture', verbose_name='picture')), + ('user', models.ForeignKey(related_name='pictures', to=settings.AUTH_USER_MODEL, verbose_name='user')), + ], + ), + migrations.AlterUniqueTogether( + name='peoplepicturerelation', + unique_together=set([('user', 'picture')]), + ), + ] diff --git a/sas/models.py b/sas/models.py index 08bfc567..7a786797 100644 --- a/sas/models.py +++ b/sas/models.py @@ -1,6 +1,7 @@ from django.db import models from django.conf import settings from django.core.urlresolvers import reverse +from django.utils.translation import ugettext_lazy as _ from core.models import SithFile, User @@ -23,6 +24,12 @@ class Picture(SithFile): def get_download_url(self): return reverse('sas:download', kwargs={'picture_id': self.id}) + def get_next(self): + return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__gt=self.id).order_by('id').first() + + def get_previous(self): + return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__lt=self.id).order_by('id').last() + class Album(SithFile): class Meta: proxy = True @@ -42,3 +49,13 @@ class Album(SithFile): def get_absolute_url(self): return reverse('sas:album', kwargs={'album_id': self.id}) +class PeoplePictureRelation(models.Model): + """ + The PeoplePictureRelation class makes the connection between User and Picture + + """ + user = models.ForeignKey(User, verbose_name=_('user'), related_name="pictures", null=False, blank=False) + picture = models.ForeignKey(Picture, verbose_name=_('picture'), related_name="people", null=False, blank=False) + + class Meta: + unique_together = ['user', 'picture'] diff --git a/sas/templates/sas/album.jinja b/sas/templates/sas/album.jinja index 1a400b9e..5555ff55 100644 --- a/sas/templates/sas/album.jinja +++ b/sas/templates/sas/album.jinja @@ -6,11 +6,6 @@ {% block content %}

{{ album.get_display_name() }}

-
- {% csrf_token %} - {{ form.as_p() }} -

-
{% for a in album.children.filter(is_folder=True, is_moderated=True).all() %}
@@ -22,13 +17,18 @@ {# for a in album.children.filter(mime_type__in=['image/jpeg', 'image/png']).all() #} {% for p in album.children.filter(is_folder=False, is_moderated=True).all() %} {% if p.as_picture.can_be_viewed_by(user) %} -
- - {{ p.get_display_name() }} +
+ + {{ p.get_display_name() }}
{% endif %} {% endfor %}
+
+ {% csrf_token %} + {{ form.as_p() }} +

+
{% endblock %} diff --git a/sas/templates/sas/moderation.jinja b/sas/templates/sas/moderation.jinja index b73204ef..170652a7 100644 --- a/sas/templates/sas/moderation.jinja +++ b/sas/templates/sas/moderation.jinja @@ -23,6 +23,9 @@ {% trans %}Owner: {% endtrans %}{{ p.owner.get_display_name() }}
{% trans %}Date: {% endtrans %}{{ p.date|date(DATE_FORMAT) }} {{ p.date|time(TIME_FORMAT) }}

+ {% if p.asked_for_removal %} +

{% trans %}Asked for removal{% endtrans %}

+ {% endif %}

{% trans %}Moderate{% endtrans %} diff --git a/sas/templates/sas/picture.jinja b/sas/templates/sas/picture.jinja index 39feee7d..3b261d68 100644 --- a/sas/templates/sas/picture.jinja +++ b/sas/templates/sas/picture.jinja @@ -1,16 +1,97 @@ {% extends "core/base.jinja" %} +{% block head %} +{{ super() }} + +{% endblock %} + {% block title %} {% trans %}SAS{% endtrans %} {% endblock %} +{% macro print_path(file) %} +{% if file %} +{{ print_path(file.parent) }} +{{ file.get_display_name() }} > +{% endif %} +{% endmacro %} + {% block content %} +{{ print_path(picture.parent) }} {{ picture.get_display_name() }}

{{ picture.get_display_name() }}

-
- PREV / NEXT +
+ {{ picture.get_display_name() }}
-
- {{ picture.get_display_name() }} +
+
+ + +
+
+ +
+
+
+ {% csrf_token %} + {{ form.as_p() }} +

+
+
+

+ {% trans %}Ask for removal{% endtrans %} +

{% endblock %} +{% block script %} +{{ super() }} + +{% endblock %} diff --git a/sas/views.py b/sas/views.py index 8a9d7912..321e8259 100644 --- a/sas/views.py +++ b/sas/views.py @@ -1,5 +1,5 @@ -from django.shortcuts import render -from django.core.urlresolvers import reverse_lazy +from django.shortcuts import render, redirect +from django.core.urlresolvers import reverse_lazy, reverse from django.views.generic import ListView, DetailView, RedirectView, TemplateView from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin, FormView from django.utils.translation import ugettext as _ @@ -8,14 +8,14 @@ from django.conf import settings from django import forms from django.core.exceptions import PermissionDenied -from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField +from ajax_select import make_ajax_form, make_ajax_field from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin from core.views.forms import SelectUser, LoginForm, SelectDate, SelectDateTime from core.views.files import send_file from core.models import SithFile, User -from sas.models import Picture, Album +from sas.models import Picture, Album, PeoplePictureRelation class SASForm(forms.Form): album_name = forms.CharField(label=_("Add a new album"), max_length=30, required=False) @@ -41,6 +41,13 @@ class SASForm(forms.Form): except Exception as e: self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") % {'file_name': f, 'msg': repr(e)}) +class RelationForm(forms.ModelForm): + class Meta: + model = PeoplePictureRelation + fields = ['picture', 'user'] + widgets = {'picture': forms.HiddenInput} + user = make_ajax_field(PeoplePictureRelation, 'user', 'users', help_text="") + class SASMainView(FormView): form_class = SASForm template_name = "sas/main.jinja" @@ -65,11 +72,51 @@ class SASMainView(FormView): kwargs['root_file'] = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first() return kwargs -class PictureView(DetailView, CanViewMixin): +class PictureView(CanViewMixin, DetailView, FormMixin): model = Picture + form_class = RelationForm pk_url_kwarg = "picture_id" template_name = "sas/picture.jinja" + def get_initial(self): + return {'picture': self.object} + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + self.form = self.get_form() + if 'remove_user' in request.GET.keys(): + try: + user = User.objects.filter(id=int(request.GET['remove_user'])).first() + if user.id == request.user.id or request.user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_ID): + r = PeoplePictureRelation.objects.filter(user=user, picture=self.object).delete() + except: pass + if 'ask_removal' in request.GET.keys(): + self.object.is_moderated = False + self.object.asked_for_removal = True + self.object.save() + return redirect("sas:album", album_id=self.object.parent.id) + return super(PictureView, self).get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + self.object = self.get_object() + self.form = self.get_form() + if request.user.is_authenticated() and request.user.is_in_group('ae-membres'): + if self.form.is_valid(): + PeoplePictureRelation(user=self.form.cleaned_data['user'], + picture=self.form.cleaned_data['picture']).save() + return super(PictureView, self).form_valid(self.form) + else: + self.form.add_error(None, _("You do not have the permission to do that")) + return self.form_invalid(self.form) + + def get_context_data(self, **kwargs): + kwargs = super(PictureView, self).get_context_data(**kwargs) + kwargs['form'] = self.form + return kwargs + + def get_success_url(self): + return reverse('sas:picture', kwargs={'picture_id': self.object.id}) + def send_pict(request, picture_id): return send_file(request, picture_id, Picture) @@ -98,7 +145,7 @@ class AlbumView(CanViewMixin, DetailView, FormMixin): return self.form_invalid(self.form) def get_success_url(self): - return reverse_lazy('sas:album', kwargs={'album_id': self.object.id}) + return reverse('sas:album', kwargs={'album_id': self.object.id}) def get_context_data(self, **kwargs): kwargs = super(AlbumView, self).get_context_data(**kwargs) @@ -120,6 +167,7 @@ class ModerationView(TemplateView): pict.delete() elif v == "moderate": pict.is_moderated = True + pict.asked_for_removal = False pict.save() except: pass return super(ModerationView, self).get(request, *args, **kwargs)