Skip to content

Commit

Permalink
feat(emojis): added emojis (#553)
Browse files Browse the repository at this point in the history
* added emoji dependency

* created emoji app :feelsgood:

* removed unused files

* added emoji models, serializers and views

* added urls for emoji app :godmode:

* fixed models and added basic admin

* added view for unicode emojis

* ever closer to the end :shipit:

* fixed emoji serializers

* fixed model admin

* is ready? :shipit: :feelsgood:

* added comment for empty method

* removed type from str in CustomShortName

* ran make pr :shipit:

* Started developing UserNewsReaction

* Finished model and serializer. Created factory and started testing

* Added pytest.fixture for emoji and added it to test

* Finished tests for UserNewsReaction

* Removed unsused code and comments

* Removed line spacings

* ready for merging main into this branch

* fixed linting

* Changed read_access on reactions to only be members

* made news_emoji model, serializer and viewset

* added new tests and created a news-emoji factory

* Added realated names to news emojis and reactions

* Fixed some linting

* Added related name to reactions in news

* Added tests for both news reactions and toddel reactions and modified reactions so that they have to be allowed on a specific news or toddel

* Fixed test failures and added url support to reactions for toddel

* Changed changelog

* Removed potential cross-site scripting

* Removed remainding  potential cross-site scripting

* Refactured emoji backend and implemented reactions for unicode emojis

* Added a endpoint for retrieving if emojis is allowed on a news

* Cleaned up some code

* Added forgot import

* Fixed a bug

* Simplified the feature

* Added model, serializer and viewset for model News Reaction

* Made reactions use generic foreignkeys and added support for reactions to news

* Made tests for post and put a reaction on news and modyfied it so they pass

* Removed commented out code

* Fixed some bugs where content_type was not saved

* Modified the Reaction serializer and made a create and update serializer

* Cleaned up init files and simplyfied migrations

* Fixed conflict after merging dev

* Made enum for content types and removed unused code

* Updated update serializer to use the update method instead of create

* Modified tests and fixed update for reaction

* Made requested changes and added a test for changing other users reactions and made it pass

* Made requested changes

* Linting error from dev

* Fixed error from merging of dev

* Added a check so that a user can't create a reaction for another user

* cleanup

* fixed migration error

* Added reactions to event

* Removed unintential code

* added emojis_allowed for update and create serializer for event

* Removed unused code and linted

* added user information to reaction serializer

---------

Co-authored-by: Harry Linrui Xu <[email protected]>
Co-authored-by: Harry Linrui Xu <[email protected]>
Co-authored-by: Tobias Rødahl Thingnes <[email protected]>
Co-authored-by: Jarand Romestrand <[email protected]>
Co-authored-by: Tini Tran <[email protected]>
Co-authored-by: JarandJR <[email protected]>
Co-authored-by: Mads Nylund <[email protected]>
Co-authored-by: Mads Nylund <[email protected]>
  • Loading branch information
9 people authored Dec 14, 2023
1 parent b9af21f commit 0c7853f
Show file tree
Hide file tree
Showing 35 changed files with 589 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
## Versjon 2023.10.23
-**Brukere** HS kan styre medlemmer
-**Bannere** Filtrering på bannere
-**emoji** Nyheter kan nå ha reaksjoner
-**Spørreskjemaer** NOK medlemmer kan lage spørreskjema.
-**Bruker** Nå kan ikke HS lenger endre eller slette brukere.
-**Mails** Nå logger vi på eposttjeneren kun en gang per batch med epost som sendes.
Expand Down
1 change: 1 addition & 0 deletions app/content/factories/event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ class Meta:
start_registration_at = timezone.now() - timedelta(days=1)
end_registration_at = timezone.now() + timedelta(days=9)
sign_off_deadline = timezone.now() + timedelta(days=8)
emojis_allowed = True
1 change: 1 addition & 0 deletions app/content/factories/news_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ class Meta:
title = factory.Faker("sentence", nb_words=5)
header = factory.Faker("sentence", nb_words=5)
body = factory.Faker("paragraph", nb_sentences=10)
emojis_allowed = True
18 changes: 18 additions & 0 deletions app/content/migrations/0054_news_emojis_allowed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-10-17 22:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("content", "0053_event_contact_person"),
]

