Document index displayed as a tree, not list.
[redakcja.git] / apps / explorer / models.py
1 # -*- encoding: utf-8 -*-
2 import os
3
4 from django.db import models
5 from django.contrib.auth.models import User
6 from django.conf import settings
7 from django.utils.translation import gettext_lazy as _
8
9 import toolbar.models
10
11 from explorer import fields
12
13 class EditorSettings(models.Model):
14     """Ustawienia edytora dla użytkownika.
15     
16     Pole settings zawiera obiekt JSON o  kluczach:
17      - panels - lista otwartych paneli i ich proporcje
18      - recentFiles - lista otwartych plików i ustawienia dla nich
19     
20     Przykład:
21     {
22         'panels': [
23             {'name': 'htmleditor', 'ratio': 0.5},
24             {'name': 'gallery', 'ratio': 0.5}
25         ],
26         'recentFiles': [
27             {
28                 'fileId': 'mickiewicz_pan_tadeusz.xml',
29                 'panels': [
30                     {'name': 'htmleditor', 'ratio': 0.4},
31                     {'name': 'gallery', 'ratio': 0.6}
32                 ]
33             }
34         ]
35     }
36     """
37     user = models.ForeignKey(User, unique=True)
38     settings = fields.JSONField()
39     
40     class Meta:
41         verbose_name, verbose_name_plural = _("editor settings"), _("editor settings")
42
43     def __unicode__(self):
44         return u"Editor settings for %s" % self.user.username
45
46 class EditorPanel(models.Model):
47     id = models.CharField(max_length=24, primary_key=True)
48     display_name = models.CharField(max_length=128)
49
50     toolbar_groups = models.ManyToManyField(toolbar.models.ButtonGroup, blank=True)
51     toolbar_extra = models.ForeignKey(toolbar.models.ButtonGroup, null=True, blank=True,
52         unique=True, related_name='main_editor_panels')
53
54     def __unicode__(self):
55         return self.display_name
56     
57 class Book(models.Model):
58     class Meta:
59         permissions = (            
60             ("can_share", "Can share documents without pull requests."),
61         )
62         abstract=True
63     pass
64
65 class PullRequest(models.Model):    
66     REQUEST_STATUSES = (
67         ("N", "Pending for resolution"),
68         ("R", "Rejected"),
69         ("A", "Accepted & merged"),
70     )
71
72     comitter = models.ForeignKey(User) # the user who request the pull
73     comment = models.TextField() # addtional comments to the request
74
75     # document to merge
76     document = models.CharField(max_length=255)
77
78     # revision to be merged into the main branch
79     source_revision = models.CharField(max_length=40, unique=True)
80
81     # current status
82     status = models.CharField(max_length=1, choices=REQUEST_STATUSES)
83
84     # comment to the status change of request (if applicable)
85     response_comment = models.TextField(blank=True)
86
87     # revision number in which the changes were merged (if any)
88     merged_rev = models.CharField(max_length=40, blank=True, null=True)
89
90
91     def __unicode__(self):
92         return unicode(self.comitter) + u':' + self.document
93     
94 def get_image_folders():
95     return sorted(fn for fn in os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR)) if not fn.startswith('.'))
96
97
98 def get_images_from_folder(folder):
99     return sorted(settings.MEDIA_URL + settings.IMAGE_DIR + u'/' + folder + u'/' + fn.decode('utf-8') for fn
100             in os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, folder))
101             if not fn.decode('utf-8').startswith('.'))
102