# 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()


from datetime import datetime
from flask import url_for
from bson.objectid import ObjectId
from lingobarter.core.db import db
from lingobarter.core.models.custom_values import HasCustomValue
#!/usr/bin/env python
# -*- coding: utf-8 -*-




class Chat(db.DynamicDocument, HasCustomValue):
    # todo: the name of chat will be ignored if it is a p2p chat
    # todo: and will be several users' username by default if it is a group chat
    name = db.StringField(max_length=50, required=True)
    members = db.ListField(db.ObjectIdField(), default=[])
    last_updated = db.DateTimeField(default=datetime.now())

    @classmethod
    def get_chat_by_id(cls, chat_id):
        if type(chat_id) != ObjectId:
            chat_id = ObjectId(chat_id)
        return cls.objects(_id=chat_id).first()

    def __unicode__(self):
        return u"{0} - {1}".format(self.name, self._id)



# 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/modules/barter/models.py
from datetime import datetime
from flask import url_for
from bson.objectid import ObjectId
from lingobarter.core.db import db
from lingobarter.core.models.custom_values import HasCustomValue

#!/usr/bin/env python
# -*- coding: utf-8 -*-




class Chat(db.DynamicDocument, HasCustomValue):
    # todo: the name of chat will be ignored if it is a p2p chat
    # todo: and will be several users' username by default if it is a group chat
    name = db.StringField(max_length=50, required=True)
    members = db.ListField(db.ObjectIdField(), default=[])
    last_updated = db.DateTimeField(default=datetime.now())

    @classmethod
    def get_chat_by_id(cls, chat_id):
        if type(chat_id) != ObjectId:
            chat_id = ObjectId(chat_id)
        return cls.objects(_id=chat_id).first()

    def __unicode__(self):
        return u"{0} - {1}".format(self.name, self._id)



class Message(db.DynamicDocument, HasCustomValue):