From 27b9326dd95ad1939b9fc431cb707a6e6f8d13bd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 18 Feb 2020 23:26:42 -0800 Subject: [PATCH] Adds Favorite table works on #28 --- .../migrations/0002_auto_20200219_0725.py | 39 +++++++++++++++++++ fedireads/models/activity.py | 13 +++++++ fedireads/models/user.py | 7 ++++ 3 files changed, 59 insertions(+) create mode 100644 fedireads/migrations/0002_auto_20200219_0725.py diff --git a/fedireads/migrations/0002_auto_20200219_0725.py b/fedireads/migrations/0002_auto_20200219_0725.py new file mode 100644 index 00000000..93076b0f --- /dev/null +++ b/fedireads/migrations/0002_auto_20200219_0725.py @@ -0,0 +1,39 @@ +# Generated by Django 3.0.3 on 2020-02-19 07:25 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Favorite', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField(blank=True, null=True)), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('relationship_id', models.CharField(max_length=100)), + ('status', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Status')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'abstract': False, + }, + ), + migrations.AddField( + model_name='status', + name='favorites', + field=models.ManyToManyField(related_name='user_favorites', through='fedireads.Favorite', to=settings.AUTH_USER_MODEL), + ), + migrations.AddField( + model_name='user', + name='favorites', + field=models.ManyToManyField(related_name='favorite_statuses', through='fedireads.Favorite', to='fedireads.Status'), + ), + ] diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index 8cbceea6..d8245472 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -16,6 +16,13 @@ class Status(FedireadsModel): local = models.BooleanField(default=True) privacy = models.CharField(max_length=255, default='public') sensitive = models.BooleanField(default=False) + favorites = models.ManyToManyField( + 'User', + symmetrical=False, + through='Favorite', + through_fields=('status', 'user'), + related_name='user_favorites' + ) reply_parent = models.ForeignKey( 'self', null=True, @@ -38,3 +45,9 @@ class Review(Status): self.activity_type = 'Article' super().save(*args, **kwargs) + +class Favorite(FedireadsModel): + ''' fav'ing a post ''' + user = models.ForeignKey('User', on_delete=models.PROTECT) + status = models.ForeignKey('Status', on_delete=models.PROTECT) + relationship_id = models.CharField(max_length=100) diff --git a/fedireads/models/user.py b/fedireads/models/user.py index a8911c0d..6f277f9a 100644 --- a/fedireads/models/user.py +++ b/fedireads/models/user.py @@ -40,6 +40,13 @@ class User(AbstractUser): through='UserRelationship', through_fields=('user_subject', 'user_object') ) + favorites = models.ManyToManyField( + 'Status', + symmetrical=False, + through='Favorite', + through_fields=('user', 'status'), + related_name='favorite_statuses' + ) @property def absolute_id(self):