From 0689f864d254a17046fc6e950c97407b56120ce5 Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 18 Aug 2016 21:32:18 +0200 Subject: [PATCH] Migrate permanencies and add user stats view --- core/templates/core/user_base.jinja | 1 + core/templates/core/user_stats.jinja | 18 +++++++++++++++ core/urls.py | 1 + core/views/user.py | 16 ++++++++++++++ counter/admin.py | 1 + migrate.py | 33 +++++++++++++++++++++++----- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 core/templates/core/user_stats.jinja diff --git a/core/templates/core/user_base.jinja b/core/templates/core/user_base.jinja index 26f11720..2e7410c6 100644 --- a/core/templates/core/user_base.jinja +++ b/core/templates/core/user_base.jinja @@ -5,6 +5,7 @@
{{ profile.get_display_name() }}
{% trans %}Infos{% endtrans %} + {% trans %}Stats{% endtrans %} {% if can_edit(profile, request.user) or user.id == profile.id %} {% trans %}Edit{% endtrans %} {% endif %} diff --git a/core/templates/core/user_stats.jinja b/core/templates/core/user_stats.jinja new file mode 100644 index 00000000..ba25aac7 --- /dev/null +++ b/core/templates/core/user_stats.jinja @@ -0,0 +1,18 @@ +{% extends "core/user_base.jinja" %} + +{% block title %} +{% trans user_name=profile.get_display_name() %}{{ user_name }}'s stats{% endtrans %} +{% endblock %} + +{% block infos %} + {% if profile.permanencies %} +
+

Permanencies

+

+ {{ total_time }} +

+
+ {% endif %} +{% endblock %} + + diff --git a/core/urls.py b/core/urls.py index 14aedbc2..45f43691 100644 --- a/core/urls.py +++ b/core/urls.py @@ -31,6 +31,7 @@ urlpatterns = [ url(r'^user/(?P[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'), url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'), url(r'^user/(?P[0-9]+)/account$', UserAccountView.as_view(), name='user_account'), + url(r'^user/(?P[0-9]+)/stats$', UserStatsView.as_view(), name='user_stats'), # File views # url(r'^file/add/(?Ppopup)?$', FileCreateView.as_view(), name='file_new'), diff --git a/core/views/user.py b/core/views/user.py index b45f25fd..3a26423b 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -10,6 +10,8 @@ from django.forms.models import modelform_factory from django.forms import CheckboxSelectMultiple from django.template.response import TemplateResponse from django.conf import settings + +from datetime import timedelta import logging from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin @@ -121,6 +123,20 @@ class UserView(CanViewMixin, DetailView): context_object_name = "profile" template_name = "core/user_detail.jinja" +class UserStatsView(CanViewMixin, DetailView): + """ + Display a user's stats + """ + model = User + pk_url_kwarg = "user_id" + context_object_name = "profile" + template_name = "core/user_stats.jinja" + + def get_context_data(self, **kwargs): + kwargs = super(UserStatsView, self).get_context_data(**kwargs) + kwargs['total_time'] = sum([p.end-p.start for p in self.object.permanencies.all()], timedelta()) + return kwargs + class UserMiniView(CanViewMixin, DetailView): """ Display a user's profile diff --git a/counter/admin.py b/counter/admin.py index bb9735d0..84c4a6d7 100644 --- a/counter/admin.py +++ b/counter/admin.py @@ -9,4 +9,5 @@ admin.site.register(Product) admin.site.register(Counter) admin.site.register(Refilling) admin.site.register(Selling) +admin.site.register(Permanency) diff --git a/migrate.py b/migrate.py index 14509f76..6e7953ed 100644 --- a/migrate.py +++ b/migrate.py @@ -18,7 +18,7 @@ from django.forms import ValidationError from core.models import User, SithFile from club.models import Club, Membership -from counter.models import Customer, Counter, Selling, Refilling, Product, ProductType +from counter.models import Customer, Counter, Selling, Refilling, Product, ProductType, Permanency from subscription.models import Subscription, Subscriber from eboutic.models import Invoice, InvoiceItem @@ -539,6 +539,28 @@ def migrate_sellings(): print("FAIL to migrate selling %s: %s" % (r['id_facture'], repr(e))) cur.close() +def migrate_permanencies(): + cur = db.cursor(MySQLdb.cursors.SSDictCursor) + cur.execute(""" + SELECT * + FROM cpt_tracking + """) + Permanency.objects.all().delete() + print("Permanencies deleted") + for r in cur: + try: + counter = Counter.objects.filter(id=r['id_comptoir']).first() + user = User.objects.filter(id=r['id_utilisateur']).first() + new = Permanency( + user=user, + counter=counter, + start=r['logged_time'].replace(tzinfo=timezone('Europe/Paris')), + end=r['closed_time'].replace(tzinfo=timezone('Europe/Paris')), + ) + new.save() + except Exception as e: + print("FAIL to migrate permanency: %s" % (repr(e))) + cur.close() def main(): # migrate_users() @@ -551,10 +573,11 @@ def main(): # migrate_typeproducts() # migrate_products() # migrate_products_to_counter() - reset_customer_amount() - migrate_invoices() - migrate_refillings() - migrate_sellings() + # reset_customer_amount() + # migrate_invoices() + # migrate_refillings() + # migrate_sellings() + # migrate_permanencies() reset_index('core', 'counter') if __name__ == "__main__":