Completed
Push — master ( 63b44b...a884de )
by Batiste
10s
created

media_filename()   A

Complexity

Conditions 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 2
1
"""Django page CMS ``models``."""
2
3
from pages.cache import cache
4
from pages.utils import get_placeholders, normalize_url, get_now
5
from pages.managers import PageManager, ContentManager
6
from pages.managers import PageAliasManager
7
from pages import settings
8
from pages.utils import slugify
9
# checks
10
from pages import checks
11
12
from django.db import models
13
from django.conf import settings as django_settings
14
from django.utils.translation import ugettext_lazy as _
15
from django.utils.safestring import mark_safe
16
from django.core.urlresolvers import reverse
17
from django.conf import settings as global_settings
18
from django.utils.encoding import python_2_unicode_compatible
19
20
21
from mptt.models import MPTTModel
22
import uuid
23
import os
24
25
PAGE_CONTENT_DICT_KEY = ContentManager.PAGE_CONTENT_DICT_KEY
26
27
if settings.PAGE_USE_SITE_ID:
28
    from django.contrib.sites.models import Site
29
30
31
@python_2_unicode_compatible
32
class Page(MPTTModel):
33
    """
34
    This model contain the status, dates, author, template.
35
    The real content of the page can be found in the
36
    :class:`Content <pages.models.Content>` model.
37
38
    .. attribute:: creation_date
39
       When the page has been created.
40
41
    .. attribute:: publication_date
42
       When the page should be visible.
43
44
    .. attribute:: publication_end_date
45
       When the publication of this page end.
46
47
    .. attribute:: last_modification_date
48
       Last time this page has been modified.
49
50
    .. attribute:: status
51
       The current status of the page. Could be DRAFT, PUBLISHED,
52
       EXPIRED or HIDDEN. You should the property :attr:`calculated_status` if
53
       you want that the dates are taken in account.
54
55
    .. attribute:: template
56
       A string containing the name of the template file for this page.
57
    """
58
59
    # some class constants to refer to, e.g. Page.DRAFT
60
    DRAFT = 0
61
    PUBLISHED = 1
62
    EXPIRED = 2
63
    HIDDEN = 3
64
    STATUSES = (
65
        (PUBLISHED, _('Published')),
66
        (HIDDEN, _('Hidden')),
67
        (DRAFT, _('Draft')),
68
    )
69
70
    PAGE_LANGUAGES_KEY = "page_%d_languages"
71
    PAGE_URL_KEY = "page_%d_url"
72
    ANCESTORS_KEY = 'ancestors_%d'
73
    CHILDREN_KEY = 'children_%d'
74
    PUB_CHILDREN_KEY = 'pub_children_%d'
75
76
    # used to identify pages across different databases
77
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)
78
79
    author = models.ForeignKey(django_settings.AUTH_USER_MODEL,
80
            verbose_name=_('author'))
81
82
    parent = models.ForeignKey('self', null=True, blank=True,
83
            related_name='children', verbose_name=_('parent'))
84
    creation_date = models.DateTimeField(_('creation date'), editable=False,
85
            default=get_now)
86
    publication_date = models.DateTimeField(_('publication date'),
87
            null=True, blank=True, help_text=_('''When the page should go
88
            live. Status must be "Published" for page to go live.'''))
89
    publication_end_date = models.DateTimeField(_('publication end date'),
90
            null=True, blank=True, help_text=_('''When to expire the page.
91
            Leave empty to never expire.'''))
92
93
    last_modification_date = models.DateTimeField(_('last modification date'))
94
95
    status = models.IntegerField(_('status'), choices=STATUSES, default=DRAFT)
96
    template = models.CharField(_('template'), max_length=100, null=True,
97
            blank=True)
98
99
    delegate_to = models.CharField(_('delegate to'), max_length=100, null=True,
100
            blank=True)
101
102
    freeze_date = models.DateTimeField(_('freeze date'),
103
            null=True, blank=True, help_text=_('''Don't publish any content
104
            after this date.'''))
105
106
    if settings.PAGE_USE_SITE_ID:
107
        sites = models.ManyToManyField(Site,
108
            default=[global_settings.SITE_ID],
109
            help_text=_('The site(s) the page is accessible at.'),
110
            verbose_name=_('sites'))
111
112
    redirect_to_url = models.CharField(max_length=200, null=True, blank=True)
113
114
    redirect_to = models.ForeignKey('self', null=True, blank=True,
115
        related_name='redirected_pages')