operations = [
migrations.AddField(
model_name="news",
name="emojis_allowed",
field=models.BooleanField(default=False),
),
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-10-08 20:45
# Generated by Django 4.2.5 on 2023-10-28 14:34

from django.conf import settings
from django.db import migrations, models
Expand All @@ -8,7 +8,7 @@
class Migration(migrations.Migration):

dependencies = [
("content", "0053_event_contact_person"),
("content", "0054_news_emojis_allowed"),
]

operations = [
Expand All @@ -29,7 +29,7 @@ class Migration(migrations.Migration):
("image", models.URLField(blank=True, max_length=600, null=True)),
("image_alt", models.CharField(blank=True, max_length=200, null=True)),
("name", models.CharField(max_length=50)),
("url", models.URLField(max_length=600)),
("content", models.CharField(max_length=600)),
(
"user",
models.ForeignKey(
Expand Down
23 changes: 0 additions & 23 deletions app/content/migrations/0055_remove_qrcode_url_qrcode_content.py

This file was deleted.

4 changes: 2 additions & 2 deletions app/content/migrations/0056_registration_created_by_admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Generated by Django 4.2.5 on 2023-11-04 10:16
# Generated by Django 4.2.5 on 2023-11-15 23:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("content", "0055_remove_qrcode_url_qrcode_content"),
("content", "0055_qrcode"),
]

operations = [
Expand Down
18 changes: 18 additions & 0 deletions app/content/migrations/0057_event_emojis_allowed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-11-16 20:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("content", "0056_registration_created_by_admin"),
]

operations = [
migrations.AddField(
model_name="event",
name="emojis_allowed",
field=models.BooleanField(default=False),
),
]
6 changes: 6 additions & 0 deletions app/content/models/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import timedelta

from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from django.db import models

Expand All @@ -11,6 +12,7 @@
)
from app.content.models import Category
from app.content.models.user import User
from app.emoji.models.reaction import Reaction
from app.forms.enums import EventFormType
from app.group.models.group import Group
from app.util.models import BaseModel, OptionalImage
Expand Down Expand Up @@ -77,6 +79,10 @@ class Event(BaseModel, OptionalImage, BasePermissionModel):
runned_sign_off_deadline_reminder = models.BooleanField(default=False)
runned_sign_up_start_notifier = models.BooleanField(default=False)

""" Reactions """
emojis_allowed = models.BooleanField(default=False)
reactions = GenericRelation(Reaction)

class Meta:
ordering = ("start_date",)

Expand Down
4 changes: 4 additions & 0 deletions app/content/models/news.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models

from app.common.enums import AdminGroup, Groups
from app.common.permissions import BasePermissionModel
from app.emoji.models.reaction import Reaction
from app.util.models import BaseModel, OptionalImage


Expand All @@ -17,6 +19,8 @@ class News(BaseModel, OptionalImage, BasePermissionModel):
related_name="created_news",
)
body = models.TextField()
emojis_allowed = models.BooleanField(default=False)
reactions = GenericRelation(Reaction)

write_access = [*AdminGroup.all(), Groups.FONDET]

Expand Down
5 changes: 5 additions & 0 deletions app/content/serializers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PriorityPoolSerializer,
)
from app.content.serializers.user import DefaultUserSerializer
from app.emoji.serializers.reaction import ReactionSerializer
from app.group.models.group import Group
from app.group.serializers.group import SimpleGroupSerializer
from app.payment.models.paid_event import PaidEvent
Expand All @@ -29,6 +30,7 @@ class EventSerializer(serializers.ModelSerializer):
required=False, allow_null=True
)
contact_person = DefaultUserSerializer(read_only=True, required=False)
reactions = ReactionSerializer(required=False, many=True)

class Meta:
model = Event
Expand Down Expand Up @@ -63,6 +65,8 @@ class Meta:
"paid_information",
"is_paid_event",
"contact_person",
"reactions",
"emojis_allowed",
)

