Add home for users and clubs

This commit is contained in:
Skia 2016-08-10 16:23:12 +02:00
parent 830c15a585
commit a8858fa781
11 changed files with 132 additions and 37 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0002_user_home'),
('club', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='club',
name='home',
field=models.OneToOneField(blank=True, null=True, related_name='home_of_club', verbose_name='home', to='core.SithFile'),
),
]

View File

@ -3,10 +3,10 @@ from django.core import validators
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import IntegrityError from django.db import IntegrityError, transaction
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from core.models import User, MetaGroup, Group from core.models import User, MetaGroup, Group, SithFile
from subscription.models import Subscriber from subscription.models import Subscriber
# Create your models here. # Create your models here.
@ -35,6 +35,7 @@ class Club(models.Model):
default=settings.SITH_GROUPS['root']['id']) default=settings.SITH_GROUPS['root']['id'])
edit_groups = models.ManyToManyField(Group, related_name="editable_club", blank=True) edit_groups = models.ManyToManyField(Group, related_name="editable_club", blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_club", blank=True) view_groups = models.ManyToManyField(Group, related_name="viewable_club", blank=True)
home = models.OneToOneField(SithFile, related_name='home_of_club', verbose_name=_("home"), null=True, blank=True)
def check_loop(self): def check_loop(self):
"""Raise a validation error when a loop is found within the parent list""" """Raise a validation error when a loop is found within the parent list"""
@ -49,11 +50,45 @@ class Club(models.Model):
def clean(self): def clean(self):
self.check_loop() self.check_loop()
def save(self): def _change_unixname(self, new_name):
if self.id is None: c = Club.objects.filter(unix_name=new_name).first()
MetaGroup(name=self.unix_name+settings.SITH_BOARD_SUFFIX).save() if c is None:
MetaGroup(name=self.unix_name+settings.SITH_MEMBER_SUFFIX).save() if self.home:
super(Club, self).save() self.home.name = new_name
self.home.save()
else:
raise ValidationError(_("A club with that unix_name already exists"))
def make_home(self):
if not self.home:
home_root = SithFile.objects.filter(parent=None, name="clubs").first()
root = User.objects.filter(username="root").first()
if home_root and root:
home = SithFile(parent=home_root, name=self.unix_name, owner=root)
home.save()
self.home = home
self.save()
def save(self, *args, **kwargs):
with transaction.atomic():
creation = False
if self.id is None:
creation = True
else:
old = Club.objects.filter(id=self.id).first()
if old.unix_name != self.unix_name:
self._change_unixname(self.unix_name)
super(Club, self).save(*args, **kwargs)
if creation:
board = MetaGroup(name=self.unix_name+settings.SITH_BOARD_SUFFIX)
board.save()
member = MetaGroup(name=self.unix_name+settings.SITH_MEMBER_SUFFIX)
member.save()
subscribers = Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first()
self.make_home()
self.home.edit_groups = [board]
self.home.view_groups = [member, subscribers]
self.home.save()
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -8,7 +8,7 @@ from django.conf import settings
from django.db import connection from django.db import connection
from core.models import Group, User, Page, PageRev from core.models import Group, User, Page, PageRev, SithFile
from accounting.models import GeneralJournal, BankAccount, ClubAccount, Operation, AccountingType, Company from accounting.models import GeneralJournal, BankAccount, ClubAccount, Operation, AccountingType, Company
from club.models import Club, Membership from club.models import Club, Membership
from subscription.models import Subscription, Subscriber from subscription.models import Subscription, Subscriber
@ -38,6 +38,10 @@ class Command(BaseCommand):
is_superuser=True, is_staff=True) is_superuser=True, is_staff=True)
root.set_password("plop") root.set_password("plop")
root.save() root.save()
home_root = SithFile(parent=None, name="users", is_folder=True, owner=root)
home_root.save()
club_root = SithFile(parent=None, name="clubs", is_folder=True, owner=root)
club_root.save()
main_club = 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'])
main_club.save() main_club.save()
@ -57,6 +61,12 @@ class Command(BaseCommand):
c.save() c.save()
self.reset_index("counter") self.reset_index("counter")
Counter(name="Eboutic", club=main_club, type='EBOUTIC').save() Counter(name="Eboutic", club=main_club, type='EBOUTIC').save()
home_root.view_groups = [Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first()]
club_root.view_groups = [Group.objects.filter(name=settings.SITH_MAIN_MEMBERS_GROUP).first()]
home_root.save()
club_root.save()
p = Page(name='Index') p = Page(name='Index')
p.set_lock(root) p.set_lock(root)
p.save() p.save()

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', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='user',
name='home',
field=models.OneToOneField(null=True, verbose_name='home', to='core.SithFile', blank=True, related_name='home_of'),
),
]

View File

