shiftregister/shiftregister/team/models.py

51 lines
1.3 KiB
Python

import secrets
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
from shiftregister.app.models import *
# Create your models here.
class IncomingMessage(models.Model):
class Meta:
get_latest_by = "created_at"
indexes = (models.Index(fields=("sender",)),)
id = models.CharField(max_length=50, primary_key=True)
sender = PhoneNumberField()
content = models.TextField()
created_at = models.DateTimeField()
read = models.BooleanField(default=False)
def from_helper(self):
try:
return Helper.objects.get(phone=self.sender)
except Helper.DoesNotExist:
return None
def display_sender(self):
helper = self.from_helper()
return helper.name if helper else self.sender
def gen_token():
return secrets.token_urlsafe(15)
class RoomViewToken(models.Model):
id = models.CharField(
max_length=20, primary_key=True, default=gen_token, editable=False
)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
def __str__(self):
return f"{self.room.name} {self.id}"
def url(self):
return "https://helfen.kntkt.de" + reverse(
"team:room_view_token",
kwargs={"token": self.id},
)