116
117
    # Managers
118
    objects = PageManager()
119
120
    if settings.PAGE_TAGGING:
121
        tags = settings.PAGE_TAGGING_FIELD()
122
123
    class Meta:
124
        """Make sure the default page ordering is correct."""
125
        ordering = ['tree_id', 'lft']
126
        get_latest_by = "publication_date"
127
        verbose_name = _('page')
128
        verbose_name_plural = _('pages')
129
        permissions = settings.PAGE_EXTRA_PERMISSIONS
130
131
    def __init__(self, *args, **kwargs):
132
        """Instanciate the page object."""
133
        # per instance cache
134
        self._languages = None
135
        self._content_dict = None
136
        self._is_first_root = None
137
        self._complete_slug = None
138
        super(Page, self).__init__(*args, **kwargs)
139
140
    def save(self, *args, **kwargs):
141
        """Override the default ``save`` method."""
142
        if not self.status:
143
            self.status = self.DRAFT
144
        # Published pages should always have a publication date
145
        if self.publication_date is None and self.status == self.PUBLISHED:
146
            self.publication_date = get_now()
147
        # Drafts should not, unless they have been set to the future
148
        if self.status == self.DRAFT:
149
            if settings.PAGE_SHOW_START_DATE:
150
                if (self.publication_date and
151
                        self.publication_date <= get_now()):
152
                    self.publication_date = None
153
            else:
154
                self.publication_date = None
155
        self.last_modification_date = get_now()
156
        super(Page, self).save(*args, **kwargs)
157
        # fix sites many-to-many link when the're hidden from the form
158
        if settings.PAGE_HIDE_SITES and self.sites.count() == 0:
159
            self.sites.add(Site.objects.get(pk=global_settings.SITE_ID))
160
161
    def _get_calculated_status(self):
162
        """Get the calculated status of the page based on
163
        :attr:`Page.publication_date`,
164
        :attr:`Page.publication_end_date`,
165
        and :attr:`Page.status`."""
166
        if settings.PAGE_SHOW_START_DATE and self.publication_date:
167
            if self.publication_date > get_now():
168
                return self.DRAFT
169
170
        if settings.PAGE_SHOW_END_DATE and self.publication_end_date:
171
            if self.publication_end_date < get_now():
172
                return self.EXPIRED
173
174
        return self.status
175
    calculated_status = property(_get_calculated_status)
176
177
    def _visible(self):
178
        """Return True if the page is visible on the frontend."""
179
        return self.calculated_status in (self.PUBLISHED, self.HIDDEN)
180
    visible = property(_visible)
181
182
    def get_children(self):
183
        """Cache superclass result"""
184
        key = self.CHILDREN_KEY % self.id
185
        #children = cache.get(key, None)
186
        # if children is None:
187
        children = super(Page, self).get_children()
188
        #cache.set(key, children)
189
        return children
190
191
    def published_children(self):
192
        """Return a :class:`QuerySet` of published children page"""
193
        key = self.PUB_CHILDREN_KEY % self.id
194
        #children = cache.get(key, None)
195
        # if children is None:
196
        children = Page.objects.filter_published(self.get_children()).all()
197
        #cache.set(key, children)
198
        return children
199
200
    def get_children_for_frontend(self):
201
        """Return a :class:`QuerySet` of published children page"""
202
        return self.published_children()
203
204
    def get_date_ordered_children_for_frontend(self):
205
        """Return a :class:`QuerySet` of published children page ordered
206
        by publication date."""
207
        return self.published_children().order_by('-publication_date')
208
209
    def move_to(self, target, position='first-child'):
210
        """Invalidate cache when moving"""
211
212
        # Invalidate both in case position matters,
213
        # otherwise only target is needed.
214
        self.invalidate()
215
        target.invalidate()
216
        super(Page, self).move_to(target, position=position)
217
218
    def invalidate(self):
219
        """Invalidate cached data for this page."""
220
221
        cache.delete(self.PAGE_LANGUAGES_KEY % (self.id))
222
        cache.delete('PAGE_FIRST_ROOT_ID')
223
        cache.delete(self.CHILDREN_KEY % self.id)
224
        cache.delete(self.PUB_CHILDREN_KEY % self.id)
225
        # XXX: Should this have a depth limit?
226
        if self.parent_id:
227
            self.parent.invalidate()
228
        self._languages = None
229
        self._complete_slug = None
230
        self._content_dict = dict()
