Rontim / MyCampus-backend

Backend for the MyCampus project
1 stars 0 forks source link

Transitioning to a Unified Blog Model for Campus Website #6

Closed Rontim closed 6 months ago

Rontim commented 6 months ago

Currently, our campus blogging website operates with two distinct blog models. While this setup might have seemed feasible initially, it's becoming increasingly apparent that maintaining two separate models is inefficient and counterproductive. As we've encountered various issues and setbacks recently, it's time to reevaluate our approach and streamline our blog structure for improved functionality and manageability.

Proposal:

I propose we transition to a unified blog model for our campus website. Consolidating our blogging system into a single model offers several advantages, including:

  1. Simplified maintenance
  2. Enhanced Scalability
Rontim commented 6 months ago

This is my proposal:

class Blog(models.Model):
    AUTHOR_TYPE_CHOICES = (
        ('user', 'User'),
        ('club', 'Club'),
    )

    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True, null=True, blank=True)
    thumbnail = models.ImageField(upload_to='blog/thumbnails/', null=True, blank=True)
    content = models.TextField()
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)
    author_type = models.CharField(max_length=20, choices=AUTHOR_TYPE_CHOICES, default='user')
    author_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_blogs', null=True, blank=True)
    author_club = models.ForeignKey(Club, on_delete=models.CASCADE, related_name='club_blogs', null=True, blank=True)
    topics = models.ManyToManyField(Topic, related_name='blogs', blank=True)

    class Meta:
        verbose_name_plural = 'Blogs'

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title
Rontim commented 6 months ago

Request data sample:

{
  "title": "Test Blog3",
  "author_type": "user",
  "author": "val",
  "content": {
    "time": 264062375715,
    "blocks": [
      {
      "id": "hsgvdy265",
      "type": "header",
      "data": {
        "text": "Test Blog3",
        "level": 1
        }
      },
      {
      "id": "hsgvdy265",
      "type": "paragraph",
      "data": {
        "text": "Some content"
        }
      }
    ]
  },
  "topics": [
    {"topic_name": "Health and Wellness"},
    {"topic_name": "Relationships and Dating"}
    ]
}
Rontim commented 6 months ago

Response after calling /api/v1/blog/create:

{
  "id": 12,
  "title": "Test Blog3",
  "slug": "test-blog3",
  "author_type": "user",
  "author_user": "val",
  "author_club": null,
  "thumbnail": null,
  "content": {
    "time": 264062375715,
    "blocks": [
      {
        "id": "hsgvdy265",
        "type": "header",
        "data": {
          "text": "Test Blog3",
          "level": 1
        }
      },
      {
        "id": "hsgvdy265",
        "type": "paragraph",
        "data": {
          "text": "Some content"
        }
      }
    ]
  },
  "topics": [
    {
      "id": 3,
      "slug": "health-and-wellness",
      "topic_name": "Health and Wellness"
    },
    {
      "id": 10,
      "slug": "relationships-and-dating",
      "topic_name": "Relationships and Dating"
    }
  ],
  "updated_at": "2024-03-07T06:10:19.480236Z",
  "created_at": "2024-03-07T06:10:19.480291Z"
}