def tearDownClass(cls):
     super(BaseOrphanedMediaCommandTestsMixin, cls).tearDownClass()
     raw_storage = RawMediaCloudinaryStorage()
     raw_storage.delete(cls.file)
     raw_storage.delete(cls.file_2)
     raw_storage.delete(cls.file_3)
     raw_storage.delete(cls.file_removed)
     raw_storage.delete(cls.file_removed_2)
     image_storage = MediaCloudinaryStorage()
     image_storage.delete(cls.file_4)
     set_media_tag(DEFAULT_MEDIA_TAG)
Exemple #2
0
class Flag(models.Model):
    '''
    profile class to define FlagRecord objects
    '''
    STATUS = [('Under Investigation', 'Under Investigation'),
              ('rejected', 'rejected'), ('resolved', 'resolved')]
    title = models.CharField(max_length=100)
    description = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    latitude = models.DecimalField(max_digits=9, decimal_places=6, default='')
    longitude = models.DecimalField(max_digits=9, decimal_places=6, default='')
    tags = models.ManyToManyField(Tag)
    image = models.ImageField(upload_to='images/flagimages/',
                              blank=True,
                              storage=MediaCloudinaryStorage())
    videos = models.FileField(upload_to='videos/',
                              blank=True,
                              storage=VideoMediaCloudinaryStorage(),
                              validators=[validate_video])
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "Flags"
        ordering = ["-pk"]
Exemple #3
0
class TestImageModel(models.Model):
    name = models.CharField(max_length=100)
    file = models.FileField(upload_to='tests/',
                            blank=True,
                            storage=RawMediaCloudinaryStorage())
    image = models.ImageField(upload_to='tests-images/',
                              blank=True,
                              storage=MediaCloudinaryStorage())
Exemple #4
0
class InterventionRecord(models.Model):
    '''
    profile class to define InterventionRecord objects
    '''
    STATUS = (('Under Investigation', 'Under Investigation'),
              ('rejected', 'rejected'), ('resolved', 'resolved'))
    title = models.CharField(max_length=50, blank=False)
    description = models.TextField(blank=True, null=True)
    time_of_creation = models.DateTimeField(auto_now_add=True)
    time_last_edit = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=20,
                              choices=STATUS,
                              blank=True,
                              null=True,
                              default="waiting")
    latitude = models.CharField(max_length=200, blank=True, null=True)
    longitude = models.CharField(max_length=200, blank=True, null=True)

    image = models.ImageField(
        upload_to='images/interventionimages/',
        blank=True,
        null=True,
        storage=MediaCloudinaryStorage(),
        max_length=100000,
        default="media/images/intervention_default_ozvizh.jpg")
    videos = models.FileField(upload_to='videos/',
                              blank=True,
                              null=True,
                              storage=VideoMediaCloudinaryStorage(),
                              validators=[validate_video])
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    tags = models.ManyToManyField(Tag, blank=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-pk"]
Exemple #5
0
class Article(CoreModel):
    """
    A model for representing each article for the blog.

    Attributes:
        author (ForeignKey): one-to-one relation to set user to object
        tags (ManyToManyField): many-to-many relation to set tags to object
        video (FileField): field for video files. saved to cloudinary
        cover (ImageField): field for image files. saved to cloudinary
        title (CharField): field for article title. max length to 255. this field needs to be unique
        slug (SlugField): field for article slug. used for routing
        description (TextField): field for article description.
        content (MartorField): field for article content. uses martor's MartorField for markdown.
        related_article (ManyToManyField): many-to-many relation to set self as related articles
        keywords (CharField): field for article keyword. this is used for SEO.
        publish_at (DateTimeField) field for article publish datetime.
        objects (ArticleManager): set custom Manager to model

    Note:
        because ImageField and DateTimeField saves string in 
        the database, null=True is not necessary.
    """
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    tags = models.ManyToManyField(Tag, blank=True)
    video = models.FileField(upload_to=upload_video_to,
                             blank=True,
                             null=True,
                             storage=VideoMediaCloudinaryStorage(),
                             validators=[validate_video])
    cover = models.ImageField(upload_to=upload_image_to,
                              blank=True,
                              null=True,
                              storage=MediaCloudinaryStorage())
    title = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(unique=True)
    description = models.TextField()
    content = MartorField()
    related_articles = models.ManyToManyField('self',
                                              verbose_name='related articles',
                                              blank=True)
    keywords = models.CharField('記事のキーワード', max_length=255, default='プログラミング')
    publish_at = models.DateTimeField(auto_now=False,
                                      auto_now_add=False,
                                      blank=True,
                                      null=True)

    objects = ArticleManager()

    class Meta:
        """
        Attributes:
            ordering (List): use to determine the ordering of model objects when listed
        """
        ordering = [
            '-publish_at',
            '-timestamp',
            '-updated',
        ]

    def __str__(self):
        """
        determine which field of the model should be representing the model object.
        mainly used in admin site.

        Returns:
            str: returns title field.
        """
        return self.title

    def get_absolute_url(self):
        """
        determine to absolute url of the model.
        mainly used to route to detail view.
        """
        return reverse("articles:article_detail", kwargs={"slug": self.slug})

    def get_markdown(self):
        """
        return a cleaned html.
        in case there is a markdown we use markdown package to convert them to html.

        Returns:
            str: string of safe html
        """
        content = self.content
        markdown_content = markdownify(content)
        return mark_safe(markdown_content)

    def get_description(self):
        """
        return description. if the description str length
        is greater then 50 it truncates the str.

        Returns:
            str: truncated string
        """
        if len(self.description) > 50:
            return f"{self.description[:50]}..."
        return self.description

    def get_text_count(self):
        """
        count strings in html.
        using BeautifulSoup4 to sanitize html tags.

        Returns:
            int: length of the string after sanitized by BeautifulSoup4
        """
        content = self.content
        markdown_content = markdownify(content)
        souped = BeautifulSoup(markdown_content,
                               features="html.parser").findAll(text=True)
        stripted_text = "".join(souped).replace(" ", "")
        return len(stripted_text)

    def get_img_count(self):
        """
        count img tags in html.
        using BeautifulSoup4 search through html.

        Returns:
            int: length of the images after filtering through using BeautifulSoup4
        """
        content = self.content
        markdown_content = markdownify(content)
        img_tags = BeautifulSoup(markdown_content,
                                 features="html.parser").find_all("img")
        return len(img_tags)

    @property
    def is_series_summary(self):
        return 'Series' in [tag.name for tag in self.tags.all()]

    @property
    def is_published(self):
        if self.publish_at is None:
            return False
        return timezone.now() > self.publish_at
 def setUpClass(cls):
     super(CloudinaryMediaStorageTests, cls).setUpClass()
     cls.file_content = b'Content of file'
     cls.storage = MediaCloudinaryStorage(tag=TAG, resource_type='raw')
     cls.file_name, cls.file = cls.upload_file()