231
232
        placeholders = get_placeholders(self.get_template())
233
234
        p_names = [p.ctype for p in placeholders]
235
        if 'slug' not in p_names:
236
            p_names.append('slug')
237
        if 'title' not in p_names:
238
            p_names.append('title')
239
240
        from pages.managers import fake_page
241
        shared = [p for p in placeholders if p.shared]
242
        for share in shared:
243
            print(share.ctype)
244
            fake_page.invalidate(share.ctype)
245
246
        # delete content cache, frozen or not
247
        for name in p_names:
248
            # frozen
249
            cache.delete(
250
                PAGE_CONTENT_DICT_KEY %
251
                (self.id, name, 1))
252
            # not frozen
253
            cache.delete(
254
                PAGE_CONTENT_DICT_KEY %
255
                (self.id, name, 0))
256
257
        cache.delete(self.PAGE_URL_KEY % (self.id))
258
259
    def get_languages(self):
260
        """
261
        Return a list of all used languages for this page.
262
        """
263
        if self._languages:
264
            return self._languages
265
        self._languages = cache.get(self.PAGE_LANGUAGES_KEY % (self.id))
266
        if self._languages is not None:
267
            return self._languages
268
269
        languages = [c['language'] for
270
            c in Content.objects.filter(page=self,
271
            type="slug").values('language')]
272
        # remove duplicates
273
        languages = sorted(set(languages))
274
        cache.set(self.PAGE_LANGUAGES_KEY % (self.id), languages)
275
        self._languages = languages
276
        return languages
277
278
    def is_first_root(self):
279
        """Return ``True`` if this page is the first root pages."""
280
        parent_cache_key = 'PARENT_FOR_%d' % self.id
281
        has_parent = cache.get(parent_cache_key, None)
282
        if has_parent is None:
283
            has_parent = not not self.parent
284
            cache.set(parent_cache_key, has_parent)
285
286
        if has_parent:
287
            return False
288
        if self._is_first_root is not None:
289
            return self._is_first_root
290
        first_root_id = cache.get('PAGE_FIRST_ROOT_ID')
291
        if first_root_id is not None:
292
            self._is_first_root = first_root_id == self.id
293
            return self._is_first_root
294
        try:
295
            first_root_id = Page.objects.root().values('id')[0]['id']
296
        except IndexError:
297
            first_root_id = None
298
        if first_root_id is not None:
299
            cache.set('PAGE_FIRST_ROOT_ID', first_root_id)
300
        self._is_first_root = self.id == first_root_id
301
        return self._is_first_root
302
303
    def get_template(self):
304
        """
305
        Get the :attr:`template <Page.template>` of this page if
306
        defined or the closer parent's one if defined
307
        or :attr:`pages.settings.PAGE_DEFAULT_TEMPLATE` otherwise.
308
        """
309
        if self.template:
310
            return self.template
311
312
        template = None
313
        for p in self.get_ancestors(ascending=True):
314
            if p.template:
315
                template = p.template
316
                break
317
318
        if not template:
319
            template = settings.PAGE_DEFAULT_TEMPLATE
320
321
        return template
322
323
    def get_template_name(self):
324
        """
325
        Get the template name of this page if defined or if a closer
326
        parent has a defined template or
327
        :data:`pages.settings.PAGE_DEFAULT_TEMPLATE` otherwise.
328
        """
329
        template = self.get_template()
330
        page_templates = settings.get_page_templates()
331
        for t in page_templates:
332
            if t[0] == template:
333
                return t[1]
334
        return template
335
336
    def valid_targets(self):
337
        """Return a :class:`QuerySet` of valid targets for moving a page
338
        into the tree.
339
340
        :param perms: the level of permission of the concerned user.
341
        """
342
        exclude_list = [self.id]
343
        for p in self.get_descendants():
344
            exclude_list.append(p.id)
345
        return Page.objects.exclude(id__in=exclude_list)
346
347
    # Content methods
348
349
    def get_content(self, language, ctype, language_fallback=False):
350
        """Shortcut method for retrieving a piece of page content
351
352
        :param language: wanted language, if not defined default is used.
353
        :param ctype: the type of content.
354
        :param fallback: if ``True``, the content will also be searched in \
355
        other languages.
356
        """
357
        return Content.objects.get_content(self, language, ctype,
358
            language_fallback)
359
360
    def expose_content(self):
361
        """Return all the current content of this page into a `string`.
362
363
        This is used by the haystack framework to build the search index."""
