# Path: lingobarter/core/db.py

# 
# Path: lingobarter/core/models/custom_values.py
# class HasCustomValue(object):
#     values = db.ListField(db.EmbeddedDocumentField(CustomValue))
# 
#     def get_values_tuple(self):
#         return [(value.name, value.value, value.formatter)
#                 for value in self.values]
# 
#     def get_value(self, name, default=None):
#         try:
#             return self.values.get(name=name).value
#         except:
#             return default
# 
#     def add_value(self, name, value, formatter='text'):
#         """
#         Another implementation
#         data = {"name": name, "rawvalue": value, "formatter": formatter}
#         self.values.update(data, name=name) or self.values.create(**data)
#         """
#         custom_value = CustomValue(
#             name=name,
#             value=value,
#             formatter=formatter
#         )
#         self.values.append(custom_value)
# 
#     def clean(self):
#         current_names = [value.name for value in self.values]
#         for name in current_names:
#             if current_names.count(name) > 1:
#                 raise Exception(lazy_gettext("%(name)s already exists",
#                                              name=name))
#         super(HasCustomValue, self).clean()
# 
# Path: lingobarter/core/models/signature.py
# class Publishable(Dated, Owned):
#     published = db.BooleanField(default=False)
# 
#     @property
#     def is_available(self):
#         now = datetime.datetime.now()
#         return (
#             self.published and
#             self.available_at <= now and
#             (self.available_until is None or
#              self.available_until >= now)
#         )
# 
#     def save(self, *args, **kwargs):
#         self.updated_at = datetime.datetime.now()
# 
#         user = get_current_user_for_models()
# 
#         if not self.id and not self.created_by:
#             self.created_by = user
#         self.last_updated_by = user
# 
#         super(Publishable, self).save(*args, **kwargs)
# 
# class ContentFormat(object):
#     content_format = db.StringField(
#         choices=TEXT_FORMATS,
#         default=get_setting_value('DEFAULT_TEXT_FORMAT', 'html')
#     )
# 
# class Dated(object):
#     available_at = db.DateTimeField(default=datetime.datetime.now)
#     available_until = db.DateTimeField(required=False)
#     created_at = db.DateTimeField(default=datetime.datetime.now)
#     updated_at = db.DateTimeField(default=datetime.datetime.now)
# 
# class Slugged(object):
#     slug = db.StringField(max_length=255, required=True)
# 
#     def validate_slug(self, title=None):
#         if self.slug:
#             self.slug = slugify(self.slug)
#         else:
#             self.slug = slugify(title or self.title)


import logging
from flask import current_app
from lingobarter.core.db import db
from lingobarter.core.fields import MultipleObjectsReturned
from lingobarter.core.models.custom_values import HasCustomValue
from lingobarter.core.models.signature import (
    Publishable, ContentFormat, Dated, Slugged
)
    @classmethod
    def get(cls, group, name=None, default=None):

        try:
            instance = cls.objects.get(group=group)
        except:
            return None

        if not name:
            ret = instance.values
            if group == 'settings':
                ret = {}
                ret.update(current_app.config)
                ret.update({item.name: item.value for item in instance.values})
        else:
            try:
                ret = instance.values.get(name=name).value
            except (MultipleObjectsReturned, AttributeError):
                ret = None

        if not ret and group == 'settings' and name is not None:
            # get direct from store to avoid infinite loop
            ret = current_app.config.store.get(name)

        return ret or default

    def __unicode__(self):
        return self.group



# Path: lingobarter/core/db.py

# 
# Path: lingobarter/core/models/custom_values.py
# class HasCustomValue(object):
#     values = db.ListField(db.EmbeddedDocumentField(CustomValue))
# 
#     def get_values_tuple(self):
#         return [(value.name, value.value, value.formatter)
#                 for value in self.values]
# 
#     def get_value(self, name, default=None):
#         try:
#             return self.values.get(name=name).value
#         except:
#             return default
# 
#     def add_value(self, name, value, formatter='text'):
#         """
#         Another implementation
#         data = {"name": name, "rawvalue": value, "formatter": formatter}
#         self.values.update(data, name=name) or self.values.create(**data)
#         """
#         custom_value = CustomValue(
#             name=name,
#             value=value,
#             formatter=formatter
#         )
#         self.values.append(custom_value)
# 
#     def clean(self):
#         current_names = [value.name for value in self.values]
#         for name in current_names:
#             if current_names.count(name) > 1:
#                 raise Exception(lazy_gettext("%(name)s already exists",
#                                              name=name))
#         super(HasCustomValue, self).clean()
# 
# Path: lingobarter/core/models/signature.py
# class Publishable(Dated, Owned):
#     published = db.BooleanField(default=False)
# 
#     @property
#     def is_available(self):
#         now = datetime.datetime.now()
#         return (
#             self.published and
#             self.available_at <= now and
#             (self.available_until is None or
#              self.available_until >= now)
#         )
# 
#     def save(self, *args, **kwargs):
#         self.updated_at = datetime.datetime.now()
# 
#         user = get_current_user_for_models()
# 
#         if not self.id and not self.created_by:
#             self.created_by = user
#         self.last_updated_by = user
# 
#         super(Publishable, self).save(*args, **kwargs)
# 
# class ContentFormat(object):
#     content_format = db.StringField(
#         choices=TEXT_FORMATS,
#         default=get_setting_value('DEFAULT_TEXT_FORMAT', 'html')
#     )
# 
# class Dated(object):
#     available_at = db.DateTimeField(default=datetime.datetime.now)
#     available_until = db.DateTimeField(required=False)
#     created_at = db.DateTimeField(default=datetime.datetime.now)
#     updated_at = db.DateTimeField(default=datetime.datetime.now)
# 
# class Slugged(object):
#     slug = db.StringField(max_length=255, required=True)
# 
#     def validate_slug(self, title=None):
#         if self.slug:
#             self.slug = slugify(self.slug)
#         else:
#             self.slug = slugify(title or self.title)


# Path: lingobarter/core/models/config.py
import logging
from flask import current_app
from lingobarter.core.db import db
from lingobarter.core.fields import MultipleObjectsReturned
from lingobarter.core.models.custom_values import HasCustomValue
from lingobarter.core.models.signature import (
    Publishable, ContentFormat, Dated, Slugged
)

    @classmethod
    def get(cls, group, name=None, default=None):

        try:
            instance = cls.objects.get(group=group)
        except:
            return None

        if not name:
            ret = instance.values
            if group == 'settings':
                ret = {}
                ret.update(current_app.config)
                ret.update({item.name: item.value for item in instance.values})
        else:
            try:
                ret = instance.values.get(name=name).value
            except (MultipleObjectsReturned, AttributeError):
                ret = None

        if not ret and group == 'settings' and name is not None:
            # get direct from store to avoid infinite loop
            ret = current_app.config.store.get(name)

        return ret or default

    def __unicode__(self):
        return self.group



class Lingobarter(Dated, Slugged, db.DynamicDocument):