Improve and fix user accounts

This commit is contained in:
Skia 2016-08-18 21:06:10 +02:00
parent 05bd177a9d
commit b69c3a6792
7 changed files with 73 additions and 9 deletions

View File

@ -1,10 +1,16 @@
{% extends "core/base.jinja" %} {% extends "core/base.jinja" %}
{% block title %}
{% trans %}Delete confirmation{% endtrans %}
{% endblock %}
{% block content %} {% block content %}
<h2>{% trans %}Delete confirmation{% endtrans %}</h2> <h2>{% trans %}Delete confirmation{% endtrans %}</h2>
<form action="" method="post">{% csrf_token %} <form action="" method="post">{% csrf_token %}
<p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p> <p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p>
<input type="submit" value="{% trans %}Confirm{% endtrans %}" /> <input type="submit" value="{% trans %}Confirm{% endtrans %}" />
</form>
<form method="GET" action="javascript:history.back();">
<input type="submit" name="cancel" value="{% trans %}Cancel{% endtrans %}" /> <input type="submit" name="cancel" value="{% trans %}Cancel{% endtrans %}" />
</form> </form>
{% endblock %} {% endblock %}

View File

@ -1,10 +1,16 @@
{% extends "core/file.jinja" %} {% extends "core/file.jinja" %}
{% block title %}
{% trans %}Delete confirmation{% endtrans %}
{% endblock %}
{% block file %} {% block file %}
<h2>{% trans %}Delete confirmation{% endtrans %}</h2> <h2>{% trans %}Delete confirmation{% endtrans %}</h2>
<form action="" method="post">{% csrf_token %} <form action="" method="post">{% csrf_token %}
<p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p> <p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p>
<input type="submit" value="{% trans %}Confirm{% endtrans %}" /> <input type="submit" value="{% trans %}Confirm{% endtrans %}" />
</form>
<form method="GET" action="javascript:history.back();">
<input type="submit" name="cancel" value="{% trans %}Cancel{% endtrans %}" /> <input type="submit" name="cancel" value="{% trans %}Cancel{% endtrans %}" />
</form> </form>
{% endblock %} {% endblock %}

View File

@ -28,6 +28,9 @@
<td><a href="{{ i.operator.get_absolute_url() }}">{{ i.operator.get_display_name() }}</a></td> <td><a href="{{ i.operator.get_absolute_url() }}">{{ i.operator.get_display_name() }}</a></td>
<td>{{ i.amount }} €</td> <td>{{ i.amount }} €</td>
<td>{{ i.get_payment_method_display() }}</td> <td>{{ i.get_payment_method_display() }}</td>
{% if i.is_owned_by(user) %}
<td><a href="{{ url('counter:refilling_delete', refilling_id=i.id) }}">Delete</a></td>
{% endif %}
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -57,6 +60,9 @@
<td>{{ i.quantity }}</td> <td>{{ i.quantity }}</td>
<td>{{ i.quantity * i.unit_price }} €</td> <td>{{ i.quantity * i.unit_price }} €</td>
<td>{{ i.get_payment_method_display() }}</td> <td>{{ i.get_payment_method_display() }}</td>
{% if i.is_owned_by(user) %}
<td><a href="{{ url('counter:selling_delete', selling_id=i.id) }}">Delete</a></td>
{% endif %}
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View File

@ -5,8 +5,7 @@ from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.forms import ValidationError from django.forms import ValidationError
from datetime import timedelta, datetime from datetime import timedelta
from pytz import timezone
import random import random
import string import string
@ -188,7 +187,7 @@ class Counter(models.Model):
def get_random_barman(self): def get_random_barman(self):
bl = self.get_barmen_list() bl = self.get_barmen_list()
return bl[randrange(0, len(bl))] return bl[random.randrange(0, len(bl))]
def is_open(self): def is_open(self):
response = False response = False
@ -220,12 +219,20 @@ class Refilling(models.Model):
def __str__(self): def __str__(self):
return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name()) 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): # def get_absolute_url(self):
# return reverse('counter:details', kwargs={'counter_id': self.id}) # 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): def save(self, *args, **kwargs):
if not self.date: if not self.date:
self.date = datetime.now().replace(tzinfo=timezone(settings.TIME_ZONE)) self.date = timezone.now()
self.full_clean() self.full_clean()
if not self.is_validated: if not self.is_validated:
self.customer.amount += self.amount 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, return "Selling: %d x %s (%f) for %s" % (self.quantity, self.label,
self.quantity*self.unit_price, self.customer.user.get_display_name()) 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): def save(self, *args, **kwargs):
if not self.date: if not self.date:
self.date = datetime.now().replace(tzinfo=timezone(settings.TIME_ZONE)) self.date = timezone.now()
self.full_clean() self.full_clean()
if not self.is_validated: if not self.is_validated:
self.customer.amount -= self.quantity * self.unit_price self.customer.amount -= self.quantity * self.unit_price

View File

@ -17,6 +17,8 @@ urlpatterns = [
url(r'^admin/producttype/list$', ProductTypeListView.as_view(), name='producttype_list'), 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/create$', ProductTypeCreateView.as_view(), name='new_producttype'),
url(r'^admin/producttype/(?P<type_id>[0-9]+)$', ProductTypeEditView.as_view(), name='producttype_edit'), url(r'^admin/producttype/(?P<type_id>[0-9]+)$', ProductTypeEditView.as_view(), name='producttype_edit'),
url(r'^admin/selling/(?P<selling_id>[0-9]+)/delete$', SellingDeleteView.as_view(), name='selling_delete'),
url(r'^admin/refilling/(?P<refilling_id>[0-9]+)/delete$', RefillingDeleteView.as_view(), name='refilling_delete'),
] ]

View File

@ -410,3 +410,26 @@ class ProductEditView(CanEditPropMixin, UpdateView):
template_name = 'core/edit.jinja' template_name = 'core/edit.jinja'
# TODO: add management of the 'counters' ForeignKey # 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})

View File

@ -337,6 +337,15 @@ def migrate_counters():
print("FAIL to migrate counter %s: %s" % (r['id_comptoir'], repr(e))) print("FAIL to migrate counter %s: %s" % (r['id_comptoir'], repr(e)))
cur.close() 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(): def migrate_refillings():
BANK = { BANK = {
0: "OTHER", 0: "OTHER",
@ -359,10 +368,6 @@ def migrate_refillings():
""") """)
Refilling.objects.filter(payment_method="SITH_ACCOUNT").delete() Refilling.objects.filter(payment_method="SITH_ACCOUNT").delete()
print("Sith account refillings deleted") print("Sith account refillings deleted")
for c in Customer.objects.all():
c.amount = 0
c.save()
print("Customer amount reset")
fail = 100 fail = 100
root_cust = Customer.objects.filter(user__id=0).first() root_cust = Customer.objects.filter(user__id=0).first()
mde = Counter.objects.filter(id=1).first() mde = Counter.objects.filter(id=1).first()
@ -546,6 +551,7 @@ def main():
# migrate_typeproducts() # migrate_typeproducts()
# migrate_products() # migrate_products()
# migrate_products_to_counter() # migrate_products_to_counter()
reset_customer_amount()
migrate_invoices() migrate_invoices()
migrate_refillings() migrate_refillings()
migrate_sellings() migrate_sellings()