@ -6,6 +6,7 @@ from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.conf import settings from django.conf import settings
from django.db import transaction
from datetime import datetime, timedelta from datetime import datetime, timedelta
import unicodedata import unicodedata
@ -99,6 +100,7 @@ class User(AbstractBaseUser):
), ),
) )
groups = models.ManyToManyField(RealGroup, related_name='users', blank=True) groups = models.ManyToManyField(RealGroup, related_name='users', blank=True)
home = models.OneToOneField('SithFile', related_name='home_of', verbose_name=_("home"), null=True, blank=True)
objects = UserManager() objects = UserManager()
@ -157,6 +159,32 @@ class User(AbstractBaseUser):
return True return True
return self.groups.filter(name=group_name).exists() return self.groups.filter(name=group_name).exists()
def save(self, *args, **kwargs):
with transaction.atomic():
if self.id:
old = User.objects.filter(id=self.id).first()
if old.username != self.username:
self._change_username(self.username)
super(User, self).save(*args, **kwargs)
def make_home(self):
if self.home is None:
home_root = SithFile.objects.filter(parent=None, name="users").first()
if home_root is not None:
home = SithFile(parent=home_root, name=self.username, owner=self)
home.save()
self.home = home
self.save()
def _change_username(self, new_name):
u = User.objects.filter(username=new_name).first()
if u is None:
if self.home:
self.home.name = new_name
self.home.save()
else:
raise ValidationError(_("A user with that username already exists"))
def get_profile(self): def get_profile(self):
return { return {
"last_name": self.last_name, "last_name": self.last_name,

View File

@ -26,6 +26,12 @@
<div class="tool-bar"> <div class="tool-bar">
<div class="tools"> <div class="tools">
<div>
{% set home = user.home %}
{% if home %}
<a href="{{ url('core:file_detail', home.id, popup) }}">{% trans %}My files{% endtrans %}</a>
{% endif %}
</div>
{% if file %} {% if file %}
<a href="{{ url('core:file_detail', file.id, popup) }}">{% trans %}View{% endtrans %}</a> <a href="{{ url('core:file_detail', file.id, popup) }}">{% trans %}View{% endtrans %}</a>
{% if can_edit(file, user) %} {% if can_edit(file, user) %}

View File

@ -39,7 +39,9 @@
<p><a href="{{ url('core:download', file_id=file.id) }}">{% trans %}Download{% endtrans %}</a></p> <p><a href="{{ url('core:download', file_id=file.id) }}">{% trans %}Download{% endtrans %}</a></p>
{% endif %} {% endif %}
{% if not file.home_of and not file.home_of_club and file.parent %}
<p><a href="{{ url('core:file_delete', file_id=file.id, popup=popup) }}">{% trans %}Delete{% endtrans %}</a></p> <p><a href="{{ url('core:file_delete', file_id=file.id, popup=popup) }}">{% trans %}Delete{% endtrans %}</a></p>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -2,13 +2,6 @@
{% block content %} {% block content %}
{{ super() }} {{ super() }}
{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Add{% endtrans %}"></p>
</form>
{% endif %}
{% if file_list %} {% if file_list %}
<h3>{% trans %}File list{% endtrans %}</h3> <h3>{% trans %}File list{% endtrans %}</h3>
<ul> <ul>

View File

@ -62,27 +62,9 @@ class AddFileForm(forms.Form):
self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") % self.add_error(None, _("Error uploading file %(file_name)s: %(msg)s") %
{'file_name': f, 'msg': str(e.message)}) {'file_name': f, 'msg': str(e.message)})
class FileListView(ListView, FormMixin): class FileListView(ListView):
template_name = 'core/file_list.jinja' template_name = 'core/file_list.jinja'
context_object_name = "file_list" context_object_name = "file_list"
form_class = AddFileForm
def get(self, request, *args, **kwargs):
self.form = self.get_form()
return super(FileListView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
self.form = self.get_form()
files = request.FILES.getlist('file_field')
if request.user.is_authenticated() and request.user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) and self.form.is_valid():
self.form.process(parent=None, owner=request.user, files=files)
if self.form.is_valid():
return super(FileListView, self).form_valid(self.form)
return self.form_invalid(self.form)
def get_success_url(self):
return reverse('core:file_list', kwargs={'popup': self.kwargs['popup'] or ""})
def get_queryset(self): def get_queryset(self):
return SithFile.objects.filter(parent=None) return SithFile.objects.filter(parent=None)
@ -90,7 +72,6 @@ class FileListView(ListView, FormMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs = super(FileListView, self).get_context_data(**kwargs) kwargs = super(FileListView, self).get_context_data(**kwargs)
kwargs['popup'] = "" kwargs['popup'] = ""
kwargs['form'] = self.form
if self.kwargs['popup']: if self.kwargs['popup']:
kwargs['popup'] = 'popup' kwargs['popup'] = 'popup'
return kwargs return kwargs

View File

@ -241,8 +241,8 @@ SITH_GROUPS = {
}, },
} }
SITH_BOARD_SUFFIX="-board" SITH_BOARD_SUFFIX="-bureau"
SITH_MEMBER_SUFFIX="-members" SITH_MEMBER_SUFFIX="-membres"
SITH_MAIN_BOARD_GROUP=SITH_MAIN_CLUB['unix_name']+SITH_BOARD_SUFFIX SITH_MAIN_BOARD_GROUP=SITH_MAIN_CLUB['unix_name']+SITH_BOARD_SUFFIX
SITH_MAIN_MEMBERS_GROUP=SITH_MAIN_CLUB['unix_name']+SITH_MEMBER_SUFFIX SITH_MAIN_MEMBERS_GROUP=SITH_MAIN_CLUB['unix_name']+SITH_MEMBER_SUFFIX

View File

@ -54,6 +54,7 @@ class Subscription(models.Model):
from counter.models import Customer from counter.models import Customer
if not Customer.objects.filter(user=self.member).exists(): if not Customer.objects.filter(user=self.member).exists():
Customer(user=self.member, account_id=Customer.generate_account_id(self.id), amount=0).save() Customer(user=self.member, account_id=Customer.generate_account_id(self.id), amount=0).save()
self.member.make_home()
def get_absolute_url(self): def get_absolute_url(self):
return reverse('core:user_profile', kwargs={'user_id': self.member.pk}) return reverse('core:user_profile', kwargs={'user_id': self.member.pk})