-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
111 lines (85 loc) · 4.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.conf import settings
from django.contrib.auth.models import User as BaseUser
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
class User(models.Model):
base = models.OneToOneField(BaseUser, on_delete=models.CASCADE)
apiKey = models.CharField(max_length=64, blank=True, verbose_name="API key", help_text="Leave blank to regenerate.")
def save(self, *args, **kwargs):
import hashlib
import time
if self.apiKey == "":
now = time.ctime().encode()
self.apiKey = hashlib.sha256(now).hexdigest()
super().save(*args, **kwargs)
class AuthKeys(models.Model):
user = models.ForeignKey(BaseUser, on_delete=models.CASCADE)
key = models.CharField(max_length=64)
expires = models.DateTimeField(auto_now=False, auto_now_add=False)
class UpdaterBins(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
description = models.CharField(max_length=160, help_text="Use to record e.g. the git tag+hash the zips were built from.")
winZip = models.FileField(upload_to="epu/zips/", verbose_name="Windows zip")
macZip = models.FileField(upload_to="epu/zips/", verbose_name="macOS zip")
linuxZip = models.FileField(upload_to="epu/zips/", verbose_name="Linux zip")
class Meta:
verbose_name = "Client Binary"
verbose_name_plural = "Client Binaries"
def __str__(self):
return "Binaries created {} ({})".format(self.created.strftime("%Y-%m-%d/%H:%M"), self.description)
def save(self, *args, **kwargs):
try:
old = UpdaterBins.objects.get(id=self.id)
if old.winZip != self.winZip:
os.remove(old.winZip.path)
if old.macZip != self.macZip:
os.remove(old.macZip.path)
if old.linuxZip != self.linuxZip:
os.remove(old.linuxZip.path)
except UpdaterBins.DoesNotExist:
pass
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
os.remove(self.winZip.path)
os.remove(self.macZip.path)
os.remove(self.linuxZip.path)
super().delete(*args, **kwargs)
class Pack(models.Model):
name = models.CharField(max_length=1024, verbose_name="Display name")
slug = models.CharField(max_length=256, verbose_name="URL-friendly name")
gitURL = models.CharField(max_length=1024, verbose_name="Git repository")
instanceZip = models.FileField(upload_to="epu/zips/", verbose_name="Instance zip")
updaterBinaries = models.ForeignKey(UpdaterBins, null=True, on_delete=models.SET_NULL, verbose_name="Client binaries")
icon = models.FilePathField(path=os.path.join(settings.STATIC_ROOT, "epu/icons"), recursive=False, default="infinity.png")
def __str__(self):
return self.name
def icon_filename(self):
return self.icon.replace(settings.STATIC_ROOT, "")
def save(self, *args, **kwargs):
try:
old = Pack.objects.get(id=self.id)
if old.instanceZip != self.instanceZip:
os.remove(old.instanceZip.path)
except Pack.DoesNotExist:
pass
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
os.remove(self.instanceZip.path)
super().delete(*args, **kwargs)
@receiver(pre_save, sender=BaseUser)
def baseUserPreSave(sender, **kwargs):
base = kwargs["instance"]
if len(base.password) > 0:
# effectively disable any use of password auth, e.g. login prompt in admin
base.password = ""
@receiver(post_save, sender=BaseUser)
def baseUserPostSave(sender, **kwargs):
base = kwargs["instance"]
if not User.objects.filter(base__pk=base.id).exists():
# ensure newly-created users have an API key generated for them
User(base=base).save()