forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
67 lines (51 loc) · 2.7 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
from django.db import models
from zerver.models import AbstractPushDeviceToken
from analytics.models import BaseCount
def get_remote_server_by_uuid(uuid: str) -> 'RemoteZulipServer':
return RemoteZulipServer.objects.get(uuid=uuid)
class RemoteZulipServer(models.Model):
UUID_LENGTH = 36
API_KEY_LENGTH = 64
HOSTNAME_MAX_LENGTH = 128
uuid = models.CharField(max_length=UUID_LENGTH, unique=True) # type: str
api_key = models.CharField(max_length=API_KEY_LENGTH) # type: str
hostname = models.CharField(max_length=HOSTNAME_MAX_LENGTH) # type: str
contact_email = models.EmailField(blank=True, null=False) # type: str
last_updated = models.DateTimeField('last updated', auto_now=True) # type: datetime.datetime
def __str__(self) -> str:
return "<RemoteZulipServer %s %s>" % (self.hostname, self.uuid[0:12])
# Variant of PushDeviceToken for a remote server.
class RemotePushDeviceToken(AbstractPushDeviceToken):
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
# The user id on the remote server for this device device this is
user_id = models.BigIntegerField(db_index=True) # type: int
class Meta:
unique_together = ("server", "user_id", "kind", "token")
def __str__(self) -> str:
return "<RemotePushDeviceToken %s %s>" % (self.server, self.user_id)
class RemoteInstallationCount(BaseCount):
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
# The remote_id field lets us deduplicate data from the remote server
remote_id = models.IntegerField(db_index=True) # type: int
class Meta:
unique_together = ("server", "property", "subgroup", "end_time")
index_together = [
["server", "remote_id"],
]
def __str__(self) -> str:
return "<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)
# We can't subclass RealmCount because we only have a realm_id here, not a foreign key.
class RemoteRealmCount(BaseCount):
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
realm_id = models.IntegerField(db_index=True) # type: int
# The remote_id field lets us deduplicate data from the remote server
remote_id = models.IntegerField(db_index=True) # type: int
class Meta:
unique_together = ("server", "realm_id", "property", "subgroup", "end_time")
index_together = [
["property", "end_time"],
["server", "remote_id"],
]
def __str__(self) -> str:
return "%s %s %s %s %s" % (self.server, self.realm_id, self.property, self.subgroup, self.value)