Commits

Andriy Kornatskyy committed 7fcb9a3

Refactored work with vocabularies: moved to repository.

  • Participants
  • Parent commits 3795f71

Comments (0)

Files changed (7)

File demos/template/setup.py

 README = open(os.path.join(os.path.dirname(__file__), 'README')).read()
 
 install_requires = [
-    'wheezy.core>=0.1.83',
+    'wheezy.core>=0.1.86',
     'wheezy.caching>=0.1.62',
     'wheezy.html>=0.1.109',
     'wheezy.http>=0.1.255',

File demos/template/src/config.py

     render_template = WheezyTemplate(engine)
 
 # BaseHandler
+translations = TranslationsManager(
+    directories=['i18n'],
+    default_lang='en')
 options.update({
-    'translations_manager': TranslationsManager(
-        directories=['i18n'],
-        default_lang='en'),
-
+    'translations_manager': translations,
     'render_template': render_template,
 
     'ticket': Ticket(

File demos/template/src/factory.py

 class Factory(object):
 
     def __init__(self, context, session_name='ro'):
-        self.translations = context['translations']
-        self.errors = context['errors']
+        self.context = context
         self.session = sessions[session_name]
         self.repository = RepositoryFactory(self.session)
 
 
     @attribute
     def membership(self):
+        context = self.context
         return MembershipService(
             self.repository,
-            self.errors,
-            self.translations)
+            context['errors'],
+            context['translations'],
+            context['locale'])
 
 
 class RepositoryFactory(object):

File demos/template/src/membership/repository/caching.py

     def __init__(self, inner):
         self.inner = inner
 
+    def password_questions(self, locale):
+        # TODO:
+        return self.inner.password_questions(locale)
+
+    def list_password_questions(self, locale):
+        # TODO:
+        return self.inner.list_password_questions(locale)
+
+    def account_types(self, locale):
+        # TODO:
+        return self.inner.account_types(locale)
+
+    def list_account_types(self, locale):
+        # TODO:
+        return self.inner.list_account_types(locale)
+
     def authenticate(self, credential):
         # TODO:
         return self.inner.authenticate(credential)

File demos/template/src/membership/repository/contract.py

+
 """
 """
 
+from wheezy.core.descriptors import attribute
+
 
 class IMembershipRepository(object):  # pragma: nocover
 
+    def password_questions(self, locale):
+        return {}
+
+    def list_password_questions(self, locale):
+        return sorted([])
+
+    def account_types(self, locale):
+        return {}
+
+    def list_account_types(self, locale):
+        return sorted([])
+
     def authenticate(self, credential):
         assert isinstance(credential, Credential)
         return False

File demos/template/src/membership/repository/mock.py

 """
 """
 
+from operator import itemgetter
+
 from wheezy.core.comp import u
+from wheezy.core.i18n import ref_gettext
 
+from config import translations
 from membership.repository.contract import IMembershipRepository
 
 
+translations = translations['membership']
+
+
 class MembershipRepository(object):
 
     def __init__(self, session):
         # ensure session is entered
         session.cursor()
 
+    def password_questions(self, locale):
+        gettext = ref_gettext(translations[locale])
+        return dict([(key, gettext(value))
+                     for key, value in db['password_question'].items()])
+
+    def list_password_questions(self, locale):
+        return sorted(self.password_questions(locale).items(),
+                      key=itemgetter(1))
+
+    def account_types(self, locale):
+        gettext = ref_gettext(translations[locale])
+        return dict([(key, gettext(value))
+                     for key, value in db['account_type'].items()])
+
+    def list_account_types(self, locale):
+        return sorted(self.account_types(locale).items(),
+                      key=itemgetter(1))
+
     def authenticate(self, credential):
-        return credential.password == db['credentials'].get(
+        return credential.password == db['user'].get(
             credential.username, None)
 
     def has_account(self, username):
-        return username in db['credentials']
+        return username in db['user']
 
     def user_roles(self, username):
-        return tuple(db['roles'].get(username, None))
+        return tuple(db['role'].get(username, None))
 
     def create_account(self, registration):
         credential = registration.credential
-        db['credentials'][credential.username] = credential.password
-        db['roles'][credential.username] = tuple(
+        db['user'][credential.username] = credential.password
+        db['role'][credential.username] = tuple(
             [registration.account.account_type])
         return True
 
 # region: internal details
 
 db = {
-    'credentials': {
+    'user': {
         'demo': u('P@ssw0rd'),
         'biz': u('P@ssw0rd')
     },
-    'roles': {
-    'demo': ['user'],
-    'biz': ['business']
+    'role': {
+        'demo': ['user'],
+        'biz': ['business']
+    },
+    'password_question': {
+        '1': 'Favorite number',
+        '2': 'City of birth',
+        '3': 'Favorite color'
+    },
+    'account_type': {
+        'user': 'User',
+        'business': 'Business'
     }
 }
 

File demos/template/src/membership/service/bridge.py

 """
 """
 
-from operator import itemgetter
-
 from wheezy.core.descriptors import attribute
 from wheezy.core.i18n import ref_gettext
 from wheezy.validation.mixin import ValidationMixin
 
 class MembershipService(ValidationMixin):
 
-    def __init__(self, repository, errors, translations):
+    def __init__(self, repository, errors, translations, locale):
         self.repository = repository
         self.errors = errors
         self.translations = translations
+        self.locale = locale
 
     @attribute
     def gettext(self):
 
     @attribute
     def password_questions(self):
-        return {
-            '1': self.gettext('Favorite number'),
-            '2': self.gettext('City of birth'),
-            '3': self.gettext('Favorite color')
-        }
+        return self.repository.membership.password_questions(self.locale)
 
     @attribute
     def list_password_questions(self):
-        return sorted(self.password_questions.items(), key=itemgetter(1))
+        return self.repository.membership.list_password_questions(self.locale)
 
     @attribute
     def account_types(self):
-        return {
-            'user': self.gettext('User'),
-            'business': self.gettext('Business')
-        }
+        return self.repository.membership.account_types(self.locale)
 
     @attribute
     def list_account_types(self):
-        return sorted(self.account_types.items(), key=itemgetter(1))
+        return self.repository.membership.list_account_types(self.locale)
 
     def authenticate(self, credential):
         assert isinstance(credential, Credential)