class Activity(WebLocalePageModel, PublishedModel): annotation = StripTextField( verbose_name='аннотация', null=True, blank=True, ) content = StripTextField( verbose_name='содержание', ) class Meta(WebLocalePageModel.Meta): db_table = 'cck_activity' verbose_name = 'деятельность' verbose_name_plural = 'деятельности' @cached_property def articles(self): return self.activityarticle_set.all() @cached_property def targets(self): return self.object_set.all() def targets_actual(self): return self.targets.filter(actual=True) def targets_unactual(self): return self.targets.filter(actual=False) def get_absolute_url(self): lang = [self.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('activities:detail', args=lang + [self.slug]) get_absolute_url.short_description = 'URI' get_absolute_url.admin_order_field = 'slug' def clean(self): super(Activity, self).clean() try: model = Activity.objects.get( locale=self.locale, slug=self.slug, ) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except Activity.DoesNotExist: pass
class ActivityArticle(WebPageModel, PublishedModel): parent = models.ForeignKey( Activity, verbose_name='предок', null=True, on_delete=models.CASCADE, ) content = StripTextField( verbose_name='содержание', ) class Meta: db_table = 'cck_activity_article' verbose_name = 'статья' verbose_name_plural = 'статьи' ordering = ( 'parent', 'index', ) unique_together = ('parent', 'slug',), def get_absolute_url(self): lang = [self.parent.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('activities:content', args=lang + [self.parent.slug, self.slug]) get_absolute_url.short_description = 'URI' get_absolute_url.admin_order_field = 'slug' def clean(self): super(ActivityArticle, self).clean() if self.aliace.find('//') == -1: try: model = ActivityArticle.objects.get( parent=self.parent, slug=self.slug, ) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except ActivityArticle.DoesNotExist: pass
class Order(FromDBModel): name = StripCharField( verbose_name='имя', max_length=128, db_index=True, ) phone = StripCharField( verbose_name='телефон', max_length=128, db_index=True, ) sent_date = models.DateTimeField( verbose_name='дата', auto_now_add=True, db_index=True, ) state = models.NullBooleanField( verbose_name='обратный звонок', db_index=True, ) note = StripTextField( verbose_name='примечание', null=True, blank=True, ) agent = models.ForeignKey( settings.AUTH_USER_MODEL, limit_choices_to={'is_staff': True}, related_name='+', verbose_name='агент', null=True, blank=True, on_delete=models.SET_NULL, ) site = models.ForeignKey( Site, related_name='+', verbose_name='сайт', null=True, on_delete=models.CASCADE, ) class Meta: db_table = CallbackConfig.label verbose_name = 'заказ' verbose_name_plural = 'заказы' ordering = ( 'state', '-sent_date', 'site', ) def __str__(self): return force_text(self.name) def save(self, *args, **kwargs): if self._state.adding: self.site = Site.objects.get_current() super(Order, self).save(*args, **kwargs)
class Permit(WebLocalePageModel, PublishedModel): values = getattr(settings, 'PERMITS_AFFILIATES', []) OWNERS = {} OWNER_CHOICES = [] for value in values: key = slugify(value) OWNERS[key] = value OWNER_CHOICES.append(( key, value, )) owner = StripCharField( verbose_name='владелец', max_length=128, choices=OWNER_CHOICES, default=OWNER_CHOICES[0][0], db_index=True, ) number = StripCharField( verbose_name='номер', max_length=128, null=True, blank=True, ) provider = StripCharField( verbose_name='кем выдано', max_length=128, ) issue_date = models.DateField( verbose_name='дата выдачи', null=True, blank=True, ) onset_date = models.DateField( verbose_name='начало действия', null=True, blank=True, ) end_date = models.DateField( verbose_name='окончание действия', null=True, blank=True, ) note = StripTextField( verbose_name='примечания', null=True, blank=True, ) alboms = GenericRelation(AlbomGeneric) class Meta: db_table = 'cck_permit' verbose_name = 'документ' verbose_name_plural = 'документы' ordering = ( '-locale', 'owner', 'index', ) unique_together = ( 'locale', 'owner', 'slug', ), @cached_property def generic(self): return self.alboms.all() @cached_property def cover(self): if self.generic: return self.generic[0].cover return None def get_absolute_url(self): lang = [self.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('permits:detail', args=lang + [self.owner, self.slug]) get_absolute_url.short_description = 'URI' get_absolute_url.admin_order_field = 'slug' def get_owner_name(self): return self.OWNERS.get(self.owner, '<%s>' % self.owner) def get_owner_url(self): lang = [self.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('permits:list', args=lang + [self.owner]) def clean(self): if not bool(self.title): self.title = '%s %s' % (strip_tags(self.name), self.number) super(Permit, self).clean() try: model = Permit.objects.get( locale=self.locale, owner=self.owner, slug=self.slug, ) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except Permit.DoesNotExist: pass
class Object(WebLocalePageModel, PublishedModel): actual = models.BooleanField( verbose_name='в работе', default=False, db_index=True, ) address = StripCharField( verbose_name='адрес', max_length=255, ) longitude = models.FloatField( verbose_name='долгота', default=0, blank=True, ) latitude = models.FloatField( verbose_name='широта', default=0, blank=True, ) zoom = models.PositiveSmallIntegerField( verbose_name='зум', null=True, blank=True, ) customer = StripCharField( verbose_name='заказчик', max_length=128, ) developer = StripCharField( verbose_name='застройщик', max_length=128, null=True, blank=True, ) start_date = models.DateField( verbose_name='начало работ', null=True, blank=True, ) finish_date = models.DateField( verbose_name='окончание работ', null=True, blank=True, ) note = StripTextField( verbose_name='примечания', null=True, blank=True, ) activity = models.ManyToManyField( Activity, limit_choices_to={'published': True}, db_table='cck_object_activity', verbose_name='деятельность', blank=True, ) alboms = GenericRelation(AlbomGeneric) class Meta: db_table = 'cck_object' verbose_name = 'объект' verbose_name_plural = 'объекты' ordering = ( '-locale', '-actual', 'index', ) unique_together = ('locale', 'slug',), @cached_property def activities(self): return self.activity.all() @cached_property def generic(self): return self.alboms.all() @cached_property def cover(self): if bool(self.generic): return self.generic[0].albom.cover return None def get_absolute_url(self): lang = [self.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('objects:detail', args=lang + [self.slug]) get_absolute_url.short_description = 'URI' get_absolute_url.admin_order_field = 'slug' def clean(self): super(Object, self).clean() self.longitude = abs(self.longitude) if bool(self.longitude) else 0 self.latitude = abs(self.latitude) if bool(self.latitude) else 0 self.zoom = abs(self.zoom) if bool(self.zoom) else None try: model = Object.objects.get( locale=self.locale, slug=self.slug, ) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except Object.DoesNotExist: pass
class Article(WebLocalePageModel, PublishedModel): UPLOAD_TO = 'news' PATH = path.join(settings.MEDIA_ROOT, UPLOAD_TO) annotation = StripTextField( verbose_name='аннотация', null=True, blank=True, ) content = StripTextField( verbose_name='содержание', ) tags = ArrayField( models.CharField(max_length=32), size=4, verbose_name='ярлыки', null=True, blank=True, ) source = models.URLField( verbose_name='источник', null=True, blank=True, ) images = GenericRelation(ImageGeneric) class Meta(WebLocalePageModel.Meta): db_table = 'cck_news' verbose_name = 'статья' verbose_name_plural = 'статьи' ordering = ( '-locale', '-published_date', 'name', ) @cached_property def generic(self): return self.images.all() @cached_property def cover(self): if bool(self.generic): return self.generic[0].image return None def get_absolute_url(self): lang = [self.locale] if len(settings.LANGUAGES) > 1 else [] return reverse('news:detail', args=lang + [self.slug]) get_absolute_url.short_description = 'URI' get_absolute_url.admin_order_field = 'slug' def clean(self): super(Article, self).clean() if bool(self.tags): self.tags = [slugify(tag, ascii=False, space=' ') for tag in self.tags] try: model = Article.objects.get( locale=self.locale, slug=self.slug, ) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except Article.DoesNotExist: pass
class Page(MPTTModel, WebLocalePageModel, PublishedModel): parent = TreeForeignKey( 'self', limit_choices_to={'cluster': True}, verbose_name='предок', null=True, blank=True, on_delete=models.SET_NULL, ) cluster = models.BooleanField( verbose_name='кластер', default=False, db_index=True, ) annotation = StripTextField( verbose_name='аннотация', null=True, blank=True, ) content = StripTextField(verbose_name='содержание', ) source = models.URLField( verbose_name='источник', max_length=255, null=True, blank=True, ) layout = StripCharField( verbose_name='шаблон', max_length=255, null=True, blank=True, ) publish = PublishMPTTManager() navigate = NavigateMPTTManager() class MPTTMeta: tree_id_attr = 'mptt_tree' level_attr = 'mptt_level' left_attr = 'mptt_left' right_attr = 'mptt_right' order_insertion_by = ['index'] class Meta(WebLocalePageModel.Meta): db_table = ContentConfig.label verbose_name = 'страница' verbose_name_plural = 'страницы' ordering = ( 'mptt_tree', 'mptt_left', ) def get_absolute_url(self): return reverse('content:detail', args=[self.slug]) get_absolute_url.short_description = 'URL' get_absolute_url.admin_order_field = 'slug' def get_layout(self): if bool(self.layout): return self.layout elif bool(self.parent): return self.parent.get_layout() return None get_layout.short_description = 'шаблон' def inherit(self): super(Page, self).clean() self.slug = '%s/%s' % (self.parent.slug, self.slug) self.save() def clean(self): super(Page, self).clean() if not self.is_leaf_node(): self.cluster = True if bool(self.parent): if self.parent.pk == self.pk: raise ValidationError( 'Элемент не может быть потомком самому себе') self.slug = '%s/%s' % (self.parent.slug, self.slug) try: model = Page.objects.get(slug=self.slug) if model.pk != self.pk: raise ValidationError('Генерируется неуникальное значение URI') except Page.DoesNotExist: pass def save(self, *args, **kwargs): commit = adding = self._state.adding if not adding: for field in self._loaded_values: if self._loaded_values[field] != getattr(self, field): commit = True break if commit: super(Page, self).save(*args, **kwargs) if not adding and self._loaded_values['slug'] != self.slug: for model in self.get_children(): model.inherit() Page._tree_manager.rebuild()
class Mail(FromDBModel): STATE_CHOICES = ( ('0', 'Важное',), ('1', 'Прочитано',), ('2', 'Спам',), ) name = StripCharField( verbose_name='имя', max_length=128, db_index=True, ) email = StripCharField( max_length=128, db_index=True, ) phone = StripCharField( verbose_name='телефон', max_length=128, null=True, blank=True, ) subject = StripCharField( verbose_name='тема', max_length=128, null=True, blank=True, db_index=True, ) message = StripTextField( verbose_name='сообщение', ) sent_date = models.DateTimeField( verbose_name='дата', auto_now_add=True, db_index=True, ) state = models.CharField( verbose_name='тип', max_length=1, choices=STATE_CHOICES, null=True, blank=True, db_index=True, ) note = StripTextField( verbose_name='примечание', null=True, blank=True, ) agent = models.ForeignKey( settings.AUTH_USER_MODEL, limit_choices_to={'is_staff': True}, related_name='+', verbose_name='агент', null=True, blank=True, on_delete=models.SET_NULL, ) site = models.ForeignKey( Site, related_name='+', verbose_name='сайт', null=True, on_delete=models.CASCADE, ) class Meta: db_table = PostboxConfig.label verbose_name = 'письмо' verbose_name_plural = 'письма' ordering = ( 'state', '-sent_date', 'site', ) def __str__(self): return force_text(self.name) def save(self, *args, **kwargs): if self._state.adding: self.site = Site.objects.get_current() super(Mail, self).save(*args, **kwargs)