class Rating(ExportModelOperationsMixin('rating'), models.Model): """ A rating that can be given to a piece of content. """ value = models.IntegerField(_("Value")) rating_date = models.DateTimeField(_("Rating date"), auto_now_add=True, null=True) content_type = models.ForeignKey("contenttypes.ContentType") object_pk = models.IntegerField() content_object = GenericForeignKey("content_type", "object_pk") user = models.ForeignKey(get_user_model_name(), verbose_name=_("Rater"), null=True, related_name="%(class)ss") class Meta: verbose_name = _("Rating") verbose_name_plural = _("Ratings") def save(self, *args, **kwargs): """ Validate that the rating falls between the min and max values. """ valid = map(str, settings.RATINGS_RANGE) if str(self.value) not in valid: raise ValueError("Invalid rating. %s is not in %s" % (self.value, ", ".join(valid))) super(Rating, self).save(*args, **kwargs)
class OwnableOrNot(models.Model): """ Abstract model that provides ownership of an object for a user. """ user = models.ForeignKey(get_user_model_name(), verbose_name=_("Author"), related_name="%(class)ss", null=True, blank=True, on_delete=models.SET_NULL) class Meta: abstract = True
class NewsPost(models.Model): slug = models.CharField(_('Slug'), max_length=255, editable=False, db_index=True) title = models.CharField(_('Title'), max_length=255, blank=True) content = RichTextField(_('Content')) user = models.ForeignKey(get_user_model_name(), verbose_name=_('Author'), related_name='%(class)ss', on_delete=models.PROTECT) status = models.IntegerField( _('Status'), choices=CONTENT_STATUS_CHOICES, default=CONTENT_STATUS_PUBLISHED, db_index=True, help_text=_( 'With Draft chosen, will only be shown for admin users on the site.' )) entry_date = models.DateTimeField(_('Published'), editable=False, auto_now_add=True, db_index=True) publish_date = models.DateTimeField(_('Published on'), blank=True, db_index=True, default=timezone.now) # add a 'published' method to the Manager to filter by published status objects = PublishedManager() class Meta: ordering = ('-publish_date', ) verbose_name = _('News') verbose_name_plural = _('News') # ---------------------------------------------------------------------- def save(self, *args, **kwargs): # pylint: disable=signature-differs if not self.slug: self.slug = slugify(self.title) super().save(*args, **kwargs) # ---------------------------------------------------------------------- def get_absolute_url(self): return reverse('news_detail', kwargs={'newspost_slug': self.slug}) # ---------------------------------------------------------------------- def __str__(self): return self.title
from django.utils.encoding import python_2_unicode_compatible from django.utils.html import strip_tags from django.utils.timesince import timesince from django.utils.timezone import now from django.utils.translation import ugettext, ugettext_lazy as _ from mezzanine.conf import settings from mezzanine.core.fields import RichTextField from mezzanine.core.managers import DisplayableManager, CurrentSiteManager from mezzanine.generic.fields import KeywordsField from mezzanine.utils.html import TagCloser from mezzanine.utils.models import base_concrete_model, get_user_model_name from mezzanine.utils.sites import current_site_id from mezzanine.utils.urls import admin_url, slugify, unique_slug user_model_name = get_user_model_name() class SiteRelated(models.Model): """ Abstract model for all things site-related. Adds a foreignkey to Django's ``Site`` model, and filters by site with all querysets. See ``mezzanine.utils.sites.current_site_id`` for implementation details. """ objects = CurrentSiteManager() class Meta: abstract = True
from django.utils.encoding import python_2_unicode_compatible from django.utils.html import strip_tags from django.utils.timesince import timesince from django.utils.timezone import now from django.utils.translation import ugettext, ugettext_lazy as _ from mezzanine.core.fields import RichTextField from mezzanine.core.managers import DisplayableManager, CurrentSiteManager from mezzanine.generic.fields import KeywordsField from mezzanine.utils.html import TagCloser from mezzanine.utils.models import base_concrete_model, get_user_model_name from mezzanine.utils.sites import current_site_id from mezzanine.utils.urls import admin_url, slugify, unique_slug user_model_name = get_user_model_name() class SiteRelated(models.Model): """ Abstract model for all things site-related. Adds a foreignkey to Django's ``Site`` model, and filters by site with all querysets. See ``mezzanine.utils.sites.current_site_id`` for implementation details. """ objects = CurrentSiteManager() class Meta: abstract = True