Beispiel #1
0
class Picture(NewebeDocument):
    '''
    Picture document for picture storage. Picture describes image posted by
    Newebe owner and his contacts.
    '''

    author = StringProperty()
    title = StringProperty(required=True)
    isMine = BooleanProperty(required=True, default=True)
    path = StringProperty()
    contentType = StringProperty()
    isFile = BooleanProperty(required=True, default=False)

    def get_path(self):
        '''
        Return path where micropost could be find.
        '''
        return "pictures/{}/".format(self._id)
Beispiel #2
0
class MicroPost(NewebeDocument):
    '''
    Micropost object for micro blogging.
    '''

    author = StringProperty()
    content = StringProperty(required=True)
    isMine = BooleanProperty(required=True, default=True)
    pictures = ListProperty(required=False)
    pictures_to_download = ListProperty(required=False)
    commons = ListProperty(required=False)
    commons_to_donwload = ListProperty(required=False)

    def get_path(self):
        '''
        Return path where micropost could be found.
        '''

        return "microposts/{}/".format(self._id)
Beispiel #3
0
class Note(NewebeDocument):
    '''
    Note document for note storage.
    '''

    author = StringProperty()
    title = StringProperty(required=True)
    content = StringProperty(required=False)
    lastModified = DateTimeProperty(required=True,
                                    default=datetime.datetime.now())
    isMine = BooleanProperty(required=True, default=True)

    def save(self):
        '''
        When document is saved, the last modified field is updated to
        make sure it is always correct.
        '''

        if not self.authorKey:
            user = UserManager.getUser()
            self.authorKey = user.key
            self.author = user.name

        self.lastModified = datetime.datetime.utcnow()
        NewebeDocument.save(self)

    def toDict(self, localized=True):
        '''
        Return a dict representation of the document (copy).

        Removes _rev key and convert date field and last modified field
        to local timezone if *localized* is set to True.
        '''

        docDict = NewebeDocument.toDict(self, localized)

        if localized and "lastModified" in docDict:

            utc_date = get_date_from_db_date(docDict.get("lastModified"))
            date = convert_utc_date_to_timezone(utc_date)
            docDict["lastModified"] = get_db_date_from_date(date)

        return docDict
Beispiel #4
0
class Contact(NewebeDocument):
    '''
    Contact describes another newebe with which you are going to share thing
    (a "friend").
    '''

    name = StringProperty()
    key = StringProperty()
    url = StringProperty(required=True)
    state = StringProperty(required=True, default=STATE_PENDING)
    slug = StringProperty(required=True)
    requestDate = DateTimeProperty()
    description = StringProperty()

    def toDict(self, localized=True):
        '''
        Return a dict representation of the document (copy).

        Removes _rev key and convert date field and request date field
        to local timezone if *localized* is set to True.
        '''

        docDict = NewebeDocument.toDict(self, localized)

        if localized and docDict.get("requestDate", ""):

            utc_date = date_util.get_date_from_db_date(
                docDict.get("requestDate"))
            date = date_util.convert_utc_date_to_timezone(utc_date)
            docDict["requestDate"] = date_util.get_db_date_from_date(date)

        return docDict
Beispiel #5
0
class Activity(NewebeDocument):
    '''
    Activity describes a user action or a contact action.
    With activities, newebe log alls actions from owner and his contacts.
    Moreover inside each activity are stored error which occured
    while transfering document corresponding to this activity. It
    handles a list of error: one error for each contact where error occurs.
    This list is used to make retry sending data later to concerned
    contact.
    '''

    author = StringProperty()
    verb = StringProperty(required=True)
    docType = StringProperty(required=True)
    # docId is used to retrieve the doc linked to the activity.
    docId = StringProperty(required=True)
    method = StringProperty(required=True, default="POST")
    isMine = BooleanProperty(required=True, default=True)
    errors = ListProperty()

    def add_error(self, contact, extra=None):
        '''
        And to the error list an error based on *contact* data. Extra
        information can be added to error object (sometimes linked object
        does not exist anymore so some extra data are date are needed).
        '''

        if not self.errors:
            self.errors = []

        activityError = {
            "contactKey": contact.key,
            "contactName": contact.name,
            "contactUrl": contact.url
        }

        if extra:
            activityError["extra"] = extra

        self.errors.append(activityError)
Beispiel #6
0
class User(NewebeDocument):
    '''
    Users object used to handle owner data.
    '''

    name = StringProperty(required=True)
    description = StringProperty()
    url = StringProperty()
    key = StringProperty()
    password = StringProperty()
    picture_name = StringProperty()
    picture_content_type = StringProperty()

    def save(self):
        '''
        Before being saved, if no key is set, couchdb id is set as key for
        current user.
        '''
        if not self.key and "_id" in self.to_json():
            self.key = self.to_json()["_id"]

        if not self.date:
            self.date = datetime.datetime.now()

        super(NewebeDocument, self).save()

    def asContact(self):
        '''
        Return current user data as a Contact object.
        '''
        contact = Contact()
        contact.url = self.url
        contact.key = self.key
        contact.name = self.name
        contact.description = self.description
        contact.date = self.date

        return contact
Beispiel #7
0
class NewebeDocument(Document):
    '''
    Base class for document used by newebe apps. Contains some utility methods.
    '''

    authorKey = StringProperty()
    date = DateTimeProperty(required=True)
    attachments = ListProperty()
    tags = ListProperty(default=["all"])

    def toDict(self, localized=True):
        '''
        Return a dict representation of the document (copy).

        Removes _rev key.
        '''

        docDict = self.__dict__["_doc"].copy()

        if "_rev" in docDict:
            del docDict["_rev"]

        if localized and docDict.get("date", None):

            utc_date = get_date_from_db_date(docDict.get("date"))
            docDict["date"] = get_db_date_from_date(utc_date)

        return docDict

    def toDictForAttachment(self, localized=True):
        '''
        Transform doc to dictionary. Removes all fields that are not useful
        for attachments
        '''

        docDict = self.toDict(localized)
        del docDict["attachments"]
        del docDict["isMine"]
        #del docDict["authorKey"]
        del docDict["_id"]
        if "_attachments" in docDict:
            del docDict["_attachments"]

        return docDict

    def toJson(self, localized=True):
        '''
        Return json representation of the document.
        '''

        docDict = self.toDict(localized)
        return json_encode(docDict)

    def save(self):
        '''
        When document is saved if its date is null, it is set to now.
        '''

        if self.date is None:
            self.date = datetime.datetime.utcnow()
        super(Document, self).save()

    @classmethod
    def get_db(cls):
        '''
        Set DB for each class
        '''
        db = getattr(cls, '_db', None)
        if db is None:
            db = server.get_or_create_db(CONFIG.db.name)
            cls._db = db
        return db
Beispiel #8
0
class ContactTag(NewebeDocument):
    name = StringProperty()

    def toDict(self, localized=True):
        return {"name": self.name, "_id": self._id}