2022-07-21 12:48:18 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import stripe
|
2023-01-14 16:36:00 +00:00
|
|
|
from django.conf import settings
|
2022-07-21 12:45:44 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
|
from django.db import models
|
2023-01-12 07:20:48 +00:00
|
|
|
from yaml import load
|
|
|
|
from yaml.parser import ParserError
|
|
|
|
from yaml.scanner import ScannerError
|
2022-07-21 12:45:44 +00:00
|
|
|
|
2022-07-21 12:48:14 +00:00
|
|
|
from core.lib.customers import get_or_create, update_customer_fields
|
2022-07-21 12:45:44 +00:00
|
|
|
|
2023-01-12 07:20:48 +00:00
|
|
|
try:
|
|
|
|
from yaml import CLoader as Loader
|
|
|
|
except ImportError:
|
|
|
|
from yaml import Loader
|
2022-07-21 12:48:18 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-01-12 07:20:48 +00:00
|
|
|
PRIORITY_CHOICES = (
|
|
|
|
(1, "min"),
|
|
|
|
(2, "low"),
|
|
|
|
(3, "default"),
|
|
|
|
(4, "high"),
|
|
|
|
(5, "max"),
|
|
|
|
)
|
|
|
|
|
2023-01-14 14:36:46 +00:00
|
|
|
INTERVAL_CHOICES = (
|
2023-01-14 16:36:00 +00:00
|
|
|
(0, "On demand"),
|
2023-01-15 17:59:12 +00:00
|
|
|
(5, "Every 5 seconds"),
|
2023-01-14 16:36:00 +00:00
|
|
|
(60, "Every minute"),
|
|
|
|
(900, "Every 15 minutes"),
|
|
|
|
(1800, "Every 30 minutes"),
|
|
|
|
(3600, "Every hour"),
|
|
|
|
(14400, "Every 4 hours"),
|
|
|
|
(86400, "Every day"),
|
2023-01-14 14:36:46 +00:00
|
|
|
)
|
|
|
|
|
2023-01-15 18:40:17 +00:00
|
|
|
SERVICE_CHOICES = (
|
|
|
|
("ntfy", "NTFY"),
|
|
|
|
("webhook", "Custom webhook"),
|
|
|
|
)
|
|
|
|
|
2022-07-21 12:45:44 +00:00
|
|
|
|
2022-07-21 12:45:57 +00:00
|
|
|
class Plan(models.Model):
|
|
|
|
name = models.CharField(max_length=255, unique=True)
|
|
|
|
description = models.CharField(max_length=1024, null=True, blank=True)
|
2022-07-21 12:45:44 +00:00
|
|
|
cost = models.IntegerField()
|
2022-07-21 12:48:18 +00:00
|
|
|
product_id = models.CharField(max_length=255, unique=True, null=True, blank=True)
|
2022-07-21 12:45:57 +00:00
|
|
|
image = models.CharField(max_length=1024, null=True, blank=True)
|
2022-07-21 12:45:44 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.name} (£{self.cost})"
|
|
|
|
|
|
|
|
|
|
|
|
class User(AbstractUser):
|
|
|
|
# Stripe customer ID
|
2022-07-21 12:45:57 +00:00
|
|
|
stripe_id = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
last_payment = models.DateTimeField(null=True, blank=True)
|
|
|
|
plans = models.ManyToManyField(Plan, blank=True)
|
2022-07-21 12:48:18 +00:00
|
|
|
email = models.EmailField(unique=True)
|
2022-07-21 12:45:44 +00:00
|
|
|
|
2022-07-21 12:48:18 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._original = self
|
2022-07-21 12:48:14 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Override the save function to create a Stripe customer.
|
|
|
|
"""
|
|
|
|
if not self.stripe_id: # stripe ID not stored
|
|
|
|
self.stripe_id = get_or_create(self.email, self.first_name, self.last_name)
|
|
|
|
|
|
|
|
to_update = {}
|
|
|
|
if self.email != self._original.email:
|
|
|
|
to_update["email"] = self.email
|
|
|
|
if self.first_name != self._original.first_name:
|
|
|
|
to_update["first_name"] = self.first_name
|
|
|
|
if self.last_name != self._original.last_name:
|
|
|
|
to_update["last_name"] = self.last_name
|
|
|
|
|
|
|
|
update_customer_fields(self.stripe_id, **to_update)
|
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2022-07-21 12:48:18 +00:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
if self.stripe_id:
|
|
|
|
stripe.Customer.delete(self.stripe_id)
|
|
|
|
logger.info(f"Deleted Stripe customer {self.stripe_id}")
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
2022-07-21 12:45:44 +00:00
|
|
|
def has_plan(self, plan):
|
|
|
|
plan_list = [plan.name for plan in self.plans.all()]
|
|
|
|
return plan in plan_list
|
2022-07-21 12:48:18 +00:00
|
|
|
|
2023-01-12 07:20:48 +00:00
|
|
|
def get_notification_settings(self):
|
|
|
|
return NotificationSettings.objects.get_or_create(user=self)[0]
|
|
|
|
|
2023-01-14 16:36:00 +00:00
|
|
|
@property
|
|
|
|
def allowed_indices(self):
|
|
|
|
indices = [settings.INDEX_MAIN]
|
|
|
|
if self.has_perm("core.index_meta"):
|
|
|
|
indices.append(settings.INDEX_META)
|
|
|
|
if self.has_perm("core.index_internal"):
|
|
|
|
indices.append(settings.INDEX_INT)
|
|
|
|
if self.has_perm("core.index_restricted"):
|
|
|
|
if self.has_perm("core.restricted_sources"):
|
|
|
|
indices.append(settings.INDEX_RESTRICTED)
|
|
|
|
|
|
|
|
return indices
|
|
|
|
|
2022-07-21 12:48:18 +00:00
|
|
|
|
|
|
|
class Session(models.Model):
|
2022-07-21 12:48:39 +00:00
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
request = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
session = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
subscription_id = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
plan = models.ForeignKey(Plan, null=True, blank=True, on_delete=models.CASCADE)
|
2022-07-21 12:50:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContentBlock(models.Model):
|
|
|
|
user = models.ForeignKey(User, on_delete=models.PROTECT)
|
|
|
|
position = models.IntegerField()
|
2022-07-21 12:50:36 +00:00
|
|
|
page = models.CharField(max_length=255, null=True, blank=True)
|
2022-07-21 12:50:25 +00:00
|
|
|
title = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
column1 = models.TextField(null=True, blank=True)
|
|
|
|
column2 = models.TextField(null=True, blank=True)
|
|
|
|
column3 = models.TextField(null=True, blank=True)
|
|
|
|
image1 = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
image2 = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
image3 = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
|
2022-07-21 12:50:36 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"[{self.position}] {self.page} {self.title}"
|
|
|
|
|
2022-07-21 12:50:25 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Override the save function to blank fields.
|
|
|
|
"""
|
2022-07-21 12:50:36 +00:00
|
|
|
if self.column1 == "":
|
2022-07-21 12:50:25 +00:00
|
|
|
self.column1 = None
|
2022-07-21 12:50:36 +00:00
|
|
|
if self.column2 == "":
|
2022-07-21 12:50:25 +00:00
|
|
|
self.column2 = None
|
2022-07-21 12:50:36 +00:00
|
|
|
if self.column3 == "":
|
2022-07-21 12:50:25 +00:00
|
|
|
self.column3 = None
|
2022-07-21 12:50:36 +00:00
|
|
|
if self.image1 == "":
|
|
|
|
self.image1 = None
|
|
|
|
if self.image2 == "":
|
|
|
|
self.image2 = None
|
|
|
|
if self.image3 == "":
|
|
|
|
self.image3 = None
|
2022-07-21 12:50:25 +00:00
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
2022-08-16 07:58:35 +00:00
|
|
|
|
|
|
|
|
2022-08-16 18:43:55 +00:00
|
|
|
class Perms(models.Model):
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
("post_irc", "Can post to IRC"),
|
|
|
|
("post_discord", "Can post to Discord"),
|
2022-08-26 06:20:30 +00:00
|
|
|
("use_insights", "Can use the Insights page"),
|
2023-01-14 16:36:00 +00:00
|
|
|
("use_rules", "Can use the Rules page"),
|
2022-11-22 07:20:37 +00:00
|
|
|
("index_internal", "Can use the internal index"),
|
2022-08-26 06:20:30 +00:00
|
|
|
("index_meta", "Can use the meta index"),
|
2022-11-22 07:20:37 +00:00
|
|
|
("index_restricted", "Can use the restricted index"),
|
2022-08-26 06:20:30 +00:00
|
|
|
("restricted_sources", "Can access restricted sources"),
|
2022-08-16 18:43:55 +00:00
|
|
|
)
|
2023-01-12 07:20:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotificationRule(models.Model):
|
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
name = models.CharField(max_length=255)
|
2023-01-12 07:20:48 +00:00
|
|
|
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1)
|
2023-01-12 07:20:48 +00:00
|
|
|
topic = models.CharField(max_length=255, null=True, blank=True)
|
2023-01-15 18:40:17 +00:00
|
|
|
url = models.CharField(max_length=1024, null=True, blank=True)
|
2023-01-14 16:36:00 +00:00
|
|
|
interval = models.IntegerField(choices=INTERVAL_CHOICES, default=0)
|
2023-01-14 14:36:46 +00:00
|
|
|
window = models.CharField(max_length=255, null=True, blank=True)
|
2023-01-12 07:20:43 +00:00
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
data = models.TextField()
|
2023-01-15 17:59:12 +00:00
|
|
|
match = models.JSONField(null=True, blank=True)
|
2023-01-15 18:40:17 +00:00
|
|
|
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
|
2023-01-12 07:20:43 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-01-12 07:20:48 +00:00
|
|
|
return f"{self.user} - {self.name}"
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
try:
|
|
|
|
parsed = load(self.data, Loader=Loader)
|
|
|
|
except (ScannerError, ParserError) as e:
|
|
|
|
raise ValueError(f"Invalid YAML: {e}")
|
|
|
|
return parsed
|
2023-01-12 07:20:43 +00:00
|
|
|
|
2023-01-15 18:40:17 +00:00
|
|
|
@property
|
|
|
|
def matches(self):
|
|
|
|
"""
|
|
|
|
Get the total number of matches for this rule.
|
|
|
|
"""
|
|
|
|
if isinstance(self.match, dict):
|
|
|
|
return f"{sum(list(self.match.values()))}/{len(self.match)}"
|
|
|
|
|
2023-01-12 07:20:43 +00:00
|
|
|
|
|
|
|
class NotificationSettings(models.Model):
|
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
2023-01-15 18:40:17 +00:00
|
|
|
topic = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
url = models.CharField(max_length=1024, null=True, blank=True)
|
|
|
|
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
|
2023-01-12 07:20:43 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"Notification settings for {self.user}"
|