class TranslatableArticle(TranslatableModel): translations = TranslatedFields(title=models.CharField('title', max_length=234), slug=models.SlugField()) section = AppHookConfigField(ExampleConfig, verbose_name='section') objects = AppHookConfigTranslatableManager()
class BlogCategory(TranslatableModel): """ Blog category """ parent = models.ForeignKey('self', verbose_name=_('parent'), null=True, blank=True) date_created = models.DateTimeField(_('created at'), auto_now_add=True) date_modified = models.DateTimeField(_('modified at'), auto_now=True) app_config = AppHookConfigField(BlogConfig, null=True, verbose_name=_('app. config')) translations = TranslatedFields( name=models.CharField(_('name'), max_length=255), slug=models.SlugField(_('slug'), max_length=255, blank=True, db_index=True), meta={'unique_together': (('language_code', 'slug'), )}) objects = AppHookConfigTranslatableManager() class Meta: verbose_name = _('blog category') verbose_name_plural = _('blog categories') @property def count(self): return self.blog_posts.namespace( self.app_config.namespace).published().count() def get_absolute_url(self, lang=None): if not lang: lang = get_language() if self.has_translation(lang, ): slug = self.safe_translation_getter('slug', language_code=lang) return reverse('%s:posts-category' % self.app_config.namespace, kwargs={'category': slug}, current_app=self.app_config.namespace) # in case category doesn't exist in this language, gracefully fallback # to posts-latest return reverse('%s:posts-latest' % self.app_config.namespace, current_app=self.app_config.namespace) def __str__(self): return self.safe_translation_getter('name') def save(self, *args, **kwargs): super(BlogCategory, self).save(*args, **kwargs) for lang in self.get_available_languages(): self.set_current_language(lang) if not self.slug and self.name: self.slug = slugify(force_text(self.name)) self.save_translations()
class BlogCategory(BlogMetaMixin, TranslatableModel): """ Blog category """ parent = models.ForeignKey('self', verbose_name=_('parent'), null=True, blank=True, related_name='children', on_delete=models.CASCADE) date_created = models.DateTimeField(_('created at'), auto_now_add=True) date_modified = models.DateTimeField(_('modified at'), auto_now=True) app_config = AppHookConfigField(BlogConfig, null=True, verbose_name=_('app. config')) translations = TranslatedFields( name=models.CharField(_('name'), max_length=752), slug=models.SlugField(_('slug'), max_length=752, blank=True, db_index=True), meta_description=models.TextField( verbose_name=_('category meta description'), blank=True, default=''), meta={'unique_together': (('language_code', 'slug'), )}) objects = AppHookConfigTranslatableManager() _metadata = { 'title': 'get_title', 'description': 'get_description', 'og_description': 'get_description', 'twitter_description': 'get_description', 'gplus_description': 'get_description', 'locale': 'get_locale', 'object_type': 'get_meta_attribute', 'og_type': 'get_meta_attribute', 'og_app_id': 'get_meta_attribute', 'og_profile_id': 'get_meta_attribute', 'og_publisher': 'get_meta_attribute', 'og_author_url': 'get_meta_attribute', 'og_author': 'get_meta_attribute', 'twitter_type': 'get_meta_attribute', 'twitter_site': 'get_meta_attribute', 'twitter_author': 'get_meta_attribute', 'gplus_type': 'get_meta_attribute', 'gplus_author': 'get_meta_attribute', 'url': 'get_absolute_url', } class Meta: verbose_name = _('blog category') verbose_name_plural = _('blog categories') def descendants(self): children = [] if self.children.exists(): children.extend(self.children.all()) for child in self.children.all(): children.extend(child.descendants()) return children @cached_property def linked_posts(self): return self.blog_posts.namespace(self.app_config.namespace) @cached_property def count(self): return self.linked_posts.published().count() @cached_property def count_all_sites(self): return self.linked_posts.published(current_site=False).count() def get_absolute_url(self, lang=None): if not lang or lang not in self.get_available_languages(): lang = get_language() if not lang or lang not in self.get_available_languages(): lang = self.get_current_language() if self.has_translation(lang): slug = self.safe_translation_getter('slug', language_code=lang) return reverse('%s:posts-category' % self.app_config.namespace, kwargs={'category': slug}, current_app=self.app_config.namespace) # in case category doesn't exist in this language, gracefully fallback # to posts-latest return reverse('%s:posts-latest' % self.app_config.namespace, current_app=self.app_config.namespace) def __str__(self): default = ugettext('BlogCategory (no translation)') return self.safe_translation_getter('name', any_language=True, default=default) def save(self, *args, **kwargs): super(BlogCategory, self).save(*args, **kwargs) for lang in self.get_available_languages(): self.set_current_language(lang) if not self.slug and self.name: self.slug = slugify(force_text(self.name)) self.save_translations() def get_title(self): title = self.safe_translation_getter('name', any_language=True) return title.strip() def get_description(self): description = self.safe_translation_getter('meta_description', any_language=True) return escape(strip_tags(description)).strip()
class JobCategory(TranslatedAutoSlugifyMixin, TranslationHelperMixin, TranslatableModel): slug_source_field_name = 'name' translations = TranslatedFields( name=models.CharField(_('name'), max_length=255), slug=models.SlugField( _('slug'), max_length=255, blank=True, help_text=_('Auto-generated. Used in the URL. If changed, the URL ' 'will change. Clear it to have the slug re-created.'))) supervisors = models.ManyToManyField( getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), verbose_name=_('supervisors'), # FIXME: This is mis-named should be "job_categories"? related_name='job_opening_categories', help_text=_('Supervisors will be notified via email when a new ' 'job application arrives.'), blank=True) app_config = models.ForeignKey(JobsConfig, null=True, verbose_name=_('app configuration'), related_name='categories') ordering = models.IntegerField(_('ordering'), default=0) objects = AppHookConfigTranslatableManager() class Meta: verbose_name = _('job category') verbose_name_plural = _('job categories') ordering = ['ordering'] def __str__(self): return self.safe_translation_getter('name', str(self.pk)) def _slug_exists(self, *args, **kwargs): """Provide additional filtering for slug generation""" qs = kwargs.get('qs', None) if qs is None: qs = self._get_slug_queryset() # limit qs to current app_config only kwargs['qs'] = qs.filter(app_config=self.app_config) return super(JobCategory, self)._slug_exists(*args, **kwargs) def get_absolute_url(self, language=None): language = language or self.get_current_language() slug = self.safe_translation_getter('slug', language_code=language) if self.app_config_id: namespace = self.app_config.namespace else: namespace = 'aldryn_jobs' with force_language(language): try: if not slug: return reverse('{0}:job-opening-list'.format(namespace)) kwargs = {'category_slug': slug} return reverse( '{0}:category-job-opening-list'.format(namespace), kwargs=kwargs, current_app=self.app_config.namespace) except NoReverseMatch: return "/%s/" % language def get_notification_emails(self): return self.supervisors.values_list('email', flat=True) # We keep this 'count' name for compatibility in templates: # there used to be annotate() call with the same property name. def count(self): return self.jobs.active().count()
class BlogCategory(BlogMetaMixin, TranslatableModel): """ Blog category """ parent = models.ForeignKey("self", verbose_name=_("parent"), null=True, blank=True, related_name="children", on_delete=models.CASCADE) date_created = models.DateTimeField(_("created at"), auto_now_add=True) date_modified = models.DateTimeField(_("modified at"), auto_now=True) app_config = AppHookConfigField(BlogConfig, null=True, verbose_name=_("app. config")) order = models.PositiveIntegerField(default=0) translations = TranslatedFields( name=models.CharField(_("name"), max_length=752), slug=models.SlugField(_("slug"), max_length=752, blank=True, db_index=True), meta_description=models.TextField( verbose_name=_("category meta description"), blank=True, default=""), meta={"unique_together": (("language_code", "slug"), )}, ) objects = AppHookConfigTranslatableManager() _metadata = { "title": "get_title", "description": "get_description", "og_description": "get_description", "twitter_description": "get_description", "gplus_description": "get_description", "locale": "get_locale", "object_type": "get_meta_attribute", "og_type": "get_meta_attribute", "og_app_id": "get_meta_attribute", "og_profile_id": "get_meta_attribute", "og_publisher": "get_meta_attribute", "og_author_url": "get_meta_attribute", "og_author": "get_meta_attribute", "twitter_type": "get_meta_attribute", "twitter_site": "get_meta_attribute", "twitter_author": "get_meta_attribute", "gplus_type": "get_meta_attribute", "gplus_author": "get_meta_attribute", "url": "get_absolute_url", } class Meta: verbose_name = _("blog category") verbose_name_plural = _("blog categories") ordering = ['order'] def descendants(self): children = [] if self.children.exists(): children.extend(self.children.all()) for child in self.children.all(): children.extend(child.descendants()) return children @cached_property def linked_posts(self): return self.blog_posts.namespace(self.app_config.namespace) @cached_property def count(self): return self.linked_posts.published().count() @cached_property def count_all_sites(self): return self.linked_posts.published(current_site=False).count() def get_absolute_url(self, lang=None): lang = _get_language(self, lang) if self.has_translation(lang): slug = self.safe_translation_getter("slug", language_code=lang) return reverse( "%s:posts-category" % self.app_config.namespace, kwargs={"category": slug}, current_app=self.app_config.namespace, ) # in case category doesn't exist in this language, gracefully fallback # to posts-latest return reverse("%s:posts-latest" % self.app_config.namespace, current_app=self.app_config.namespace) def __str__(self): default = gettext("BlogCategory (no translation)") return self.safe_translation_getter("name", any_language=True, default=default) def save(self, *args, **kwargs): super().save(*args, **kwargs) for lang in self.get_available_languages(): self.set_current_language(lang) if not self.slug and self.name: self.slug = slugify(force_str(self.name)) self.save_translations() def get_title(self): title = self.safe_translation_getter("name", any_language=True) return title.strip() def get_description(self): description = self.safe_translation_getter("meta_description", any_language=True) return escape(strip_tags(description)).strip()