mediacms/actions/models.py

53 lines
1.4 KiB
Python
Raw Normal View History

2020-12-15 22:33:43 +01:00
from django.db import models
2021-05-26 17:35:21 +02:00
2020-12-15 22:33:43 +01:00
from files.models import Media
2021-05-26 17:35:21 +02:00
from users.models import User
2020-12-15 22:33:43 +01:00
USER_MEDIA_ACTIONS = (
("like", "Like"),
("dislike", "Dislike"),
("watch", "Watch"),
("report", "Report"),
("rate", "Rate"),
)
class MediaAction(models.Model):
"""Stores different user actions"""
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
db_index=True,
blank=True,
null=True,
related_name="useractions",
)
session_key = models.CharField(
max_length=33,
db_index=True,
blank=True,
null=True,
help_text="for not logged in users",
)
2021-05-26 17:35:21 +02:00
action = models.CharField(max_length=20, choices=USER_MEDIA_ACTIONS, default="watch")
2020-12-15 22:33:43 +01:00
# keeps extra info, eg on report action, why it is reported
extra_info = models.TextField(blank=True, null=True)
2021-05-26 17:35:21 +02:00
media = models.ForeignKey(Media, on_delete=models.CASCADE, related_name="mediaactions")
2020-12-15 22:33:43 +01:00
action_date = models.DateTimeField(auto_now_add=True)
remote_ip = models.CharField(max_length=40, blank=True, null=True)
def save(self, *args, **kwargs):
super(MediaAction, self).save(*args, **kwargs)
def __str__(self):
return self.action
class Meta:
indexes = [
models.Index(fields=["user", "action", "-action_date"]),
models.Index(fields=["session_key", "action"]),
]