From 824d882b8124080fecd792e595cdf002c6a94712 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Fri, 6 Aug 2021 13:23:16 +0200 Subject: [PATCH] feat: Deposits app draft --- deposits/__init__.py | 0 deposits/admin.py | 41 +++ deposits/apps.py | 27 ++ deposits/forms.py | 85 ++++++ deposits/locale/fr/LC_MESSAGES/django.po | 19 ++ deposits/models.py | 131 ++++++++++ deposits/templates/deposits/aff_deposit.html | 74 ++++++ .../templates/deposits/aff_deposit_item.html | 48 ++++ deposits/templates/deposits/aff_deposits.html | 66 +++++ deposits/templates/deposits/aff_profil.html | 48 ++++ deposits/templates/deposits/delete.html | 39 +++ deposits/templates/deposits/deposit.html | 44 ++++ .../deposits/index_deposit_item.html | 43 ++++ deposits/templates/deposits/navbar.html | 24 ++ deposits/templates/deposits/sidebar.html | 28 ++ deposits/tests.py | 3 + deposits/urls.py | 49 ++++ deposits/views.py | 242 ++++++++++++++++++ 18 files changed, 1011 insertions(+) create mode 100644 deposits/__init__.py create mode 100644 deposits/admin.py create mode 100644 deposits/apps.py create mode 100644 deposits/forms.py create mode 100644 deposits/locale/fr/LC_MESSAGES/django.po create mode 100644 deposits/models.py create mode 100644 deposits/templates/deposits/aff_deposit.html create mode 100644 deposits/templates/deposits/aff_deposit_item.html create mode 100644 deposits/templates/deposits/aff_deposits.html create mode 100644 deposits/templates/deposits/aff_profil.html create mode 100644 deposits/templates/deposits/delete.html create mode 100644 deposits/templates/deposits/deposit.html create mode 100644 deposits/templates/deposits/index_deposit_item.html create mode 100644 deposits/templates/deposits/navbar.html create mode 100644 deposits/templates/deposits/sidebar.html create mode 100644 deposits/tests.py create mode 100644 deposits/urls.py create mode 100644 deposits/views.py diff --git a/deposits/__init__.py b/deposits/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/deposits/admin.py b/deposits/admin.py new file mode 100644 index 00000000..f9ebd881 --- /dev/null +++ b/deposits/admin.py @@ -0,0 +1,41 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Deposit admin model +""" + + +from django.contrib import admin +from reversion.admin import VersionAdmin + +from .models import Deposit, DepositItem + + +class DepositAdmin(VersionAdmin): + pass + + +class DepositItemAdmin(VersionAdmin): + pass + + +admin.site.register(Deposit, DepositAdmin) +admin.site.register(DepositItem, DepositItemAdmin) diff --git a/deposits/apps.py b/deposits/apps.py new file mode 100644 index 00000000..a0b92c1e --- /dev/null +++ b/deposits/apps.py @@ -0,0 +1,27 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from django.apps import AppConfig + + +class DepositsConfig(AppConfig): + """Configuration of the optional deposits app.""" + + name = "deposits" diff --git a/deposits/forms.py b/deposits/forms.py new file mode 100644 index 00000000..460fbfe7 --- /dev/null +++ b/deposits/forms.py @@ -0,0 +1,85 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Deposit form +""" + +from django import forms +from django.forms import Form, ModelForm +from django.utils.translation import ugettext_lazy as _ + +from re2o.field_permissions import FieldPermissionFormMixin +from re2o.mixins import FormRevMixin +from re2o.widgets import AutocompleteModelWidget + +from .models import Deposit, DepositItem + + +class DepositForm(FieldPermissionFormMixin, FormRevMixin, ModelForm): + """ + Form used to manage and create an invoice and its fields. + """ + + def __init__(self, *args, creation=False, **kwargs): + super(DepositForm, self).__init__(*args, **kwargs) + + if not creation: + self.fields["user"].label = _("Member") + self.fields["user"].empty_label = _("Select the proprietary member") + self.fields["returned"].label = _("Deposit returned") + + class Meta: + model = Deposit + fields = ("user", "item", "returned") + widgets = { + "user": AutocompleteModelWidget(url="/users/user-autocomplete"), + } + + +class DepositItemForm(FormRevMixin, ModelForm): + """ + Form used to create a deposit item. + """ + + class Meta: + model = DepositItem + fields = "__all__" + + +class DelDepositItemForm(FormRevMixin, Form): + """ + Form used to delete one or more of the currently available deposit items. + The user must choose the one to delete by checking the boxes. + """ + + deposit_items = forms.ModelMultipleChoiceField( + queryset=DepositItem.objects.none(), + label=_("Current deposit items"), + widget=forms.CheckboxSelectMultiple, + ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop("instances", None) + super(DelDepositItemForm, self).__init__(*args, **kwargs) + if instances: + self.fields["deposit_items"].queryset = instances + else: + self.fields["deposit_items"].queryset = DepositItem.objects.all() diff --git a/deposits/locale/fr/LC_MESSAGES/django.po b/deposits/locale/fr/LC_MESSAGES/django.po new file mode 100644 index 00000000..39d2e472 --- /dev/null +++ b/deposits/locale/fr/LC_MESSAGES/django.po @@ -0,0 +1,19 @@ +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. diff --git a/deposits/models.py b/deposits/models.py new file mode 100644 index 00000000..81b3ee2d --- /dev/null +++ b/deposits/models.py @@ -0,0 +1,131 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2019 Arthur Grisel-Davy +# Copyright © 2020 Gabriel Détraz +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Deposit model +""" + +from __future__ import absolute_import + +from django.db import models +from django.utils.translation import ugettext_lazy as _ + +from re2o.mixins import AclMixin, RevMixin + + +class Deposit(RevMixin, AclMixin, models.Model): + """ + The model for a deposit. It reprensents the fact that a user made a deposit + as a guarantee for an item which should be returned to get the deposit + back. + + A deposit is linked to : + * a user (the one who made the deposit) + * an item (borrowed in exchange for the deposit) + Every deposit is dated throught the 'date' value. + A deposit has a 'returned' value (default: False) which means that the item + was returned by the user and the deposit was payed back. The + 'returned_date' attribute stores when the item was returned. + """ + + user = models.ForeignKey("users.User", on_delete=models.PROTECT) + item = models.ForeignKey("DepositItem", on_delete=models.PROTECT) + date = models.DateTimeField(auto_now_add=True, verbose_name=_("date")) + returned = models.BooleanField(default=False, verbose_name=_("returned")) + return_date = models.DateTimeField(default=None, verbose_name=_("return date")) + + class Meta: + abstract = False + verbose_name = _("deposit") + verbose_name_plural = _("deposits") + + def __str__(self): + if self.returned: + return _( + "Deposit from {name} for {item} at {date}, returned at {return_date}" + ).format( + name=self.user.get_full_name(), + item=self.item, + date=self.date, + return_date=self.return_date, + ) + else: + return _( + "Deposit from {name} for {item} at {date}, not yet returned" + ).format( + name=self.user.get_full_name(), + item=self.item, + date=self.date, + ) + + def can_view(self, user_request, *_args, **_kwargs): + """Check that the user has the right to view the deposit or that it + belongs to them.""" + if ( + not user_request.has_perm("deposits.view_deposit") + and self.user != user_request + ): + return ( + False, + _("You don't have the right to view other deposits than yours."), + ("deposits.view_deposit",), + ) + else: + return True, None, None + + @staticmethod + def can_view_all(user_request, *_args, **_kwargs): + """Check that the user has access to the list of all tickets.""" + can = user_request.has_perm("deposits.view_deposit") + return ( + can, + _("You don't have the right to view the list of deposits.") + if not can + else None, + ("deposits.view_deposit",), + ) + + +class DepositItem(RevMixin, AclMixin, models.Model): + """An item for a deposit. + + Attributes: + name: the name of this deposit item. + deposit_amount: the amount needed to be deposited by users. + """ + + name = models.CharField( + max_length=255, + blank=False, + null=False, + unique=True, + verbose_name=_("designation"), + ) + deposit_amount = models.DecimalField( + max_digits=5, decimal_places=2, verbose_name=_("deposit amount") + ) + + class Meta: + verbose_name = "deposit item" + verbose_name_plural = "deposit items" + + def __str__(self): + return _("Deposit item {name}").format(name=self.name) diff --git a/deposits/templates/deposits/aff_deposit.html b/deposits/templates/deposits/aff_deposit.html new file mode 100644 index 00000000..f9284d7b --- /dev/null +++ b/deposits/templates/deposits/aff_deposit.html @@ -0,0 +1,74 @@ +{% extends 'users/sidebar.html' %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load i18n %} +{% load humanize %} +{% load logs_extra %} +{% load acl %} + +{% block title %}{% trans "Deposits" %}{% endblock %} + +{% block content %} + +

{% blocktrans with id=deposit.id %}Deposit #{{id}}{% endblocktrans %} +{% if deposit.returned %} +{% trans "Returned" %} +{% else %} +{% trans "Not returned" %} +{% endif %} +

+ +
+
+ {% trans "Lent to" %} + + {{ deposit.user.get_full_name }} + + {{ deposit.date | naturalday}}. + +
+ {% can_edit deposit %} + {% trans "Edit" %} + {% if not deposit.returned %} + {% trans "Mark as returned" %} + {% else %} + {% trans "Mark as not returned" %} + {% endif %} + {% acl_end %} + {% history_button deposit text=True %} +
+
+
+ +

{% trans "Item:" %} {{deposit.item}}

+ +
+ +
+ +
+ {% trans "All deposits" %} +
+ +{% endblock %} diff --git a/deposits/templates/deposits/aff_deposit_item.html b/deposits/templates/deposits/aff_deposit_item.html new file mode 100644 index 00000000..702b340a --- /dev/null +++ b/deposits/templates/deposits/aff_deposit_item.html @@ -0,0 +1,48 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} +{% load i18n %} +{% load logs_extra %} +{% load design %} + + + + + + + + + + {% for item in item_list %} + + + + + + {% endfor %} +
{% trans "Deposit" %}{% trans "Amount" %}
{{ item.name }}{{ item.deposit_amount }} + {% can_edit item %} + {% include 'buttons/edit.html' with href='deposits:edit-deposit-item' id=item.id %} + {% acl_end %} + {% history_button item %} +
diff --git a/deposits/templates/deposits/aff_deposits.html b/deposits/templates/deposits/aff_deposits.html new file mode 100644 index 00000000..f5eeff6a --- /dev/null +++ b/deposits/templates/deposits/aff_deposits.html @@ -0,0 +1,66 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load i18n %} + +{% block content %} + +
+ + + + + + + + + + + + {% for deposit in deposits_list %} + + + + + + + {% if deposit.returned %} + + {% else %} + + {% endif %} + + {% endfor %} + + +
{% trans "User" %}{% trans "Item" %}{% trans "Amount" %}{% trans "Date" %}{% trans "Returned" %}
+ + + + {{ deposit.user.get_short_name }}{{ deposit.item.name }}{{ deposit.item.deposit_amount }} €{{ deposit.date }}
+ + {% if deposits_list.paginator %} + {% include 'pagination.html' with list=deposits_list go_to_id="deposits" %} + {% endif %} +
+{% endblock %} diff --git a/deposits/templates/deposits/aff_profil.html b/deposits/templates/deposits/aff_profil.html new file mode 100644 index 00000000..14ee6e2b --- /dev/null +++ b/deposits/templates/deposits/aff_profil.html @@ -0,0 +1,48 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} +{% load i18n %} + +
+
+

