|
@@ -1,18 +1,46 @@
|
|
-from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
|
from django.db import models
|
|
-from django.template.defaultfilters import slugify
|
|
|
|
from django.utils import timezone
|
|
from django.utils import timezone
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
from misago.conf import settings
|
|
from misago.conf import settings
|
|
|
|
+from misago.core.cache import cache
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+CACHE_KEY = 'agreements'
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class AgreementManager(models.Manager):
|
|
|
|
+ def invalidate_cache(self):
|
|
|
|
+ cache.delete(CACHE_KEY)
|
|
|
|
+
|
|
|
|
+ def get_agreements(self):
|
|
|
|
+ agreements = self.get_agreements_from_cache()
|
|
|
|
+ if agreements == 'nada':
|
|
|
|
+ agreements = self.get_agreements_from_db()
|
|
|
|
+ cache.set(CACHE_KEY, agreements)
|
|
|
|
+ return agreements
|
|
|
|
+
|
|
|
|
+ def get_agreements_from_cache(self):
|
|
|
|
+ return cache.get(CACHE_KEY, 'nada')
|
|
|
|
+
|
|
|
|
+ def get_agreements_from_db(self):
|
|
|
|
+ agreements = {}
|
|
|
|
+ for agreement in Agreement.objects.filter('is_active'):
|
|
|
|
+ agreements[agreement.type] = {
|
|
|
|
+ 'type': agreement.type,
|
|
|
|
+ 'title': agreement.title,
|
|
|
|
+ 'link': agreement.link,
|
|
|
|
+ 'text': bool(agreement.text),
|
|
|
|
+ }
|
|
|
|
+ return agreements
|
|
|
|
|
|
|
|
|
|
class Agreement(models.Model):
|
|
class Agreement(models.Model):
|
|
- TYPE_TOS = 'terms-of-service'
|
|
|
|
- TYPE_PRIVACY = 'privacy-policy'
|
|
|
|
|
|
+ TYPE_TOS = 'terms_of_service'
|
|
|
|
+ TYPE_PRIVACY = 'privacy_policy'
|
|
TYPE_CHOICES = [
|
|
TYPE_CHOICES = [
|
|
(TYPE_TOS, _('Terms of service')),
|
|
(TYPE_TOS, _('Terms of service')),
|
|
- (TYPE_PRIVACY, _('Privacy Policy')),
|
|
|
|
|
|
+ (TYPE_PRIVACY, _('Privacy policy')),
|
|
]
|
|
]
|
|
|
|
|
|
type = models.CharField(
|
|
type = models.CharField(
|
|
@@ -21,9 +49,9 @@ class Agreement(models.Model):
|
|
choices=TYPE_CHOICES,
|
|
choices=TYPE_CHOICES,
|
|
db_index=True,
|
|
db_index=True,
|
|
)
|
|
)
|
|
- version = models.SlugField(unique=True, blank=True)
|
|
|
|
- title = models.CharField(max_length=255)
|
|
|
|
- text = models.TextField()
|
|
|
|
|
|
+ title = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
+ link = models.URLField(max_length=255, null=True, blank=True)
|
|
|
|
+ text = models.TextField(null=True, blank=True)
|
|
is_active = models.BooleanField(default=False)
|
|
is_active = models.BooleanField(default=False)
|
|
created_on = models.DateTimeField(default=timezone.now)
|
|
created_on = models.DateTimeField(default=timezone.now)
|
|
created_by = models.ForeignKey(
|
|
created_by = models.ForeignKey(
|
|
@@ -42,6 +70,19 @@ class Agreement(models.Model):
|
|
)
|
|
)
|
|
last_modified_by_name = models.CharField(max_length=255, null=True, blank=True)
|
|
last_modified_by_name = models.CharField(max_length=255, null=True, blank=True)
|
|
|
|
|
|
|
|
+ objects = AgreementManager()
|
|
|
|
+
|
|
|
|
+ def get_final_title(self):
|
|
|
|
+ return self.title or self.get_type_display()
|
|
|
|
+
|
|
|
|
+ def set_created_by(self, user):
|
|
|
|
+ self.created_by = user
|
|
|
|
+ self.created_by_name = user.username
|
|
|
|
+
|
|
|
|
+ def set_last_modified_by(self, user):
|
|
|
|
+ self.last_modified_by = user
|
|
|
|
+ self.last_modified_by_name = user.username
|
|
|
|
+
|
|
|
|
|
|
class UserAgreement(models.Model):
|
|
class UserAgreement(models.Model):
|
|
user = models.ForeignKey(
|
|
user = models.ForeignKey(
|