diff --git a/club/migrations/0002_club_home.py b/club/migrations/0002_club_home.py new file mode 100644 index 00000000..02e90eaa --- /dev/null +++ b/club/migrations/0002_club_home.py @@ -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'), + ), + ] diff --git a/club/models.py b/club/models.py index 9997b57e..a96612c1 100644 --- a/club/models.py +++ b/club/models.py @@ -3,10 +3,10 @@ from django.core import validators from django.conf import settings from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ValidationError -from django.db import IntegrityError +from django.db import IntegrityError, transaction 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 # Create your models here. @@ -35,6 +35,7 @@ class Club(models.Model): default=settings.SITH_GROUPS['root']['id']) edit_groups = models.ManyToManyField(Group, related_name="editable_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): """Raise a validation error when a loop is found within the parent list""" @@ -49,11 +50,45 @@ class Club(models.Model): def clean(self): self.check_loop() - def save(self): - if self.id is None: - MetaGroup(name=self.unix_name+settings.SITH_BOARD_SUFFIX).save() - MetaGroup(name=self.unix_name+settings.SITH_MEMBER_SUFFIX).save() - super(Club, self).save() + def _change_unixname(self, new_name): + c = Club.objects.filter(unix_name=new_name).first() + if c is None: + if self.home: + 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): return self.name diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index bf1c6cb0..bccdc451 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -8,7 +8,7 @@ from django.conf import settings 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 club.models import Club, Membership from subscription.models import Subscription, Subscriber @@ -38,6 +38,10 @@ class Command(BaseCommand): is_superuser=True, is_staff=True) root.set_password("plop") 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'], address=settings.SITH_MAIN_CLUB['address']) main_club.save() @@ -57,6 +61,12 @@ class Command(BaseCommand): c.save() self.reset_index("counter") 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.set_lock(root) p.save() diff --git a/core/migrations/0002_user_home.py b/core/migrations/0002_user_home.py new file mode 100644 index 00000000..1aa0a866 --- /dev/null +++ b/core/migrations/0002_user_home.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', '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'), + ), + ] diff --git a/core/models.py b/core/models.py index 02aa9e09..ad414f78 100644 --- a/core/models.py +++ b/core/models.py @@ -6,6 +6,7 @@ from django.core import validators from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.conf import settings +from django.db import transaction from datetime import datetime, timedelta import unicodedata @@ -99,6 +100,7 @@ class User(AbstractBaseUser): ), ) 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() @@ -157,6 +159,32 @@ class User(AbstractBaseUser): return True 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): return { "last_name": self.last_name, diff --git a/core/templates/core/file.jinja b/core/templates/core/file.jinja index 06173739..2c2297d9 100644 --- a/core/templates/core/file.jinja +++ b/core/templates/core/file.jinja @@ -26,6 +26,12 @@
+
+ {% set home = user.home %} + {% if home %} + {% trans %}My files{% endtrans %} + {% endif %} +
{% if file %} {% trans %}View{% endtrans %} {% if can_edit(file, user) %} diff --git a/core/templates/core/file_detail.jinja b/core/templates/core/file_detail.jinja index 4a92bf15..80609977 100644 --- a/core/templates/core/file_detail.jinja +++ b/core/templates/core/file_detail.jinja @@ -39,7 +39,9 @@

{% trans %}Download{% endtrans %}

{% endif %} +{% if not file.home_of and not file.home_of_club and file.parent %}

{% trans %}Delete{% endtrans %}

+{% endif %} {% endblock %} diff --git a/core/templates/core/file_list.jinja b/core/templates/core/file_list.jinja index fddafbca..6b456ea7 100644 --- a/core/templates/core/file_list.jinja +++ b/core/templates/core/file_list.jinja @@ -2,13 +2,6 @@ {% block content %} {{ super() }} -{% if user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) %} -
- {% csrf_token %} - {{ form.as_p() }} -

-
-{% endif %} {% if file_list %}

{% trans %}File list{% endtrans %}