From 198470e6d958a2ed8f338bae0a72e88caed7331a Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 22 Oct 2013 15:57:05 +0000 Subject: [PATCH] add db table for app settings --- pandora/app/migrations/0002_settings.py | 46 +++++++++++++++++++++++++ pandora/app/models.py | 29 ++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 pandora/app/migrations/0002_settings.py diff --git a/pandora/app/migrations/0002_settings.py b/pandora/app/migrations/0002_settings.py new file mode 100644 index 00000000..6ca30e89 --- /dev/null +++ b/pandora/app/migrations/0002_settings.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Settings' + db.create_table('app_settings', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), + ('modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)), + ('key', self.gf('django.db.models.fields.CharField')(unique=True, max_length=1024)), + ('value', self.gf('django.db.models.fields.TextField')(blank=True)), + )) + db.send_create_signal('app', ['Settings']) + + + def backwards(self, orm): + # Deleting model 'Settings' + db.delete_table('app_settings') + + + models = { + 'app.page': { + 'Meta': {'object_name': 'Page'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '1024'}), + 'text': ('django.db.models.fields.TextField', [], {'blank': 'True'}) + }, + 'app.settings': { + 'Meta': {'object_name': 'Settings'}, + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '1024'}), + 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'value': ('django.db.models.fields.TextField', [], {'blank': 'True'}) + } + } + + complete_apps = ['app'] \ No newline at end of file diff --git a/pandora/app/models.py b/pandora/app/models.py index d2ac59ed..827372a2 100644 --- a/pandora/app/models.py +++ b/pandora/app/models.py @@ -2,6 +2,8 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division, with_statement +import json + from django.db import models from changelog.models import Changelog @@ -24,3 +26,30 @@ class Page(models.Model): 'text': self.text, } c.save() + +class Settings(models.Model): + + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + key = models.CharField(max_length=1024, unique=True) + value = models.TextField(blank=True) + + def __unicode__(self): + return self.key + + @classmethod + def set(cls, key, value): + o, created = cls.objects.get_or_create(key=key) + o.value = json.dumps(value) + o.save() + + @classmethod + def get(cls, key, default=None): + if cls.objects.filter(key=key).exists(): + value = json.loads(cls.objects.get(key=key).value) + else: + value = default + return value + + +