gremmie@1: """ gremmie@1: This file contains the core Models used in gpp. gremmie@1: The SiteConfig information is cached in a similar manner to django.contrib.sites. gremmie@1: """ gremmie@1: gremmie@1: from django.db import models gremmie@1: gremmie@1: SITE_CACHE = {} gremmie@1: gremmie@1: class SiteConfigManager(models.Manager): gremmie@1: def get_current(self): gremmie@1: """ gremmie@1: Returns the current SiteConfig based on the SITE_ID in the gremmie@1: project's settings. The SiteConfig object is cached the first gremmie@1: time it's retrieved from the database. gremmie@1: """ gremmie@1: from django.conf import settings gremmie@1: try: gremmie@1: sid = settings.SITE_ID gremmie@1: except AttributeError: gremmie@1: from django.core.exceptions import ImproperlyConfigured gremmie@1: raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. " + gremmie@1: "Create a site in your database and set the SITE_ID setting to fix this error.") gremmie@1: try: gremmie@1: current_site_config = SITE_CACHE[sid] gremmie@1: except KeyError: gremmie@1: current_site_config = self.get(pk = sid) gremmie@1: SITE_CACHE[sid] = current_site_config gremmie@1: return current_site_config gremmie@1: gremmie@1: gremmie@1: def clear_cache(self): gremmie@1: """Clears the SiteConfig object cache.""" gremmie@1: global SITE_CACHE gremmie@1: SITE_CACHE = {} gremmie@1: gremmie@1: gremmie@1: class SiteConfig(models.Model): gremmie@1: """model to represent the site's basic configuration and settings""" gremmie@1: site_slogan = models.CharField(max_length = 128, blank = True) gremmie@1: admin_name = models.CharField(max_length = 64) gremmie@1: admin_email = models.EmailField() gremmie@1: date_created = models.DateField() gremmie@1: gremmie@1: objects = SiteConfigManager() gremmie@1: gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return u'SiteConfig ' + unicode(self.id) gremmie@1: gremmie@1: gremmie@1: def delete(self): gremmie@1: pk = self.pk gremmie@1: super(SiteConfig, self).delete() gremmie@1: try: gremmie@1: del(SITE_CACHE[pk]) gremmie@1: except KeyError: gremmie@1: pass gremmie@1: gremmie@1: gremmie@1: class DebugLog(models.Model): gremmie@1: '''Model to represent debug logs used during development; arbitary text can be stored''' gremmie@1: gremmie@1: LOG_LEVELS = ( gremmie@1: (0, 'Not Set'), gremmie@1: (10, 'Debug'), gremmie@1: (20, 'Info'), gremmie@1: (30, 'Warning'), gremmie@1: (40, 'Error'), gremmie@1: (50, 'Critical'), gremmie@1: ) gremmie@1: gremmie@1: timestamp = models.DateTimeField(auto_now_add = True) gremmie@1: level = models.IntegerField(choices = LOG_LEVELS) gremmie@1: msg = models.TextField() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return '%s - %s' % (self.timestamp.strftime('%m/%d/%Y %H:%M:%S'), gremmie@1: self.msg[:64]) gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('-timestamp', )