|
1 | | -# Create your models here. |
| 1 | +import logging |
| 2 | +from django.conf import settings |
| 3 | +from django.db import models |
| 4 | +import httpx |
| 5 | +from model_utils.models import TimeStampedModel |
| 6 | +from colorfield.fields import ColorField |
| 7 | +from django.db import transaction |
| 8 | + |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class Community(TimeStampedModel): |
| 14 | + name = models.CharField(max_length=300) |
| 15 | + hostname = models.CharField(max_length=256) |
| 16 | + description = models.TextField() |
| 17 | + |
| 18 | + landing_page_primary_color = ColorField( |
| 19 | + blank=True, |
| 20 | + null=True, |
| 21 | + help_text=( |
| 22 | + "Used for the background of the landing page. " |
| 23 | + "Depending on the contrast, it will be used to decide if " |
| 24 | + "the text should be white or black." |
| 25 | + ), |
| 26 | + ) |
| 27 | + landing_page_secondary_color = ColorField( |
| 28 | + blank=True, |
| 29 | + null=True, |
| 30 | + help_text=( |
| 31 | + "Used for the logo color, borders " |
| 32 | + "and, if not specified, the hover effect of the links." |
| 33 | + ), |
| 34 | + ) |
| 35 | + landing_page_hover_color = ColorField( |
| 36 | + blank=True, |
| 37 | + null=True, |
| 38 | + help_text="Optional. Used when hovering the links. If empty, it will use the secondary color.", |
| 39 | + ) |
| 40 | + landing_page_custom_logo_svg = models.TextField( |
| 41 | + blank=True, |
| 42 | + null=True, |
| 43 | + help_text=( |
| 44 | + "Optional. If empty, it will use the Python Italia logo with the community name below. " |
| 45 | + "Copy your SVG code here." |
| 46 | + ), |
| 47 | + ) |
| 48 | + |
| 49 | + def save(self, *args, **kwargs): |
| 50 | + super().save(*args, **kwargs) |
| 51 | + transaction.on_commit(self.revalidate_landing_page) |
| 52 | + |
| 53 | + def revalidate_landing_page(self): |
| 54 | + # this is very bad :) |
| 55 | + try: |
| 56 | + for _ in range(3): |
| 57 | + response = httpx.post( |
| 58 | + f"https://{self.hostname}/api/revalidate/", |
| 59 | + json={ |
| 60 | + "path": f"/{self.hostname}", |
| 61 | + "secret": settings.REVALIDATE_SECRET, |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + if response.status_code == 200: |
| 66 | + break |
| 67 | + |
| 68 | + response.raise_for_status() |
| 69 | + except Exception as e: |
| 70 | + logger.exception("Error while revalidating landing page", exc_info=e) |
| 71 | + pass |
| 72 | + |
| 73 | + def __str__(self) -> str: |
| 74 | + return self.name |
| 75 | + |
| 76 | + class Meta: |
| 77 | + verbose_name_plural = "Communities" |
| 78 | + |
| 79 | + |
| 80 | +class CommunityMember(TimeStampedModel): |
| 81 | + class Role(models.TextChoices): |
| 82 | + ADMIN = "ADMIN" |
| 83 | + |
| 84 | + user = models.ForeignKey( |
| 85 | + "users.User", on_delete=models.CASCADE, related_name="community_memberships" |
| 86 | + ) |
| 87 | + community = models.ForeignKey( |
| 88 | + Community, on_delete=models.CASCADE, related_name="members" |
| 89 | + ) |
| 90 | + role = models.CharField(choices=Role.choices, default=Role.ADMIN, max_length=300) |
| 91 | + |
| 92 | + |
| 93 | +class Link(TimeStampedModel): |
| 94 | + community = models.ForeignKey( |
| 95 | + Community, on_delete=models.CASCADE, related_name="links" |
| 96 | + ) |
| 97 | + url = models.URLField() |
| 98 | + label = models.CharField(max_length=300) |
| 99 | + |
| 100 | + def __str__(self) -> str: |
| 101 | + return f"{self.label} - {self.url}" |
0 commit comments