Improve greatly the counter right management

This commit is contained in:
Skia 2016-07-18 00:47:56 +02:00
parent e92a73dfb1
commit 593050d9e2
14 changed files with 181 additions and 64 deletions

View File

@ -20,25 +20,38 @@ class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('--prod', action="store_true") 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): def handle(self, *args, **options):
os.environ['DJANGO_COLORS'] = 'nocolor' os.environ['DJANGO_COLORS'] = 'nocolor'
root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
for g in settings.SITH_GROUPS.values(): for g in settings.SITH_GROUPS.values():
Group(id=g['id'], name=g['name']).save() Group(id=g['id'], name=g['name']).save()
sqlcmd = StringIO() self.reset_index("core", "auth")
call_command("sqlsequencereset", "core", "auth", stdout=sqlcmd)
cursor = connection.cursor()
print(sqlcmd.getvalue())
cursor.execute(sqlcmd.getvalue())
root = User(username='root', last_name="", first_name="Bibou", root = User(username='root', last_name="", first_name="Bibou",
email="ae.info@utbm.fr", email="ae.info@utbm.fr",
date_of_birth="1942-06-12", date_of_birth="1942-06-12",
is_superuser=True, is_staff=True) is_superuser=True, is_staff=True)
root.set_password("plop") root.set_password("plop")
root.save() 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']) 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 = Page(name='Index')
p.set_lock(root) p.set_lock(root)
p.save() p.save()
@ -77,6 +90,16 @@ Welcome to the wiki page!
subscriber.save() subscriber.save()
subscriber.view_groups=[Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first().id] subscriber.view_groups=[Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first().id]
subscriber.save() 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 # Adding user Comptable
comptable = User(username='comptable', last_name="Able", first_name="Compte", comptable = User(username='comptable', last_name="Able", first_name="Compte",
email="compta@git.an", email="compta@git.an",
@ -155,17 +178,15 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site.
# Clubs # Clubs
Club(name="Bibo'UT", unix_name="bibout", 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", 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() guyut.save()
Club(name="Woenzel'UT", unix_name="woenzel", Club(name="Woenzel'UT", unix_name="woenzel",
address="Woenzel", parent=guyut).save() address="Woenzel", parent=guyut).save()
Club(name="BdF", unix_name="bdf", Membership(user=skia, club=main_club, role=3, description="").save()
address="6 Bd Anatole France").save()
Membership(user=skia, club=ae, role=3, description="").save()
troll = Club(name="Troll Penché", unix_name="troll", troll = Club(name="Troll Penché", unix_name="troll",
address="Terre Du Milieu", parent=ae) address="Terre Du Milieu", parent=main_club)
troll.save() troll.save()
# Counters # Counters
@ -174,25 +195,24 @@ Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site.
p = ProductType(name="Bières bouteilles") p = ProductType(name="Bières bouteilles")
p.save() p.save()
barb = Product(name="Barbar", code="BARB", product_type=p, purchase_price="1.50", selling_price="1.7", 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() barb.save()
cble = Product(name="Chimay Bleue", code="CBLE", product_type=p, purchase_price="1.50", selling_price="1.7", 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() cble.save()
Product(name="Corsendonk", code="CORS", product_type=p, purchase_price="1.50", selling_price="1.7", 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", Product(name="Carolus", code="CARO", 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()
mde = Counter(name="MDE", club=ae, type="BAR") mde = Counter.objects.filter(name="MDE").first()
mde.save()
mde.products.add(barb) mde.products.add(barb)
mde.products.add(cble) mde.products.add(cble)
mde.save() mde.save()
# Accounting test values: # Accounting test values:
BankAccount(name="AE TG", club=ae).save() BankAccount(name="AE TG", club=main_club).save()
BankAccount(name="Carte AE", club=ae).save() BankAccount(name="Carte AE", club=main_club).save()
ba = BankAccount(name="AE TI", club=ae) ba = BankAccount(name="AE TI", club=main_club)
ba.save() ba.save()
ca = ClubAccount(name="Troll Penché", bank_account=ba, club=troll) ca = ClubAccount(name="Troll Penché", bank_account=ba, club=troll)
ca.save() ca.save()

View File

@ -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',
),
]

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

@ -45,7 +45,7 @@ class RealGroup(Group):
class Meta: class Meta:
proxy = True proxy = True
class User(AbstractBaseUser, PermissionsMixin): class User(AbstractBaseUser):
""" """
Defines the base user class, useable in every app 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) date_joined = models.DateField(_('date joined'), auto_now_add=True)
owner_group = models.ForeignKey(Group, related_name="owned_user", is_superuser = models.BooleanField(
default=settings.SITH_GROUPS['root']['id']) _('superuser'),
edit_groups = models.ManyToManyField(Group, related_name="editable_user", blank=True) default=False,
view_groups = models.ManyToManyField(Group, related_name="viewable_user", blank=True) help_text=_(
'Designates whether this user is a superuser. '
),
)
groups = models.ManyToManyField(RealGroup, related_name='users', blank=True)
objects = UserManager() objects = UserManager()
USERNAME_FIELD = 'username' USERNAME_FIELD = 'username'
# REQUIRED_FIELDS = ['email'] # 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_<class_name>
# view_<class_name>
permissions = (
("change_prop_user", "Can change the user's properties (groups, ...)"),
("view_user", "Can view user's profile"),
)
def get_absolute_url(self): def get_absolute_url(self):
""" """
This is needed for black magic powered UpdateView's children This is needed for black magic powered UpdateView's children

View File

@ -8,7 +8,7 @@
<a href="{{ url('core:user_edit', user_id=profile.id) }}">Edit</a> <a href="{{ url('core:user_edit', user_id=profile.id) }}">Edit</a>
{% endif %} {% endif %}
{% if can_edit_prop(profile, request.user) %} {% if can_edit_prop(profile, request.user) %}
<a href="{{ url('core:user_prop', user_id=profile.id) }}">Props</a> <a href="{{ url('core:user_groups', user_id=profile.id) }}">Groups</a>
{% endif %} {% endif %}
{% if (profile == request.user {% if (profile == request.user
or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name'])

View File

@ -12,14 +12,27 @@
{% if user.is_in_group(settings.SITH_GROUPS['root']['name']) %} {% if user.is_in_group(settings.SITH_GROUPS['root']['name']) %}
<li><a href="{{ url('core:group_list') }}">Groups</a></li> <li><a href="{{ url('core:group_list') }}">Groups</a></li>
{% endif %} {% endif %}
{% if user.is_in_group(settings.SITH_GROUPS['counter-admin']['name']) %}
<li><a href="{{ url('counter:admin_list') }}">Counters management</a></li>
{% endif %}
{% if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) %} {% if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']) %}
<li><a href="{{ url('accounting:bank_list') }}">Accounting</a></li> <li><a href="{{ url('accounting:bank_list') }}">Accounting</a></li>
{% endif %} {% endif %}
{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_in_group(settings.SITH_GROUPS['root']['name']) %} {% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_in_group(settings.SITH_GROUPS['root']['name']) %}
<li><a href="{{ url('subscription:subscription') }}">Subscriptions</a></li> <li><a href="{{ url('subscription:subscription') }}">Subscriptions</a></li>
<li><a href="{{ url('counter:admin_list') }}">Counters management</a></li>
{% endif %} {% endif %}
</ul> </ul>
<h4>Counters</h4>
<ul>
{% for b in settings.SITH_COUNTER_BARS %}
{% if user.is_in_group(b[1]+" admin") %}
<li><a href="{{ url('counter:details', counter_id=b[0]) }}">{{ b[1] }}</a> -
<a href="{{ url('counter:admin', counter_id=b[0]) }}">Edit</a></li>
{% endif %}
{% endfor %}
</ul>
<h4>Clubs</h4> <h4>Clubs</h4>
<ul> <ul>
{% for m in user.membership.filter(end_date=None).all() %} {% for m in user.membership.filter(end_date=None).all() %}

View File

@ -27,7 +27,7 @@ urlpatterns = [
url(r'^user/$', UserListView.as_view(), name='user_list'), url(r'^user/$', UserListView.as_view(), name='user_list'),
url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'), url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'),
url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'), url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'),
url(r'^user/(?P<user_id>[0-9]+)/prop$', UserUpdatePropView.as_view(), name='user_prop'), url(r'^user/(?P<user_id>[0-9]+)/groups$', UserUpdateGroupView.as_view(), name='user_groups'),
url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'), url(r'^user/tools/$', UserToolsView.as_view(), name='user_tools'),
url(r'^user/(?P<user_id>[0-9]+)/account$', UserAccountView.as_view(), name='user_account'), url(r'^user/(?P<user_id>[0-9]+)/account$', UserAccountView.as_view(), name='user_account'),

View File

@ -11,7 +11,7 @@ class RegisteringForm(UserCreationForm):
required_css_class = 'required' required_css_class = 'required'
class Meta: class Meta:
model = User model = User
fields = ('first_name', 'last_name', 'email', 'date_of_birth') fields = ('first_name', 'last_name', 'email')
def save(self, commit=True): def save(self, commit=True):
user = super(RegisteringForm, self).save(commit=False) user = super(RegisteringForm, self).save(commit=False)
@ -27,21 +27,12 @@ class UserPropForm(forms.ModelForm):
required_css_class = 'required' required_css_class = 'required'
class Meta: class Meta:
model = User model = User
fields = ['groups', 'edit_groups', 'view_groups'] fields = ['groups']
labels = {
'edit_groups': "Edit profile group",
'view_groups': "View profile group",
}
help_texts = { 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", 'groups': "Which groups this user belongs to",
} }
widgets = { widgets = {
'groups': CheckboxSelectMultiple, 'groups': CheckboxSelectMultiple,
'user_permissions': CheckboxSelectMultiple,
'edit_groups': CheckboxSelectMultiple,
'view_groups': CheckboxSelectMultiple,
} }
class PagePropForm(forms.ModelForm): class PagePropForm(forms.ModelForm):

View File

@ -5,6 +5,8 @@ from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.views.generic.edit import UpdateView from django.views.generic.edit import UpdateView
from django.views.generic import ListView, DetailView, TemplateView from django.views.generic import ListView, DetailView, TemplateView
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
import logging import logging
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
@ -114,14 +116,15 @@ class UserUpdateProfileView(CanEditMixin, UpdateView):
template_name = "core/user_edit.jinja" template_name = "core/user_edit.jinja"
fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', ) fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', )
class UserUpdatePropView(CanEditPropMixin, UpdateView): class UserUpdateGroupView(CanEditPropMixin, UpdateView):
""" """
Edit a user's groups Edit a user's groups
""" """
model = User model = User
pk_url_kwarg = "user_id" pk_url_kwarg = "user_id"
template_name = "core/user_prop.jinja" template_name = "core/user_group.jinja"
form_class = UserPropForm form_class = modelform_factory(User, fields=['groups'],
widgets={'groups':CheckboxSelectMultiple})
context_object_name = "profile" context_object_name = "profile"
class UserToolsView(TemplateView): class UserToolsView(TemplateView):

View File

@ -85,7 +85,7 @@ class Counter(models.Model):
def __getattribute__(self, name): def __getattribute__(self, name):
if name == "owner_group": 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) return object.__getattribute__(self, name)
def __str__(self): def __str__(self):
@ -94,6 +94,9 @@ class Counter(models.Model):
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 can_be_edited_by(self, user):
return user.is_in_group(settings.SITH_GROUPS['counter-admin']['name'])
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP)

View File

@ -11,8 +11,7 @@
<ul> <ul>
{% for c in counter_list %} {% for c in counter_list %}
<li><a href="{{ url('counter:details', counter_id=c.id) }}">{{ c }}</a> - <li><a href="{{ url('counter:details', counter_id=c.id) }}">{{ c }}</a> -
<a href="{{ url('counter:admin', counter_id=c.id) }}">Edit</a> - <a href="{{ url('counter:admin', counter_id=c.id) }}">Edit</a></li>
<a href="{{ url('counter:delete', counter_id=c.id) }}">Delete</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}

View File

@ -180,21 +180,34 @@ SITH_MAIN_CLUB = {
'unix_name': "ae", 'unix_name': "ae",
'address': "6 Boulevard Anatole France, 90000 Belfort" '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 # Define the date in the year serving as reference for the subscriptions calendar
# (month, day) # (month, day)
SITH_START_DATE = (8, 15) # 15th August SITH_START_DATE = (8, 15) # 15th August
SITH_GROUPS = { SITH_GROUPS = {
'root': { 'root': {
'id': 1, 'id': 1,
'name': "root", 'name': "Root",
},
'accounting-admin': {
'id': 2,
'name': "accounting-admin",
}, },
'public': { 'public': {
'id': 2,
'name': "Not registered users",
},
'accounting-admin': {
'id': 3, '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'), ('other', 'Autre'),
] ]
SITH_COUNTER_BARS = [
(1, "Foyer"),
(2, "MDE"),
(3, "La Gommette"),
]
SITH_COUNTER_PAYMENT_METHOD = [ SITH_COUNTER_PAYMENT_METHOD = [
('cheque', 'Chèque'), ('cheque', 'Chèque'),
('cash', 'Espèce'), ('cash', 'Espèce'),