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