364
        placeholders = get_placeholders(self.get_template())
365
        exposed_content = []
366
        for lang in self.get_languages():
367
            for p in placeholders:
368
                content = self.get_content(lang, p.ctype, False)
369
                if content:
370
                    exposed_content.append(content)
371
        return "\r\n".join(exposed_content)
372
373
    def content_by_language(self, language):
374
        """
375
        Return a list of latest published
376
        :class:`Content <pages.models.Content>`
377
        for a particluar language.
378
379
        :param language: wanted language,
380
        """
381
        placeholders = get_placeholders(self.get_template())
382
        content_list = []
383
        for p in placeholders:
384
            try:
385
                content = Content.objects.get_content_object(self,
386
                    language, p.ctype)
387
                content_list.append(content)
388
            except Content.DoesNotExist:
389
                pass
390
        return content_list
391
392
    ### Title and slug
393
394
    def get_url_path(self, language=None):
395
        """Return the URL's path component. Add the language prefix if
396
        ``PAGE_USE_LANGUAGE_PREFIX`` setting is set to ``True``.
397
398
        :param language: the wanted url language.
399
        """
400
        if self.is_first_root():
401
            # this is used to allow users to change URL of the root
402
            # page. The language prefix is not usable here.
403
            try:
404
                return reverse('pages-root')
405
            except Exception:
406
                pass
407
        url = self.get_complete_slug(language)
408
        if not language:
409
            language = settings.PAGE_DEFAULT_LANGUAGE
410
        if settings.PAGE_USE_LANGUAGE_PREFIX:
411
            return reverse('pages-details-by-path',
412
                args=[language, url])
413
        else:
414
            return reverse('pages-details-by-path', args=[url])
415
416
    def get_absolute_url(self, language=None):
417
        """Alias for `get_url_path`.
418
419
        :param language: the wanted url language.
420
        """
421
        return self.get_url_path(language=language)
422
423
    def get_complete_slug(self, language=None, hideroot=True):
424
        """Return the complete slug of this page by concatenating
425
        all parent's slugs.
426
427
        :param language: the wanted slug language."""
428
        if not language:
429
            language = settings.PAGE_DEFAULT_LANGUAGE
430
431
        if self._complete_slug and language in self._complete_slug:
432
            return self._complete_slug[language]
433
434
        self._complete_slug = cache.get(self.PAGE_URL_KEY % (self.id))
435
        if self._complete_slug is None:
436
            self._complete_slug = {}
437
        elif language in self._complete_slug:
438
            return self._complete_slug[language]
439
440
        if hideroot and settings.PAGE_HIDE_ROOT_SLUG and self.is_first_root():
441
            url = ''
442
        else:
443
            url = '%s' % self.slug(language)
444
445
        key = self.ANCESTORS_KEY % self.id
446
        ancestors = cache.get(key, None)
447
        if ancestors is None:
448
            ancestors = self.get_ancestors(ascending=True)
449
            cache.set(key, ancestors)
450
451
        for ancestor in ancestors:
452
            url = ancestor.slug(language) + '/' + url
453
454
        self._complete_slug[language] = url
455
        cache.set(self.PAGE_URL_KEY % (self.id), self._complete_slug)
456
        return url
457
458
    def slug_with_level(self, language=None):
459
        """Display the slug of the page prepended with insecable
460
        spaces to simluate the level of page in the hierarchy."""
461
        level = ''
462
        if self.level:
463
            for n in range(0, self.level):
464
                level += '&nbsp;&nbsp;&nbsp;'
465
        return mark_safe(level + self.slug(language))
466
467
    def slug(self, language=None, fallback=True):
468
        """
469
        Return the slug of the page depending on the given language.
470
471
        :param language: wanted language, if not defined default is used.
472
        :param fallback: if ``True``, the slug will also be searched in other \
473
        languages.
474
        """
475
476
        slug = self.get_content(language, 'slug', language_fallback=fallback)
477
        if slug == '':
478
            return "Page {0}".format(self.id)
479
480
        return slug
481
482
    def title(self, language=None, fallback=True):
483
        """
484
        Return the title of the page depending on the given language.
485
486
        :param language: wanted language, if not defined default is used.
487
        :param fallback: if ``True``, the slug will also be searched in \
488
        other languages.
489
        """
490
        if not language:
491
            language = settings.PAGE_DEFAULT_LANGUAGE
492
493
        return self.get_content(language, 'title', language_fallback=fallback)
