/apps/blogroll/models.py

https://github.com/kaitlin/HollaBackDC · Python · 53 lines · 34 code · 12 blank · 7 comment · 4 complexity · 74e39bdefa44004ef6e477b16b9d0300 MD5 · raw file

  1. # -*- mode: python; coding: utf-8; -*-
  2. """
  3. Django application that allows to create XFN-compatible block of links
  4. """
  5. from django.contrib.auth.models import User
  6. from django.contrib.sites.models import Site
  7. from django.db import models
  8. from django.utils.translation import ugettext_lazy as _
  9. from blogroll.relations import *
  10. class Link(models.Model):
  11. user = models.ForeignKey(User, related_name='blogroll', blank=True, null=True)
  12. site = models.ForeignKey(Site, related_name='blogroll')
  13. url = models.URLField(_('URL'), verify_exists=False, blank=True)
  14. name = models.CharField(_('Name'), max_length=100, blank=True)
  15. relations = models.CharField(_('Relations'), max_length=100, blank=True, editable=False)
  16. weight = models.IntegerField(_('Weight'), blank=True, default=0, help_text=_(u'You can order links by this field. Link with smaller number goes first.'))
  17. friendship_rel = models.CharField(_('Friendship relation'), max_length=20, choices=FRIENDSHIP_REL, blank=True)
  18. physical_rel = models.CharField(_('Physical relation'), max_length=20, choices=PHYSICAL_REL, blank=True)
  19. professional_rel = models.CharField(_('Profesional relation'), max_length=20, choices=PROFESSIONAL_REL, blank=True)
  20. geographical_rel = models.CharField(_('Geographical relation'), max_length=20, choices=GEOGRAPHICAL_REL, blank=True)
  21. family_rel = models.CharField(_('Family relation'), max_length=20, choices=FAMILY_REL, blank=True)
  22. romantic_rel = models.CharField(_('Romantic relation'), max_length=20, choices=ROMANTIC_REL, blank=True)
  23. identity_rel = models.CharField(_('Identity relation'), max_length=20, choices=IDENTITY_REL, blank=True)
  24. class Meta:
  25. ordering = ['weight']
  26. verbose_name = _('Link')
  27. verbose_name_plural = _('Links')
  28. def __unicode__(self):
  29. return self.name
  30. def save(self):
  31. """
  32. Cache all not empty relations in single relations field
  33. """
  34. rels = ['friendship_rel', 'physical_rel', 'professional_rel',
  35. 'geographical_rel', 'family_rel', 'romantic_rel', 'identity_rel']
  36. self.relations = ' '.join(filter(lambda x: x != '', [getattr(self, x) for x in rels]))
  37. if not self.name:
  38. self.name = self.user.name or self.user.username
  39. if not self.url:
  40. self.url = self.user.site
  41. super(self.__class__, self).save()