Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[+] unfinished params architecture

  • Loading branch information...
commit 2984cdc917f044a7e09ac1f21347dafe96e53699 1 parent 7af9141
Anatoly Larin authored August 18, 2012
25  fortuitus/feditor/dbfields.py
... ...
@@ -1,8 +1,31 @@
  1
+import json
1 2
 from django.db import models
2 3
 from south.modelsinspector import add_introspection_rules
3 4
 
  5
+
4 6
 class ParamsField(models.TextField):
5  
-    pass
  7
+    __metaclass__ = models.SubfieldBase
  8
+
  9
+    description = u'Store query params, like GET params: login=test, password=test'
  10
+
  11
+    def to_python(self, value):
  12
+        if isinstance(value, dict):
  13
+            return value
  14
+        else:
  15
+            if not value:
  16
+                return value
  17
+            return json.loads(str(value))
  18
+
  19
+    def get_prep_value(self, value):
  20
+        if value is not None and not isinstance(value, basestring):
  21
+            if isinstance(value, dict):
  22
+                value = json.dumps(value)
  23
+            else:
  24
+                raise TypeError('Not a dictionary.')
  25
+        return value
  26
+
  27
+    def get_internal_type(self):
  28
+        return 'TextField'
6 29
 
7 30
 
8 31
 add_introspection_rules([], ["^fortuitus\.feditor\.dbfields\.ParamsField"])
46  fortuitus/feditor/params.py
... ...
@@ -0,0 +1,46 @@
  1
+from __future__ import unicode_literals
  2
+import random
  3
+import string
  4
+
  5
+
  6
+class Params(dict):
  7
+    pass
  8
+
  9
+
  10
+class ParamValue(object):
  11
+    def __init__(self):
  12
+        pass
  13
+
  14
+    def __unicode__(self):
  15
+        pass
  16
+
  17
+
  18
+class PlainValue(ParamValue):
  19
+    value = None
  20
+
  21
+    def __init__(self, value):
  22
+        super(PlainValue, self).__init__()
  23
+        self.value = value
  24
+
  25
+    def __unicode__(self):
  26
+        return self.value
  27
+
  28
+    def __iter__(self):
  29
+        return
  30
+
  31
+
  32
+class RandomValue(ParamValue):
  33
+    def __init__(self, length=6, symbols=string.ascii_lowercase):
  34
+        super(RandomValue, self).__init__()
  35
+        self.length = length
  36
+        self.symbols = symbols
  37
+        self.__value = None
  38
+
  39
+    def generate(self):
  40
+        return ''.join([random.choice(self.symbols) for n in xrange(self.length)])
  41
+
  42
+    def __unicode__(self):
  43
+        if self.__value is None:
  44
+            self.__value = self.generate()
  45
+        return self.__value
  46
+
33  fortuitus/feditor/tests.py
... ...
@@ -0,0 +1,33 @@
  1
+import string
  2
+from django.test import TestCase
  3
+from fortuitus.feditor.dbfields import ParamsField
  4
+from fortuitus.feditor.params import Params, PlainValue, RandomValue
  5
+
  6
+class ParamsTestCase(TestCase):
  7
+    def test_plain(self):
  8
+        param = Params()
  9
+        param['login'] = PlainValue('test_login')
  10
+        param['password'] = PlainValue('test_password')
  11
+        self.assertEquals(unicode(param['login']), 'test_login')
  12
+        self.assertEquals(unicode(param['password']), 'test_password')
  13
+
  14
+    def test_random(self):
  15
+        length = 5
  16
+        symbols = string.ascii_letters
  17
+
  18
+        param = Params()
  19
+        param['login'] = RandomValue(length=length, symbols=symbols)
  20
+
  21
+        login = unicode(param['login'])
  22
+        self.assertEquals(len(login), 5)
  23
+        for char in login:
  24
+            self.assertTrue(char in symbols)
  25
+
  26
+        new_login = unicode(param['login'])
  27
+        self.assertEquals(login, new_login)
  28
+
  29
+        #random should be different at least 10 times
  30
+        for x in xrange(10):
  31
+            param['login'] = RandomValue(length=length, symbols=symbols)
  32
+            new_login = unicode(param['login'])
  33
+            self.assertNotEquals(login, new_login)

0 notes on commit 2984cdc

Please sign in to comment.
Something went wrong with that request. Please try again.