From b69c3a6792d6ff1eaef565bac0e8531d7dddc96b Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 18 Aug 2016 21:06:10 +0200 Subject: [PATCH] Improve and fix user accounts --- core/templates/core/delete_confirm.jinja | 6 +++++ core/templates/core/file_delete_confirm.jinja | 6 +++++ core/templates/core/user_account.jinja | 6 +++++ counter/models.py | 25 +++++++++++++++---- counter/urls.py | 2 ++ counter/views.py | 23 +++++++++++++++++ migrate.py | 14 ++++++++--- 7 files changed, 73 insertions(+), 9 deletions(-) diff --git a/core/templates/core/delete_confirm.jinja b/core/templates/core/delete_confirm.jinja index 9d0bee4c..f5c5de24 100644 --- a/core/templates/core/delete_confirm.jinja +++ b/core/templates/core/delete_confirm.jinja @@ -1,10 +1,16 @@ {% extends "core/base.jinja" %} +{% block title %} +{% trans %}Delete confirmation{% endtrans %} +{% endblock %} + {% block content %}

{% trans %}Delete confirmation{% endtrans %}

{% csrf_token %}

{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}

+
+
{% endblock %} diff --git a/core/templates/core/file_delete_confirm.jinja b/core/templates/core/file_delete_confirm.jinja index 151ba266..3efd2bd7 100644 --- a/core/templates/core/file_delete_confirm.jinja +++ b/core/templates/core/file_delete_confirm.jinja @@ -1,10 +1,16 @@ {% extends "core/file.jinja" %} +{% block title %} +{% trans %}Delete confirmation{% endtrans %} +{% endblock %} + {% block file %}

{% trans %}Delete confirmation{% endtrans %}

{% csrf_token %}

{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}

+
+
{% endblock %} diff --git a/core/templates/core/user_account.jinja b/core/templates/core/user_account.jinja index ca5093f5..991173c0 100644 --- a/core/templates/core/user_account.jinja +++ b/core/templates/core/user_account.jinja @@ -28,6 +28,9 @@ {{ i.operator.get_display_name() }} {{ i.amount }} € {{ i.get_payment_method_display() }} + {% if i.is_owned_by(user) %} + Delete + {% endif %} {% endfor %} @@ -57,6 +60,9 @@ {{ i.quantity }} {{ i.quantity * i.unit_price }} € {{ i.get_payment_method_display() }} + {% if i.is_owned_by(user) %} + Delete + {% endif %} {% endfor %} diff --git a/counter/models.py b/counter/models.py index 066f6bbc..ade72cd2 100644 --- a/counter/models.py +++ b/counter/models.py @@ -5,8 +5,7 @@ from django.conf import settings from django.core.urlresolvers import reverse from django.forms import ValidationError -from datetime import timedelta, datetime -from pytz import timezone +from datetime import timedelta import random import string @@ -188,7 +187,7 @@ class Counter(models.Model): def get_random_barman(self): bl = self.get_barmen_list() - return bl[randrange(0, len(bl))] + return bl[random.randrange(0, len(bl))] def is_open(self): response = False @@ -220,12 +219,20 @@ class Refilling(models.Model): def __str__(self): return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name()) + def is_owned_by(self, user): + return user.can_edit(self.counter) and self.payment_method != "CARD" + # def get_absolute_url(self): # return reverse('counter:details', kwargs={'counter_id': self.id}) + def delete(self, *args, **kwargs): + self.customer.amount -= self.amount + self.customer.save() + super(Refilling, self).delete(*args, **kwargs) + def save(self, *args, **kwargs): if not self.date: - self.date = datetime.now().replace(tzinfo=timezone(settings.TIME_ZONE)) + self.date = timezone.now() self.full_clean() if not self.is_validated: self.customer.amount += self.amount @@ -257,9 +264,17 @@ class Selling(models.Model): return "Selling: %d x %s (%f) for %s" % (self.quantity, self.label, self.quantity*self.unit_price, self.customer.user.get_display_name()) + def is_owned_by(self, user): + return user.can_edit(self.counter) and self.payment_method != "CARD" + + def delete(self, *args, **kwargs): + self.customer.amount += self.quantity * self.unit_price + self.customer.save() + super(Selling, self).delete(*args, **kwargs) + def save(self, *args, **kwargs): if not self.date: - self.date = datetime.now().replace(tzinfo=timezone(settings.TIME_ZONE)) + self.date = timezone.now() self.full_clean() if not self.is_validated: self.customer.amount -= self.quantity * self.unit_price diff --git a/counter/urls.py b/counter/urls.py index 860b34b3..a3190d22 100644 --- a/counter/urls.py +++ b/counter/urls.py @@ -17,6 +17,8 @@ urlpatterns = [ url(r'^admin/producttype/list$', ProductTypeListView.as_view(), name='producttype_list'), url(r'^admin/producttype/create$', ProductTypeCreateView.as_view(), name='new_producttype'), url(r'^admin/producttype/(?P[0-9]+)$', ProductTypeEditView.as_view(), name='producttype_edit'), + url(r'^admin/selling/(?P[0-9]+)/delete$', SellingDeleteView.as_view(), name='selling_delete'), + url(r'^admin/refilling/(?P[0-9]+)/delete$', RefillingDeleteView.as_view(), name='refilling_delete'), ] diff --git a/counter/views.py b/counter/views.py index f6298df0..6e3fc0a6 100644 --- a/counter/views.py +++ b/counter/views.py @@ -410,3 +410,26 @@ class ProductEditView(CanEditPropMixin, UpdateView): template_name = 'core/edit.jinja' # TODO: add management of the 'counters' ForeignKey + +class RefillingDeleteView(CanEditPropMixin, DeleteView): + """ + Delete a refilling (for the admins) + """ + model = Refilling + pk_url_kwarg = "refilling_id" + template_name = 'core/delete_confirm.jinja' + + def get_success_url(self): + return reverse_lazy('core:user_account', kwargs={'user_id': self.object.customer.user.id}) + +class SellingDeleteView(CanEditPropMixin, DeleteView): + """ + Delete a selling (for the admins) + """ + model = Selling + pk_url_kwarg = "selling_id" + template_name = 'core/delete_confirm.jinja' + + def get_success_url(self): + return reverse_lazy('core:user_account', kwargs={'user_id': self.object.customer.user.id}) + diff --git a/migrate.py b/migrate.py index 76b61843..14509f76 100644 --- a/migrate.py +++ b/migrate.py @@ -337,6 +337,15 @@ def migrate_counters(): print("FAIL to migrate counter %s: %s" % (r['id_comptoir'], repr(e))) cur.close() +def reset_customer_amount(): + Refilling.objects.all().delete() + Selling.objects.all().delete() + Invoice.objects.all().delete() + for c in Customer.objects.all(): + c.amount = 0 + c.save() + print("Customer amount reset") + def migrate_refillings(): BANK = { 0: "OTHER", @@ -359,10 +368,6 @@ def migrate_refillings(): """) Refilling.objects.filter(payment_method="SITH_ACCOUNT").delete() print("Sith account refillings deleted") - for c in Customer.objects.all(): - c.amount = 0 - c.save() - print("Customer amount reset") fail = 100 root_cust = Customer.objects.filter(user__id=0).first() mde = Counter.objects.filter(id=1).first() @@ -546,6 +551,7 @@ def main(): # migrate_typeproducts() # migrate_products() # migrate_products_to_counter() + reset_customer_amount() migrate_invoices() migrate_refillings() migrate_sellings()