+ {% trans "Deposits" %} +

+
+
+
+ {% can_create Deposit %} + + {% trans "Add a deposit" %} + + {% acl_end %} +
+
+ {% if deposits_list %} + {% include 'deposits/aff_deposits.html' with deposits_list=deposits_list %} + {% else %} +

{% trans "No deposit" %}

+ {% endif %} +
+
+
diff --git a/deposits/templates/deposits/delete.html b/deposits/templates/deposits/delete.html new file mode 100644 index 00000000..027bce15 --- /dev/null +++ b/deposits/templates/deposits/delete.html @@ -0,0 +1,39 @@ +{% extends 'deposits/sidebar.html' %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load i18n %} + +{% block title %}{% trans "Deletion of subscriptions" %}{% endblock %} + +{% block content %} + +
+ {% csrf_token %} +

+ {% blocktrans %}Warning: are you sure you really want to delete this {{ objet_name }} object ( {{ objet }} )?{% endblocktrans %} +

+ {% trans "Confirm" as tr_confirm %} + {% bootstrap_button tr_confirm button_type='submit' icon='trash' button_class='btn-danger' %} +
+{% endblock %} diff --git a/deposits/templates/deposits/deposit.html b/deposits/templates/deposits/deposit.html new file mode 100644 index 00000000..24dfb476 --- /dev/null +++ b/deposits/templates/deposits/deposit.html @@ -0,0 +1,44 @@ +{% extends 'deposits/sidebar.html' %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load staticfiles%} +{% load i18n %} + +{% block title %}{% trans "Create or edit deposit" %}{% endblock %} + +{% block content %} + +{% if title %} +

