hydra / hydra / models /

Full commit
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Alexander Shorin
# All rights reserved.
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

import time
from .. import json
from ..lib.dateutils import datetime_utc_now
from ..mapping import Document as _Document
from ..mapping import (
    Mapping, TextField, IntegerField, BooleanField, DateTimeField, DictField,

class DocumentMeta(_Document.__metaclass__):
    def __call__(self, *args, **kwargs):
        instance = super(DocumentMeta, self).__call__(*args, **kwargs)
        instance.meta.type = self.__meta_type__
        return instance

class Meta(Mapping):
    author = TextField()
    created_at = DateTimeField(default=datetime_utc_now)
    externals = DictField(Mapping)
    peer = TextField()
    readonly = BooleanField(default=False)
    timestamp = IntegerField(
        default=lambda: int(time.mktime(datetime_utc_now().timetuple())))
    type = TextField()
    updated_at = DateTimeField(default=datetime_utc_now)
    user = TextField()

class Document(_Document):
    __metaclass__ = DocumentMeta
    __meta_type__ = 'generic'
    meta = DictField(Meta, name='.meta')

    def iter(cls, db, bucket=100):
        params = {
            'key': cls._fields['meta'].mapping._fields['type'].default,
            'reduce': False,
            'include_docs': True
        if hasattr(db, 'iterview'):
            for row in db.iterview('meta/by_type', bucket, **params):
                yield cls.wrap(row.doc)
            for row in db.view('meta/by_type', **params):
                yield cls.wrap(row.doc)

    def bind(self, db, system, host, idx):
        info = {
            'system': system,
            'host': host,
            'id': idx
        db.update_doc('externals/bind',, body=info)
        return self

    def store(self, db):
        headers, body = db.update_doc('all_docs/doc',
        self._data['_id'] = json.decode(['id']
        self._data['_rev'] = headers.get('X-Couch-Update-NewRev', self.rev)
        return self

    def update(self, db):