Ejemplo n.º 1
0
class PageText(models.Model):
    id = BigAutoField(primary_key=True)
    poster = BigForeignKey(Poster, related_name='poster_texts')
    poster_page = BigForeignKey(PosterPage, related_name='page_texts')
    content = models.CharField(max_length=255)
    name = models.CharField(max_length=64)

    class Meta:
        unique_together = ('poster', 'name')

    def __str__(self):
        return "{:s}".format(self.name)
Ejemplo n.º 2
0
class Comment(models.Model):
    id = BigAutoField(primary_key=True)
    parent = BigForeignKey('Comment',
                           related_name='children',
                           null=True,
                           blank=True)
    poster = BigForeignKey('Poster')
    creator = models.ForeignKey(User)
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=256)
    content = models.CharField(max_length=2048)

    def __str__(self):
        return "{:d}".format(self.pk)
Ejemplo n.º 3
0
class PosterLike(models.Model):
    id = BigAutoField(primary_key=True)
    poster = BigForeignKey('Poster', related_name='poster_likes')
    creator = models.ForeignKey(User)
    liked = models.BooleanField(default=True)

    class Meta:
        unique_together = ('poster', 'creator')

    def __str__(self):
        return "{:d}".format(self.pk)

    def save(self, **kwargs):
        adding = self._state.adding
        with transaction.atomic():
            if not adding:
                old_poster_like = PosterLike.objects.filter(
                    pk=self.pk).only('liked').select_for_update()
                old_poster_like = old_poster_like[0]
            super(PosterLike, self).save(**kwargs)
            queryset = PosterStatistics.objects.filter(pk=self.poster_id)
            if adding:
                fields = {'likes_count': 1}
                DBUtils.increase_counts(queryset, fields)
            elif old_poster_like.liked != self.liked:
                if self.liked:
                    likes_count = 1
                else:
                    likes_count = -1
                DBUtils.increase_counts(queryset, {'likes_count': likes_count})
Ejemplo n.º 4
0
class PosterVideo(models.Model):
    id = models.AutoField(primary_key=True)
    poster = BigForeignKey('Poster', related_name='poster_videos')
    video = models.ForeignKey(Video, related_name='poster_videos')
    name = models.CharField(max_length=63)

    class Meta:
        unique_together = (('poster', 'video'), ('poster', 'name'))

    def __str__(self):
        return "{:d}".format(self.pk)
Ejemplo n.º 5
0
class PosterPage(models.Model):
    id = BigAutoField(primary_key=True)
    poster = BigForeignKey(Poster, related_name='poster_pages')
    template = models.ForeignKey('Template')
    index = models.SmallIntegerField(default=0)
    name = models.CharField(max_length=64, default='')

    class Meta:
        unique_together = ('poster', 'index')

    def __str__(self):
        return "{:s}".format(self.name)
Ejemplo n.º 6
0
class Rating(models.Model):
    id = BigAutoField(primary_key=True)
    poster = BigForeignKey('Poster', related_name='ratings')
    creator = models.ForeignKey(User)
    created_at = models.DateTimeField(auto_now_add=True)
    rate = models.SmallIntegerField(default=5,
                                    validators=[
                                        validators.MinValueValidator(1),
                                        validators.MaxValueValidator(5)
                                    ])
    RATE_TO_FIELD = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
    RATE_TO_FIELD = {
        key: value + '_count'
        for key, value in RATE_TO_FIELD.items()
    }

    def poster_statistics(self):
        name = '_poster_statistics'
        if not hasattr(self, name):
            setattr(self, name,
                    PosterStatistics.objects.get(poster_id=self.poster_id))
        return getattr(self, name)

    def save(self, **kwargs):
        adding = self._state.adding
        with transaction.atomic():
            if not adding:
                old_rating = Rating.objects.filter(
                    pk=self.pk).only('rate').select_for_update()
                old_rating = old_rating[0]
            super(Rating, self).save(**kwargs)
            queryset = PosterStatistics.objects.filter(pk=self.poster_id)
            if adding:
                fields = {
                    'ratings_count': 1,
                    self.RATE_TO_FIELD[self.rate]: 1,
                    'ratings_total': self.rate
                }
                DBUtils.increase_counts(queryset, fields)
            elif old_rating.rate != self.rate:
                fields = {
                    self.RATE_TO_FIELD[old_rating.rate]: -1,
                    self.RATE_TO_FIELD[self.rate]: 1,
                    'ratings_total': self.rate - old_rating.rate
                }
                DBUtils.increase_counts(queryset, fields)

    class Meta:
        unique_together = ('poster', 'creator')

    def __str__(self):
        return "{:d}->{:d}".format(self.pk, self.poster.id)
