OpenSlides/server/openslides/chat/models.py

90 lines
2.4 KiB
Python
Raw Normal View History

2020-12-04 06:51:01 +01:00
from django.conf import settings
from django.db import models
from openslides.utils.manager import BaseManager
from ..utils.auth import has_perm, in_some_groups
from ..utils.models import CASCADE_AND_AUTOUPDATE, RESTModelMixin
from .access_permissions import ChatGroupAccessPermissions, ChatMessageAccessPermissions
class ChatGroupManager(BaseManager):
"""
Customized model manager to support our get_prefetched_queryset method.
"""
def get_prefetched_queryset(self, *args, **kwargs):
""""""
return (
super()
.get_prefetched_queryset(*args, **kwargs)
.prefetch_related("read_groups", "write_groups")
2020-12-04 06:51:01 +01:00
)
class ChatGroup(RESTModelMixin, models.Model):
""""""
access_permissions = ChatGroupAccessPermissions()
objects = ChatGroupManager()
name = models.CharField(max_length=256)
read_groups = models.ManyToManyField(
settings.AUTH_GROUP_MODEL, blank=True, related_name="chat_read_groups"
)
write_groups = models.ManyToManyField(
settings.AUTH_GROUP_MODEL, blank=True, related_name="chat_write_groups"
2020-12-04 06:51:01 +01:00
)
class Meta:
default_permissions = ()
permissions = (("can_manage", "Can manage chat"),)
def __str__(self):
return self.name
def can_write(self, user):
2020-12-04 06:51:01 +01:00
if has_perm(user.id, "chat.can_manage"):
return True
return in_some_groups(user.id, self.write_groups.values_list(flat=True))
2020-12-04 06:51:01 +01:00
class ChatMessageManager(BaseManager):
"""
Customized model manager to support our get_prefetched_queryset method.
"""
def get_prefetched_queryset(self, *args, **kwargs):
""""""
return (
super()
.get_prefetched_queryset(*args, **kwargs)
.prefetch_related(
"chatgroup", "chatgroup__read_groups", "chatgroup__write_groups"
)
2020-12-04 06:51:01 +01:00
)
class ChatMessage(RESTModelMixin, models.Model):
""""""
access_permissions = ChatMessageAccessPermissions()
objects = ChatMessageManager()
text = models.CharField(max_length=512)
timestamp = models.DateTimeField(auto_now_add=True)
chatgroup = models.ForeignKey(
ChatGroup, on_delete=CASCADE_AND_AUTOUPDATE, related_name="messages"
)
username = models.CharField(max_length=256)
user_id = models.IntegerField()
2020-12-04 06:51:01 +01:00
class Meta:
default_permissions = ()
def __str__(self):
return f"{self.username} ({self.timestamp}): {self.text}"