From 593050d9e2240ea482ba0f0eaeac0fbe1d6d709c Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 18 Jul 2016 00:47:56 +0200 Subject: [PATCH] Improve greatly the counter right management --- core/management/commands/populate.py | 64 ++++++++++++------- core/migrations/0005_auto_20160717_1459.py | 26 ++++++++ core/migrations/0006_auto_20160717_1506.py | 31 +++++++++ core/migrations/0007_user_is_superuser.py | 19 ++++++ core/models.py | 25 +++----- core/templates/core/user_base.jinja | 2 +- .../{user_prop.jinja => user_group.jinja} | 0 core/templates/core/user_tools.jinja | 15 ++++- core/urls.py | 2 +- core/views/forms.py | 13 +--- core/views/user.py | 9 ++- counter/models.py | 5 +- counter/templates/counter/counter_list.jinja | 3 +- sith/settings_sample.py | 31 +++++++-- 14 files changed, 181 insertions(+), 64 deletions(-) create mode 100644 core/migrations/0005_auto_20160717_1459.py create mode 100644 core/migrations/0006_auto_20160717_1506.py create mode 100644 core/migrations/0007_user_is_superuser.py rename core/templates/core/{user_prop.jinja => user_group.jinja} (100%) diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index dc5d3efb..9557ab6c 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -20,25 +20,38 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument('--prod', action="store_true") + def reset_index(self, *args): + sqlcmd = StringIO() + call_command("sqlsequencereset", *args, stdout=sqlcmd) + cursor = connection.cursor() + cursor.execute(sqlcmd.getvalue()) + def handle(self, *args, **options): os.environ['DJANGO_COLORS'] = 'nocolor' root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) for g in settings.SITH_GROUPS.values(): Group(id=g['id'], name=g['name']).save() - sqlcmd = StringIO() - call_command("sqlsequencereset", "core", "auth", stdout=sqlcmd) - cursor = connection.cursor() - print(sqlcmd.getvalue()) - cursor.execute(sqlcmd.getvalue()) + self.reset_index("core", "auth") root = User(username='root', last_name="", first_name="Bibou", email="ae.info@utbm.fr", date_of_birth="1942-06-12", is_superuser=True, is_staff=True) root.set_password("plop") root.save() - ae = Club(name=settings.SITH_MAIN_CLUB['name'], unix_name=settings.SITH_MAIN_CLUB['unix_name'], + main_club = Club(name=settings.SITH_MAIN_CLUB['name'], unix_name=settings.SITH_MAIN_CLUB['unix_name'], address=settings.SITH_MAIN_CLUB['address']) - ae.save() + main_club.save() + bar_club = Club(name=settings.SITH_BAR_MANAGER['name'], unix_name=settings.SITH_BAR_MANAGER['unix_name'], + address=settings.SITH_BAR_MANAGER['address']) + bar_club.save() + for b in settings.SITH_COUNTER_BARS: + g = Group(name=b[1]+" admin") + g.save() + c = Counter(id=b[0], name=b[1], club=bar_club, type='BAR') + c.save() + c.edit_groups = [g] + c.save() + self.reset_index("counter") p = Page(name='Index') p.set_lock(root) p.save() @@ -77,6 +90,16 @@ Welcome to the wiki page! subscriber.save() subscriber.view_groups=[Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first().id] subscriber.save() + # Adding user Counter admin + counter = User(username='counter', last_name="Ter", first_name="Coun", + email="counter@git.an", + date_of_birth="1942-06-12", + is_superuser=False, is_staff=False) + counter.set_password("plop") + counter.save() + counter.view_groups=[Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first().id] + counter.groups=[Group.objects.filter(name=settings.SITH_GROUPS['counter-admin']['name']).first().id] + counter.save() # Adding user Comptable comptable = User(username='comptable', last_name="Able", first_name="Compte", email="compta@git.an", @@ -155,17 +178,15 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site. # Clubs Club(name="Bibo'UT", unix_name="bibout", - address="46 de la Boustifaille", parent=ae).save() + address="46 de la Boustifaille", parent=main_club).save() guyut = Club(name="Guy'UT", unix_name="guyut", - address="42 de la Boustifaille", parent=ae) + address="42 de la Boustifaille", parent=main_club) guyut.save() Club(name="Woenzel'UT", unix_name="woenzel", address="Woenzel", parent=guyut).save() - Club(name="BdF", unix_name="bdf", - address="6 Bd Anatole France").save() - Membership(user=skia, club=ae, role=3, description="").save() + Membership(user=skia, club=main_club, role=3, description="").save() troll = Club(name="Troll Penché", unix_name="troll", - address="Terre Du Milieu", parent=ae) + address="Terre Du Milieu", parent=main_club) troll.save() # Counters @@ -174,25 +195,24 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site. p = ProductType(name="Bières bouteilles") p.save() barb = Product(name="Barbar", code="BARB", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae) + special_selling_price="1.6", club=main_club) barb.save() cble = Product(name="Chimay Bleue", code="CBLE", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae) + special_selling_price="1.6", club=main_club) cble.save() Product(name="Corsendonk", code="CORS", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae).save() + special_selling_price="1.6", club=main_club).save() Product(name="Carolus", code="CARO", product_type=p, purchase_price="1.50", selling_price="1.7", - special_selling_price="1.6", club=ae).save() - mde = Counter(name="MDE", club=ae, type="BAR") - mde.save() + special_selling_price="1.6", club=main_club).save() + mde = Counter.objects.filter(name="MDE").first() mde.products.add(barb) mde.products.add(cble) mde.save() # Accounting test values: - BankAccount(name="AE TG", club=ae).save() - BankAccount(name="Carte AE", club=ae).save() - ba = BankAccount(name="AE TI", club=ae) + BankAccount(name="AE TG", club=main_club).save() + BankAccount(name="Carte AE", club=main_club).save() + ba = BankAccount(name="AE TI", club=main_club) ba.save() ca = ClubAccount(name="Troll Penché", bank_account=ba, club=troll) ca.save() diff --git a/core/migrations/0005_auto_20160717_1459.py b/core/migrations/0005_auto_20160717_1459.py new file mode 100644 index 00000000..1a2b93ca --- /dev/null +++ b/core/migrations/0005_auto_20160717_1459.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0004_preferences'), + ] + + operations = [ + migrations.RemoveField( + model_name='user', + name='edit_groups', + ), + migrations.RemoveField( + model_name='user', + name='owner_group', + ), + migrations.RemoveField( + model_name='user', + name='view_groups', + ), + ] diff --git a/core/migrations/0006_auto_20160717_1506.py b/core/migrations/0006_auto_20160717_1506.py new file mode 100644 index 00000000..4b47011e --- /dev/null +++ b/core/migrations/0006_auto_20160717_1506.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0005_auto_20160717_1459'), + ] + + operations = [ + migrations.AlterModelOptions( + name='user', + options={}, + ), + migrations.RemoveField( + model_name='user', + name='is_superuser', + ), + migrations.RemoveField( + model_name='user', + name='user_permissions', + ), + migrations.AlterField( + model_name='user', + name='groups', + field=models.ManyToManyField(blank=True, to='core.RealGroup', related_name='users'), + ), + ] diff --git a/core/migrations/0007_user_is_superuser.py b/core/migrations/0007_user_is_superuser.py new file mode 100644 index 00000000..70c73eec --- /dev/null +++ b/core/migrations/0007_user_is_superuser.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', '0006_auto_20160717_1506'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='is_superuser', + field=models.BooleanField(help_text='Designates whether this user is a superuser. ', default=False, verbose_name='superuser'), + ), + ] diff --git a/core/models.py b/core/models.py index fba38ea1..6d27eb81 100644 --- a/core/models.py +++ b/core/models.py @@ -45,7 +45,7 @@ class RealGroup(Group): class Meta: proxy = True -class User(AbstractBaseUser, PermissionsMixin): +class User(AbstractBaseUser): """ Defines the base user class, useable in every app @@ -91,27 +91,20 @@ class User(AbstractBaseUser, PermissionsMixin): ), ) date_joined = models.DateField(_('date joined'), auto_now_add=True) - owner_group = models.ForeignKey(Group, related_name="owned_user", - default=settings.SITH_GROUPS['root']['id']) - edit_groups = models.ManyToManyField(Group, related_name="editable_user", blank=True) - view_groups = models.ManyToManyField(Group, related_name="viewable_user", blank=True) + is_superuser = models.BooleanField( + _('superuser'), + default=False, + help_text=_( + 'Designates whether this user is a superuser. ' + ), + ) + groups = models.ManyToManyField(RealGroup, related_name='users', blank=True) objects = UserManager() USERNAME_FIELD = 'username' # REQUIRED_FIELDS = ['email'] - class Meta: - verbose_name = _('user') - verbose_name_plural = _('users') - # Add permissions like this to allow automatic permission validation in is_owner&co - # model: change_prop_ - # view_ - permissions = ( - ("change_prop_user", "Can change the user's properties (groups, ...)"), - ("view_user", "Can view user's profile"), - ) - def get_absolute_url(self): """ This is needed for black magic powered UpdateView's children diff --git a/core/templates/core/user_base.jinja b/core/templates/core/user_base.jinja index 1c4dad76..5e373dfc 100644 --- a/core/templates/core/user_base.jinja +++ b/core/templates/core/user_base.jinja @@ -8,7 +8,7 @@ Edit {% endif %} {% if can_edit_prop(profile, request.user) %} - Props + Groups {% endif %} {% if (profile == request.user or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) diff --git a/core/templates/core/user_prop.jinja b/core/templates/core/user_group.jinja similarity index 100% rename from core/templates/core/user_prop.jinja rename to core/templates/core/user_group.jinja diff --git a/core/templates/core/user_tools.jinja b/core/templates/core/user_tools.jinja index ec9e6312..faddfad7 100644 --- a/core/templates/core/user_tools.jinja +++ b/core/templates/core/user_tools.jinja @@ -12,14 +12,27 @@ {% if user.is_in_group(settings.SITH_GROUPS['root']['name']) %}
  • Groups
  • {% endif %} +{% if user.is_in_group(settings.SITH_GROUPS['counter-admin']['name']) %} +
  • Counters management
  • +{% endif %} {% if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) %}
  • Accounting
  • {% endif %} {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_in_group(settings.SITH_GROUPS['root']['name']) %}
  • Subscriptions
  • -
  • Counters management
  • {% endif %} + +

    Counters

    +
      +{% for b in settings.SITH_COUNTER_BARS %} + {% if user.is_in_group(b[1]+" admin") %} +
    • {{ b[1] }} - + Edit
    • + {% endif %} +{% endfor %} +
    +

    Clubs

      {% for m in user.membership.filter(end_date=None).all() %} diff --git a/core/urls.py b/core/urls.py index 647613a2..3510443a 100644 --- a/core/urls.py +++ b/core/urls.py @@ -27,7 +27,7 @@ urlpatterns = [ url(r'^user/$', UserListView.as_view(), name='user_list'), url(r'^user/(?P[0-9]+)/$', UserView.as_view(), name='user_profile'), url(r'^user/(?P[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'), - url(r'^user/(?P[0-9]+)/prop$', UserUpdatePropView.as_view(), name='user_prop'), + 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'), diff --git a/core/views/forms.py b/core/views/forms.py index 2f0ab6d8..92666af9 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -11,7 +11,7 @@ class RegisteringForm(UserCreationForm): required_css_class = 'required' class Meta: model = User - fields = ('first_name', 'last_name', 'email', 'date_of_birth') + fields = ('first_name', 'last_name', 'email') def save(self, commit=True): user = super(RegisteringForm, self).save(commit=False) @@ -27,21 +27,12 @@ class UserPropForm(forms.ModelForm): required_css_class = 'required' class Meta: model = User - fields = ['groups', 'edit_groups', 'view_groups'] - labels = { - 'edit_groups': "Edit profile group", - 'view_groups': "View profile group", - } + fields = ['groups'] help_texts = { - 'edit_groups': "Groups that can edit this user's profile", - 'view_groups': "Groups that can view this user's profile", 'groups': "Which groups this user belongs to", } widgets = { 'groups': CheckboxSelectMultiple, - 'user_permissions': CheckboxSelectMultiple, - 'edit_groups': CheckboxSelectMultiple, - 'view_groups': CheckboxSelectMultiple, } class PagePropForm(forms.ModelForm): diff --git a/core/views/user.py b/core/views/user.py index 5df7d101..17646c7b 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -5,6 +5,8 @@ from django.core.urlresolvers import reverse from django.core.exceptions import PermissionDenied, ObjectDoesNotExist from django.views.generic.edit import UpdateView from django.views.generic import ListView, DetailView, TemplateView +from django.forms.models import modelform_factory +from django.forms import CheckboxSelectMultiple import logging from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin @@ -114,14 +116,15 @@ class UserUpdateProfileView(CanEditMixin, UpdateView): template_name = "core/user_edit.jinja" fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', ) -class UserUpdatePropView(CanEditPropMixin, UpdateView): +class UserUpdateGroupView(CanEditPropMixin, UpdateView): """ Edit a user's groups """ model = User pk_url_kwarg = "user_id" - template_name = "core/user_prop.jinja" - form_class = UserPropForm + template_name = "core/user_group.jinja" + form_class = modelform_factory(User, fields=['groups'], + widgets={'groups':CheckboxSelectMultiple}) context_object_name = "profile" class UserToolsView(TemplateView): diff --git a/counter/models.py b/counter/models.py index c18732d5..12913a91 100644 --- a/counter/models.py +++ b/counter/models.py @@ -85,7 +85,7 @@ class Counter(models.Model): def __getattribute__(self, name): if name == "owner_group": - return Group(name=self.club.unix_name+settings.SITH_BOARD_SUFFIX) + return Group.objects.filter(name=self.club.unix_name+settings.SITH_BOARD_SUFFIX).first() return object.__getattribute__(self, name) def __str__(self): @@ -94,6 +94,9 @@ class Counter(models.Model): def get_absolute_url(self): return reverse('counter:details', kwargs={'counter_id': self.id}) + def can_be_edited_by(self, user): + return user.is_in_group(settings.SITH_GROUPS['counter-admin']['name']) + def can_be_viewed_by(self, user): return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) diff --git a/counter/templates/counter/counter_list.jinja b/counter/templates/counter/counter_list.jinja index 6457336f..422038c0 100644 --- a/counter/templates/counter/counter_list.jinja +++ b/counter/templates/counter/counter_list.jinja @@ -11,8 +11,7 @@ {% else %} diff --git a/sith/settings_sample.py b/sith/settings_sample.py index ef15da46..a82b9601 100644 --- a/sith/settings_sample.py +++ b/sith/settings_sample.py @@ -180,21 +180,34 @@ SITH_MAIN_CLUB = { 'unix_name': "ae", 'address': "6 Boulevard Anatole France, 90000 Belfort" } + +# Bar managers +SITH_BAR_MANAGER = { + 'name': "BdF", + 'unix_name': "bdf", + 'address': "6 Boulevard Anatole France, 90000 Belfort" + } + # Define the date in the year serving as reference for the subscriptions calendar # (month, day) SITH_START_DATE = (8, 15) # 15th August + SITH_GROUPS = { 'root': { 'id': 1, - 'name': "root", - }, - 'accounting-admin': { - 'id': 2, - 'name': "accounting-admin", + 'name': "Root", }, 'public': { + 'id': 2, + 'name': "Not registered users", + }, + 'accounting-admin': { 'id': 3, - 'name': "not-registered-users", + 'name': "Accounting admin", + }, + 'counter-admin': { + 'id': 4, + 'name': "Counter admin", }, } @@ -217,6 +230,12 @@ SITH_SUBSCRIPTION_PAYMENT_METHOD = [ ('other', 'Autre'), ] +SITH_COUNTER_BARS = [ + (1, "Foyer"), + (2, "MDE"), + (3, "La Gommette"), + ] + SITH_COUNTER_PAYMENT_METHOD = [ ('cheque', 'Chèque'), ('cash', 'Espèce'),