annotate gpp/core/models.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children f3ad863505bf
rev   line source
gremmie@1 1 """
gremmie@1 2 This file contains the core Models used in gpp.
gremmie@1 3 The SiteConfig information is cached in a similar manner to django.contrib.sites.
gremmie@1 4 """
gremmie@1 5
gremmie@1 6 from django.db import models
gremmie@1 7
gremmie@1 8 SITE_CACHE = {}
gremmie@1 9
gremmie@1 10 class SiteConfigManager(models.Manager):
gremmie@1 11 def get_current(self):
gremmie@1 12 """
gremmie@1 13 Returns the current SiteConfig based on the SITE_ID in the
gremmie@1 14 project's settings. The SiteConfig object is cached the first
gremmie@1 15 time it's retrieved from the database.
gremmie@1 16 """
gremmie@1 17 from django.conf import settings
gremmie@1 18 try:
gremmie@1 19 sid = settings.SITE_ID
gremmie@1 20 except AttributeError:
gremmie@1 21 from django.core.exceptions import ImproperlyConfigured
gremmie@1 22 raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. " +
gremmie@1 23 "Create a site in your database and set the SITE_ID setting to fix this error.")
gremmie@1 24 try:
gremmie@1 25 current_site_config = SITE_CACHE[sid]
gremmie@1 26 except KeyError:
gremmie@1 27 current_site_config = self.get(pk = sid)
gremmie@1 28 SITE_CACHE[sid] = current_site_config
gremmie@1 29 return current_site_config
gremmie@1 30
gremmie@1 31
gremmie@1 32 def clear_cache(self):
gremmie@1 33 """Clears the SiteConfig object cache."""
gremmie@1 34 global SITE_CACHE
gremmie@1 35 SITE_CACHE = {}
gremmie@1 36
gremmie@1 37
gremmie@1 38 class SiteConfig(models.Model):
gremmie@1 39 """model to represent the site's basic configuration and settings"""
gremmie@1 40 site_slogan = models.CharField(max_length = 128, blank = True)
gremmie@1 41 admin_name = models.CharField(max_length = 64)
gremmie@1 42 admin_email = models.EmailField()
gremmie@1 43 date_created = models.DateField()
gremmie@1 44
gremmie@1 45 objects = SiteConfigManager()
gremmie@1 46
gremmie@1 47
gremmie@1 48 def __unicode__(self):
gremmie@1 49 return u'SiteConfig ' + unicode(self.id)
gremmie@1 50
gremmie@1 51
gremmie@1 52 def delete(self):
gremmie@1 53 pk = self.pk
gremmie@1 54 super(SiteConfig, self).delete()
gremmie@1 55 try:
gremmie@1 56 del(SITE_CACHE[pk])
gremmie@1 57 except KeyError:
gremmie@1 58 pass
gremmie@1 59
gremmie@1 60
gremmie@1 61 class DebugLog(models.Model):
gremmie@1 62 '''Model to represent debug logs used during development; arbitary text can be stored'''
gremmie@1 63
gremmie@1 64 LOG_LEVELS = (
gremmie@1 65 (0, 'Not Set'),
gremmie@1 66 (10, 'Debug'),
gremmie@1 67 (20, 'Info'),
gremmie@1 68 (30, 'Warning'),
gremmie@1 69 (40, 'Error'),
gremmie@1 70 (50, 'Critical'),
gremmie@1 71 )
gremmie@1 72
gremmie@1 73 timestamp = models.DateTimeField(auto_now_add = True)
gremmie@1 74 level = models.IntegerField(choices = LOG_LEVELS)
gremmie@1 75 msg = models.TextField()
gremmie@1 76
gremmie@1 77 def __unicode__(self):
gremmie@1 78 return '%s - %s' % (self.timestamp.strftime('%m/%d/%Y %H:%M:%S'),
gremmie@1 79 self.msg[:64])
gremmie@1 80
gremmie@1 81 class Meta:
gremmie@1 82 ordering = ('-timestamp', )