Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[+] unfinished params architecture

  • Loading branch information...
commit 2984cdc917f044a7e09ac1f21347dafe96e53699 1 parent 7af9141
@alarin alarin authored
View
25 fortuitus/feditor/dbfields.py
@@ -1,8 +1,31 @@
+import json
from django.db import models
from south.modelsinspector import add_introspection_rules
+
class ParamsField(models.TextField):
- pass
+ __metaclass__ = models.SubfieldBase
+
+ description = u'Store query params, like GET params: login=test, password=test'
+
+ def to_python(self, value):
+ if isinstance(value, dict):
+ return value
+ else:
+ if not value:
+ return value
+ return json.loads(str(value))
+
+ def get_prep_value(self, value):
+ if value is not None and not isinstance(value, basestring):
+ if isinstance(value, dict):
+ value = json.dumps(value)
+ else:
+ raise TypeError('Not a dictionary.')
+ return value
+
+ def get_internal_type(self):
+ return 'TextField'
add_introspection_rules([], ["^fortuitus\.feditor\.dbfields\.ParamsField"])
View
46 fortuitus/feditor/params.py
@@ -0,0 +1,46 @@
+from __future__ import unicode_literals
+import random
+import string
+
+
+class Params(dict):
+ pass
+
+
+class ParamValue(object):
+ def __init__(self):
+ pass
+
+ def __unicode__(self):
+ pass
+
+
+class PlainValue(ParamValue):
+ value = None
+
+ def __init__(self, value):
+ super(PlainValue, self).__init__()
+ self.value = value
+
+ def __unicode__(self):
+ return self.value
+
+ def __iter__(self):
+ return
+
+
+class RandomValue(ParamValue):
+ def __init__(self, length=6, symbols=string.ascii_lowercase):
+ super(RandomValue, self).__init__()
+ self.length = length
+ self.symbols = symbols
+ self.__value = None
+
+ def generate(self):
+ return ''.join([random.choice(self.symbols) for n in xrange(self.length)])
+
+ def __unicode__(self):
+ if self.__value is None:
+ self.__value = self.generate()
+ return self.__value
+
View
33 fortuitus/feditor/tests.py
@@ -0,0 +1,33 @@
+import string
+from django.test import TestCase
+from fortuitus.feditor.dbfields import ParamsField
+from fortuitus.feditor.params import Params, PlainValue, RandomValue
+
+class ParamsTestCase(TestCase):
+ def test_plain(self):
+ param = Params()
+ param['login'] = PlainValue('test_login')
+ param['password'] = PlainValue('test_password')
+ self.assertEquals(unicode(param['login']), 'test_login')
+ self.assertEquals(unicode(param['password']), 'test_password')
+
+ def test_random(self):
+ length = 5
+ symbols = string.ascii_letters
+
+ param = Params()
+ param['login'] = RandomValue(length=length, symbols=symbols)
+
+ login = unicode(param['login'])
+ self.assertEquals(len(login), 5)
+ for char in login:
+ self.assertTrue(char in symbols)
+
+ new_login = unicode(param['login'])
+ self.assertEquals(login, new_login)
+
+ #random should be different at least 10 times
+ for x in xrange(10):
+ param['login'] = RandomValue(length=length, symbols=symbols)
+ new_login = unicode(param['login'])
+ self.assertNotEquals(login, new_login)
Please sign in to comment.
Something went wrong with that request. Please try again.