494
495
    # Formating methods
496
497
    def margin_level(self):
498
        """Used in the admin menu to create the left margin."""
499
        return self.level * 2
500
501
    def __str__(self):
502
        """Representation of the page, saved or not."""
503
        if self.id:
504
            # without ID a slug cannot be retrieved
505
            return self.slug()
506
        return "Page without id"
507
508
509
@python_2_unicode_compatible
510
class Content(models.Model):
511
    """A block of content, tied to a :class:`Page <pages.models.Page>`,
512
    for a particular language"""
513
514
    # languages could have five characters : Brazilian Portuguese is pt-br
515
    language = models.CharField(_('language'), max_length=5, blank=False)
516
    body = models.TextField(_('body'), blank=True)
517
    type = models.CharField(
518
        _('type'), max_length=100, blank=False,
519
        db_index=True)
520
    page = models.ForeignKey(Page, verbose_name=_('page'), null=True)
521
522
    creation_date = models.DateTimeField(
523
        _('creation date'), editable=False,
524
        default=get_now)
525
    objects = ContentManager()
526
527
    class Meta:
528
        get_latest_by = 'creation_date'
529
        verbose_name = _('content')
530
        verbose_name_plural = _('contents')
531
532
    def __str__(self):
533
        return u"{0} :: {1}".format(self.page.slug(), self.body[0:15])
534
535
536
@python_2_unicode_compatible
537
class PageAlias(models.Model):
538
    """URL alias for a :class:`Page <pages.models.Page>`"""
539
    page = models.ForeignKey(Page, null=True, blank=True,
540
        verbose_name=_('page'))
541
    url = models.CharField(max_length=255, unique=True)
542
    objects = PageAliasManager()
543
544
    class Meta:
545
        verbose_name_plural = _('Aliases')
546
547
    def save(self, *args, **kwargs):
548
        # normalize url
549
        self.url = normalize_url(self.url)
550
        super(PageAlias, self).save(*args, **kwargs)
551
552
    def __str__(self):
553
        return "{0} :: {1}".format(self.url, self.page.get_complete_slug())
554
555
556 View Code Duplication
def media_filename(instance, filename):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
557
    avoid_collision = uuid.uuid4().hex[:8]
558
    name_parts = filename.split('.')
559
    if len(name_parts) > 1:
560
        name = slugify('.'.join(name_parts[:-1]), allow_unicode=True)
561
        ext = slugify(name_parts[-1])
562
        name = name + '.' + ext
563
    else:
564
        name = slugify(data.name)
565
    filename = os.path.join(
566
        settings.PAGE_UPLOAD_ROOT,
567
        'medias',
568
        name
569
    )
570
    return filename
571
572
573
@python_2_unicode_compatible
574
class Media(models.Model):
575
    """Media model :class:`Media <pages.models.Media>`"""
576
    title = models.CharField(max_length=255, blank=True)
577
    description = models.TextField(blank=True)
578
    url = models.FileField(upload_to=media_filename)
579
    extension = models.CharField(max_length=32, blank=True, editable=False)
580
    creation_date = models.DateTimeField(_('creation date'), editable=False,
581
            default=get_now)
582
583
    def image(self):
584
        if self.extension in ['png', 'jpg', 'jpeg']:
585
            return u'<img width="60" src="%s" />' % os.path.join(
586
                settings.PAGES_MEDIA_URL, self.url.name)
587
        if self.extension == 'pdf':
588
            return u'<i class="fa fa-file-pdf-o" aria-hidden="true"></i>'
589
        if self.extension in ['doc', 'docx']:
590
            return u'<i class="fa fa-file-word-o" aria-hidden="true"></i>'
591
        if self.extension in ['zip', 'gzip', 'rar']:
592
            return u'<i class="fa fa-file-archive-o" aria-hidden="true"></i>    '
593
        return u'<i class="fa fa-file-o" aria-hidden="true"></i>'
594
    image.short_description = _('Thumbnail')
595
    image.allow_tags = True
596
597
    class Meta:
598
        verbose_name_plural = _('Medias')
599
600
    def save(self, *args, **kwargs):
601
        parts = self.url.name.split('.')
602
        if len(parts) > 1:
603
            self.extension = parts[-1].lower()
604
        if not self.title:
605
            parts = self.url.name.split('/')
606
            self.title = parts[-1]
607
608
        super(Media, self).save(*args, **kwargs)
609
610
    def __str__(self):
611
        return self.url.name
612
613