def get_paid_information(self, obj):
Expand Down Expand Up @@ -150,6 +154,7 @@ class Meta:
"paid_information",
"is_paid_event",
"contact_person",
"emojis_allowed",
)

def to_internal_value(self, data):
Expand Down
4 changes: 4 additions & 0 deletions app/content/serializers/news.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app.common.serializers import BaseModelSerializer
from app.content.models import News, User
from app.content.serializers.user import DefaultUserSerializer
from app.emoji.serializers.reaction import ReactionSerializer


class SimpleNewsSerializer(BaseModelSerializer):
Expand All @@ -19,12 +20,15 @@ class Meta:

class NewsSerializer(SimpleNewsSerializer):
creator = DefaultUserSerializer(read_only=True)
reactions = ReactionSerializer(required=False, many=True)

class Meta:
model = SimpleNewsSerializer.Meta.model
fields = SimpleNewsSerializer.Meta.fields + (
"creator",
"body",
"reactions",
"emojis_allowed",
)

def create(self, validated_data):
Expand Down
10 changes: 10 additions & 0 deletions app/content/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ class Meta(UserSerializer.Meta):
)


class SimpleUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
"user_id",
"first_name",
"last_name",
)


class UserCreateSerializer(serializers.ModelSerializer):
study = serializers.SlugRelatedField(
slug_field="slug",
Expand Down
Empty file added app/emoji/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions app/emoji/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin

from app.emoji.models.reaction import Reaction


@admin.register(Reaction)
class ReactionAdmin(admin.ModelAdmin):
pass
5 changes: 5 additions & 0 deletions app/emoji/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class EmojiConfig(AppConfig):
name = "app.emoji"
6 changes: 6 additions & 0 deletions app/emoji/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.db import models


class ContentTypes(models.TextChoices):
NEWS = "news"
EVENT = "event"
17 changes: 17 additions & 0 deletions app/emoji/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import status
from rest_framework.exceptions import APIException


class APIReactionNotAllowedException(APIException):
status_code = status.HTTP_403_FORBIDDEN
default_detail = "Reaksjoner er ikke tillatt her"


class APIContentTypeNotSupportedException(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = "Er ikke støtte for denne typen"


class APIReactionDuplicateNotAllowedException(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = "Du har allerede reagert her"
Empty file added app/emoji/factories/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions app/emoji/factories/reaction_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.contrib.contenttypes.models import ContentType

import factory
from factory.django import DjangoModelFactory

from app.content.factories.event_factory import EventFactory
from app.content.factories.news_factory import NewsFactory
from app.content.factories.user_factory import UserFactory
from app.content.models.event import Event
from app.content.models.news import News
from app.emoji.models.reaction import Reaction


class NewsReactionFactory(DjangoModelFactory):
class Meta:
model = Reaction

emoji = factory.Faker("emoji")
user = factory.SubFactory(UserFactory)
content_object = factory.SubFactory(NewsFactory)

@factory.lazy_attribute
def content_type(self):
return ContentType.objects.get_for_model(News)


class EventReactionFactory(DjangoModelFactory):
class Meta:
model = Reaction

emoji = factory.Faker("emoji")
user = factory.SubFactory(UserFactory)
content_object = factory.SubFactory(EventFactory)

@factory.lazy_attribute
def content_type(self):
return ContentType.objects.get_for_model(Event)
53 changes: 53 additions & 0 deletions app/emoji/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.2.5 on 2023-11-18 16:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Reaction",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"reaction_id",
models.AutoField(editable=False, primary_key=True, serialize=False),
),
("emoji", models.CharField(max_length=60)),
("object_id", models.PositiveIntegerField(blank=True, null=True)),
(
"content_type",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="reactions",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"verbose_name": "Reaction",
"verbose_name_plural": "Reactions",
"unique_together": {("user", "object_id", "content_type")},
},
),
]
Empty file.
Empty file added app/emoji/models/__init__.py
Empty file.
Loading

0 comments on commit 0c7853f

Please sign in to comment.