{{ title }}

+{% else %} +

{% trans "Add" %}

+{% endif %} + +
+ {% csrf_token %} + {% bootstrap_form depositform %} + {% bootstrap_button action_name button_type='submit' icon='ok' button_class='btn-success' %} +
+ +{% endblock %} diff --git a/deposits/templates/deposits/index_deposit_item.html b/deposits/templates/deposits/index_deposit_item.html new file mode 100644 index 00000000..13cfe6b5 --- /dev/null +++ b/deposits/templates/deposits/index_deposit_item.html @@ -0,0 +1,43 @@ +{% extends 'deposits/sidebar.html' %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load bootstrap3 %} +{% load acl %} +{% load i18n %} + +{% block title %}{% trans "Deposit items" %}{% endblock %} + +{% block content %} +

{% trans "List of deposit items" %}

+ {% can_create DepositItem %} + + {% trans "Add a deposit item" %} + + {% acl_end %} + {% can_delete DepositItem %} + + {% trans "Delete one or several deposit items" %} + + {% acl_end %} + {% include 'deposits/aff_deposit_item.html' with item_list=item_list %} +{% endblock %} diff --git a/deposits/templates/deposits/navbar.html b/deposits/templates/deposits/navbar.html new file mode 100644 index 00000000..378ee066 --- /dev/null +++ b/deposits/templates/deposits/navbar.html @@ -0,0 +1,24 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load i18n %} +
  • {% trans "Deposit items" %}
  • diff --git a/deposits/templates/deposits/sidebar.html b/deposits/templates/deposits/sidebar.html new file mode 100644 index 00000000..fcf4bcbc --- /dev/null +++ b/deposits/templates/deposits/sidebar.html @@ -0,0 +1,28 @@ +{% extends 'base.html' %} +{% comment %} +Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2021 Jean-Romain Garnier + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} +{% load i18n %} + +{% block sidebar %} +{% endblock %} diff --git a/deposits/tests.py b/deposits/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/deposits/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/deposits/urls.py b/deposits/urls.py new file mode 100644 index 00000000..51ef34e0 --- /dev/null +++ b/deposits/urls.py @@ -0,0 +1,49 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Deposits url +""" + +from django.urls import path + +from . import views + +app_name = "deposits" + +urlpatterns = [ + path("new_deposit/", views.new_deposit, name="new-deposit"), + path("edit_deposit/", views.edit_deposit, name="edit-deposit"), + path("del_deposit/", views.del_deposit, name="del-deposit"), + path("aff_deposit/", views.aff_deposit, name="aff-deposit"), + path( + "change_deposit_status/", + views.change_deposit_status, + name="change-deposit-status", + ), + path("add_deposit_item", views.add_deposit_item, name="add-deposit-item"), + path( + "edit_deposit_item/", + views.edit_deposit_item, + name="edit-deposit-item", + ), + path("del_deposit_item", views.del_deposit_item, name="del-deposit-item"), + path("index_deposit_item", views.index_deposit_item, name="index-deposit-item"), +] diff --git a/deposits/views.py b/deposits/views.py new file mode 100644 index 00000000..2a466c07 --- /dev/null +++ b/deposits/views.py @@ -0,0 +1,242 @@ +# -*- mode: python; coding: utf-8 -*- +# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2021 Jean-Romain Garnier +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Deposits views +""" + +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.shortcuts import redirect, render +from django.template.loader import render_to_string +from django.urls import reverse +from django.utils.translation import ugettext as _ + +from preferences.models import GeneralOption +from re2o.acl import ( + can_create, + can_delete, + can_delete_set, + can_edit, + can_view, + can_view_all, +) +from re2o.base import re2o_paginator +from re2o.views import form +from users.models import User + +from .forms import DepositForm, DepositItemForm, DelDepositItemForm +from .models import Deposit, DepositItem + + +@login_required +@can_create(Deposit) +@can_edit(User) +def new_deposit(request, user, userid): + """ + View called to create a new deposit. + """ + deposit = Deposit(user=user) + # Building the invoice form and the article formset + deposit_form = DepositForm( + request.POST or None, instance=deposit, user=request.user, creation=True + ) + + if deposit_form.is_valid(): + deposit_form.save() + messages.success(request, _("The deposit was created.")) + return redirect(reverse("users:profil", kwargs={"userid": deposit.user.pk})) + + return form( + { + "depositform": deposit_form, + "action_name": _("Confirm"), + "title": _("New deposit"), + }, + "deposits/deposit.html", + request, + ) + + +@login_required +@can_edit(Deposit) +def edit_deposit(request, deposit, **_kwargs): + """ + View used to edit an existing deposit. + """ + deposit_form = DepositForm( + request.POST or None, instance=deposit, user=request.user + ) + if deposit_form.is_valid(): + deposit_form.save() + messages.success(request, _("The deposit was edited.")) + return redirect(reverse("users:profil", kwargs={"userid": deposit.user.pk})) + + return form( + { + "depositform": deposit_form, + "action_name": _("Edit"), + "title": _("Edit deposit"), + }, + "deposits/deposit.html", + request, + ) + + +@login_required +@can_delete(Deposit) +def del_deposit(request, deposit, **_kwargs): + """ + View used to delete an existing deposit. + """ + if request.method == "POST": + deposit.delete() + messages.success(request, _("The deposit was deleted.")) + return redirect(reverse("users:profil", kwargs={"userid": deposit.user.pk})) + return form( + {"object": deposit, "objet_name": _("deposit")}, + "deposits/delete.html", + request, + ) + + +@login_required +@can_view(Deposit) +def aff_deposit(request, deposit, **_kwargs): + """ + View used to view an existing deposit. + """ + return render( + request, + "deposits/aff_deposit.html", + {"deposit": deposit}, + ) + + +@login_required +@can_edit(Deposit) +def change_deposit_status(request, deposit, depositid): + """View used to change a ticket's status.""" + deposit.returned = not deposit.solved + deposit.save() + return redirect( + reverse("deposits:aff-deposit", kwargs={"depositid": str(depositid)}) + ) + + +@login_required +@can_create(DepositItem) +def add_deposit_item(request): + """ + View used to add a deposit item. + """ + item = DepositItemForm(request.POST or None) + if item.is_valid(): + item.save() + messages.success(request, _("The item was created.")) + return redirect(reverse("deposits:index-deposit-item")) + return form( + { + "depositform": item, + "action_name": _("Add"), + "title": _("New deposit item"), + }, + "deposits/deposit.html", + request, + ) + + +@login_required +@can_edit(DepositItem) +def edit_deposit_item(request, item_instance, **_kwargs): + """ + View used to edit a deposit item. + """ + item = DepositItemForm(request.POST or None, instance=item_instance) + if item.is_valid(): + if item.changed_data: + item.save() + messages.success(request, _("The item was edited.")) + return redirect(reverse("deposits:index-deposit-item")) + return form( + { + "depositform": item, + "action_name": _("Edit"), + "title": _("Edit deposit item"), + }, + "deposits/deposit.html", + request, + ) + + +@login_required +@can_delete_set(DepositItem) +def del_deposit_item(request, instances): + """ + View used to delete one of the deposit items. + """ + item = DelDepositItemForm(request.POST or None, instances=instances) + if item.is_valid(): + item_del = item.cleaned_data["items"] + item_del.delete() + messages.success(request, _("The items were deleted.")) + return redirect(reverse("deposits:index-deposit-item")) + return form( + { + "depositform": item, + "action_name": _("Delete"), + "title": _("Delete deposit item"), + }, + "deposits/deposit.html", + request, + ) + + +@login_required +@can_view_all(DepositItem) +def index_deposit_item(request): + """ + View used to display the list of all available deposit items. + """ + # TODO : Offer other means of sorting + item_list = DepositItem.objects.order_by("name") + return render(request, "deposits/index_deposit_item.html", {"item_list": item_list}) + + +# Canonic views for optional apps +def aff_profil(request, user): + """View used to display the deposits on a user's profile.""" + deposits_list = Deposit.objects.filter(user=user).all().order_by("-date") + pagination_number = GeneralOption.get_cached_value("pagination_large_number") + + deposits = re2o_paginator(request, deposits_list, pagination_number) + context = { + "deposits_list": deposits, + } + return render_to_string( + "deposits/aff_profil.html", context=context, request=request, using=None + ) + + +def navbar_user(): + """View used to display a link to deposit items in the navbar (in the dropdown + menu Treasury). + """ + return ("cotisations", render_to_string("deposits/navbar.html"))