-
Notifications
You must be signed in to change notification settings - Fork 43
/
models.py
82 lines (59 loc) · 3.02 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
import os
import hashlib
from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth.models import AbstractUser, Group
from django.contrib.postgres.fields import ArrayField
from django.template.loader import get_template
from rules.models import YaraRule
class User(AbstractUser):
def save(self, *args, **kwargs):
# Ensure new user name does not conflict with already existing group
if self._state.adding == True:
if Group.objects.filter(name=self.username).count():
raise ValidationError('Username already taken')
super(User, self).save(*args, **kwargs)
class GroupMeta(models.Model):
group = models.OneToOneField(Group, on_delete=models.CASCADE, primary_key=True)
owner = models.ForeignKey(User, related_name="group_owner", on_delete=models.CASCADE)
admins = models.ManyToManyField(User)
source_required = models.BooleanField(default=True)
source_options = ArrayField(models.CharField(max_length=75), default=list)
category_required = models.BooleanField(default=True)
category_options = ArrayField(models.CharField(max_length=75), default=list)
nonprivileged_submission_status = models.CharField(max_length=75,
choices=YaraRule.STATUS_CHOICES,
default=YaraRule.PENDING_STATUS)
def save(self, *args, **kwargs):
self.source_options = list(set(self.source_options))
self.category_options = list(set(self.category_options))
super(GroupMeta, self).save(*args, **kwargs)
class RegistrationToken(models.Model):
def generate_token():
return hashlib.sha256(os.urandom(4096)).hexdigest()
email = models.EmailField(unique=True)
token = models.CharField(max_length=64, default=generate_token)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True, editable=False)
def save(self, *args, **kwargs):
subject = 'Registration'
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [self.email]
# Grab templates
plaintext_template = get_template('emails/RegistrationEmail.txt')
html_template = get_template('emails/RegistrationEmail.html')
# Specify context data
template_context = {'token': self.token,
'email': self.email}
# Render templates with context data
text_message = plaintext_template.render(template_context)
html_message = html_template.render(template_context)
message = EmailMultiAlternatives(subject,
text_message,
from_email,
recipient_list)
message.attach_alternative(html_message, "text/html")
message.send()
super(RegistrationToken, self).save(*args, **kwargs)