Ejemplo n.º 7
0
class PosterFun(models.Model):
    id = BigAutoField(primary_key=True)
    poster = BigForeignKey('Poster', related_name='poster_funs')
    ip_address = models.GenericIPAddressField()

    class Meta:
        unique_together = ('poster', 'ip_address')

    def __str__(self):
        return "{:d} -> {:s}".format(self.pk, self.ip_address)

    def save(self, **kwargs):
        if self._state.adding:
            with transaction.atomic():
                super(PosterFun, self).save(**kwargs)
                queryset = PosterStatistics.objects.filter(pk=self.poster_id)
                DBUtils.increase_counts(queryset, {'fun_count': 1})
Ejemplo n.º 8
0
class Poster(models.Model):
    STATUS_DRAFT = 'Draft'
    STATUS_PUBLISHED = 'Published'
    STATUS_ACTIVE = 'Active'
    STATUS_INACTIVE= 'Inactive'
    STATUS_CHOICES = (
        (STATUS_DRAFT, STATUS_DRAFT),
        (STATUS_PUBLISHED, STATUS_PUBLISHED),
        (STATUS_ACTIVE, STATUS_ACTIVE),
        (STATUS_INACTIVE, STATUS_INACTIVE)
    )
    LIFETIME_WEEKLY = 'weekly'
    LIFETIME_SPECIFIC_DAYS = 'specific_days'
    LIFETIME_CHOICES = (
        (LIFETIME_WEEKLY, LIFETIME_WEEKLY),
        (LIFETIME_SPECIFIC_DAYS,LIFETIME_SPECIFIC_DAYS)
    )
    id = BigAutoField(primary_key=True)
    creator = models.ForeignKey(User)
    unique_name = models.CharField(max_length=255, unique=True)
    url = models.CharField(max_length=255, default='')

    #qr_image
    logo_image = models.ForeignKey('Image', related_name='poster_logo_images', null=True)
    logo_title = models.CharField(max_length=255, default='')
    short_description = models.CharField(max_length=255, default='')
    phone = models.CharField(max_length=16, default='')
    mobile = models.CharField(max_length=16, blank=True, default='')
    email = models.EmailField(blank=True, default='')
    address = BigForeignKey('Address', related_name='posters', null=True)
    lifetime_type = models.CharField(max_length=32, choices=LIFETIME_CHOICES, default=LIFETIME_WEEKLY)
    lifetime_timezone = models.CharField(max_length=32, default='America/Los_Angeles')
    lifetime_value = models.CharField(max_length=1024, default='')
    music = models.ForeignKey('Music', null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    main_category = models.ForeignKey('Category', related_name='main_posters')
    sub_category = models.ForeignKey('Category', related_name='sub_posters')
    status = models.CharField(max_length=15, choices=STATUS_CHOICES)
    width = models.PositiveSmallIntegerField(default=800)
    height = models.PositiveSmallIntegerField(default=1024)
    data = OverWriteFileField(upload_to=file.get_data_path, default='')
    html = OverWriteFileField(upload_to=file.get_html_path)
    css = OverWriteFileField(upload_to=file.get_css_path)
    script = OverWriteFileField(upload_to=file.get_script_path)
    tags = models.CharField(max_length=128, default='', blank=True)

    objects = InheritanceManager()

    # prevent save parent
    def save_base(self, raw=False, *args, **kwargs):
        _raw = getattr(self, '_raw', None)
        if _raw is not None:
            self._raw = raw
            raw = _raw
        super(Poster, self).save_base(raw, *args, **kwargs)

    # recover raw
    def _save_table(self, raw=False, *args, **kwargs):
        _raw = getattr(self, '_raw', None)
        if _raw is not None:
            raw = _raw
        return super(Poster, self)._save_table(raw, *args, **kwargs)

    def save(self, **kwargs):
        if self._state.adding and self.__class__ is Poster:
            with transaction.atomic():
                super(Poster, self).save(**kwargs)
                template = getattr(self, 'template', None)
                if template:
                    PosterPage.objects.create(template=self.template, poster=self)
                self.poster_statistics = PosterStatistics.objects.create(poster=self)
                self.history_statistics = HistoryStatistics.objects.create(poster=self)
                child_type = self.main_category.get_type_class()
                child = child_type(poster=self)
                child._raw = True
                child.save(force_insert=True)
        else:
            super(Poster, self).save(**kwargs)

    def __str__(self):
        return "{:d}".format(self.pk)