models.py 2.04 KB
Newer Older
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
from django.db import models
from django.core.validators import RegexValidator, URLValidator


class Partner(models.Model):
    is_active = models.BooleanField(default=False)
    is_main_partner = models.BooleanField(default=False)
    name = models.CharField(max_length=255)
    slug = models.SlugField()
    link = models.CharField(
        max_length=255,
        blank=True,
        validators=[URLValidator()]
    )
    company_profile = models.TextField(max_length=3072, blank=True)
    logo = models.ImageField(upload_to='public/partners/logos/')
    site_header = models.ImageField(
        upload_to='public/partners/headers/',
        null=True,
        blank=True
    )

    address = models.CharField(max_length=100, validators=[
        RegexValidator(
            regex=(r'^([1-9][e][\s])*([ëéÉËa-zA-Z]'
                   '+(([\.][\s])|([\s]))?)+[1-9][0-9]'
                   '*(([-][1-9][0-9]*)|([\s]?[ëéÉËa-zA-Z]+))?$'),
            message='Enter a valid address')
    ])
    zip_code = models.CharField(max_length=12, validators=[
        RegexValidator(
            regex=r'^[1-9][0-9]{3}[\s]?[A-Za-z]{2}$',
            message='Enter a valid zip code'
        )
    ])
    city = models.CharField(max_length=100)

    def save(self, *args, **kwargs):
        if self.is_main_partner:
            self._reset_main_partner()

        super(Partner, self).save(*args, **kwargs)

    def _reset_main_partner(self):
        try:
            current_main_partner = Partner.objects.get(is_main_partner=True)
            if self != current_main_partner:
                current_main_partner.is_main_partner = False
                current_main_partner.save()
        except Partner.DoesNotExist:
            pass

    def __str__(self):
        return self.name


class PartnerImage(models.Model):
    partner = models.ForeignKey(
        Partner,
        on_delete=models.CASCADE,
        related_name="images"
    )
Luuk Scholten's avatar
Luuk Scholten committed
63
    image = models.ImageField(upload_to='public/partners/images/')
64
65
66

    def __str__(self):
        return 'image of {}'.format(self.partner.name)