Esempio n. 1
0
class Session(Document):
    object_id = TextField()
    bag_id = TextField()
    added = DateTimeField(default=datetime.now)
    Type = TextField(default="Session")
    all = ViewField(
        'sessions', '''\
        function(doc) {
            if(doc.Type == "Session")
                emit(null,doc)
        }
    ''')
    by_object_id = ViewField(
        'sessions', '''\
        function(doc) {
            if(doc.Type == "Session")
                emit(doc.object_id,doc)
        }
    ''')
    by_bag_id = ViewField(
        'sessions', '''\
        function(doc) {
            if(doc.Type == "Session")
                emit(doc.bag_id,doc)
        }
    ''')

    @classmethod
    def sync(cls, db):
        cls.all.sync(db)
        cls.by_object_id.sync(db)
        cls.by_bag_id.sync(db)
Esempio n. 2
0
class Organization(BasicCouchOperations):
    __predicate__ = "organization"
    _id = TextField()
    name = TextField()
    local = ListField(
        DictField(
            Mapping.build(lat=FloatField(),
                          lon=FloatField(),
                          address_str=TextField(),
                          city=TextField())))
    donation_types = ListField(TextField())
    schedule = TextField()
    doc_type = TextField(default="organization")

    all = ViewField(
        __predicate__, '''function(doc){
        if(doc.doc_type == "organization"){
            emit(doc._id, doc);
        }
    }''')

    city = ViewField(
        __predicate__, '''function(doc){
            if (doc.doc_type == "organization"){
                doc.local.forEach(function(local){
                    emit (local.city, doc);
                });
            }
         }''')

    @classmethod
    def sync(cls, db):
        cls.all.sync(db)
        cls.city.sync(db)
Esempio n. 3
0
class Observation(Document):
    object_id = TextField()
    session_id = TextField()
    frame_number = IntegerField()
    Type = TextField(default="Observation")
    by_object_id = ViewField(
        'observations', '''\
        function(doc) {
        if(doc.Type == "Observation")
            emit(doc.object_id, doc)
        }
    ''')

    by_frame_number = ViewField(
        'observations', '''\
        function(doc) {
            if(doc.Type == "Observation")
                emit(doc.frame_number, doc)
        }
    ''')

    by_session_id = ViewField(
        'observations', '''\
        function(doc) {
            if(doc.Type == "Observation")
                emit(doc.session_id, doc)
        }
    ''')

    @classmethod
    def sync(cls, db):
        cls.by_session_id.sync(db)
        cls.by_object_id.sync(db)
        cls.by_frame_number.sync(db)
Esempio n. 4
0
class Article(Document):
    """
        Represents articles stored in DB. Used for both reading and inserting articles datas
    """

    type = TextField()
    title = TextField()
    text = TextField()
    source = TextField()
    pub_date = TextField()
    cluster = IntegerField()
    by_source = ViewField(
        'source', '''\
        function(doc) {
            if (doc.type=='article'){emit(doc.source, doc);};
        }''')
    by_article = ViewField(
        'article', '''\
        function(doc) {
            if (doc.type=='article'){emit(doc);};
        }''')

    @staticmethod
    def check_article_url(url):
        #permanent view
        result = COUCHDB.view('source/by_source')
        if not list(result[url]):
            return True

    def _set_article(self, article):
        self.type = 'article'
        self.source = article.url
        self.title = article.title
        self.text = article.text
        if article.publish_date:
            self.pub_date = str(article.publish_date[0].date())
        else:  # just in case publishing date cannot be retrieved, stores 'None'
            self.pub_date = str(article.publish_date)

    def save_article(self, article):
        self._set_article(article)
        self.store(COUCHDB)

    @staticmethod
    def get_all_articles():
        options = {"include_docs": True}
        result = Article.view(COUCHDB, 'article/by_article', **options)
        return list(result)

    def update_article(self, cluster_key):

        self.cluster = cluster_key
        self.store(COUCHDB)
Esempio n. 5
0
class ProjectType(Document):
    project = TextField()
    type = TextField()
    rate = DecimalField(default=0)

    _by_project_type = ViewField(
        'by_project_type', '''\
            function(doc) {
                    emit([doc.project, doc.type], doc);
            }''')

    @classmethod
    def type_list(cls, project):
        '''List all the types associated with timesheets for a given project'''
        return [
            t.key[1] for t in type_names(timesheets,
                                         group=True,
                                         startkey=[project],
                                         endkey=[project, {}],
                                         inclusive_end=True)
        ]

    def store(self, db=project_types):
        # default database
        super(ProjectType, self).store(db)

    @classmethod
    def load_or_create(cls, project, type):
        project_type = cls._by_project_type(project_types, key=[project, type])
        if project_type:
            return project_type.rows[0]
        return cls(project=project, type=type)
Esempio n. 6
0
class Stopword(Document):
    """
        Class for storing stopwords objects
    """
    type = TextField()
    lang = TextField()
    word = TextField()
    by_stopword = ViewField(
        'stopword', '''\
            function(doc) {
                if (doc.type=='stopword'){emit(doc.lang, doc);};
            }''')

    def sw_exist(self):
        result = COUCHDB.query(self.by_stopword.map_fun)
        if list(result):
            return True

    def set_stopwords(self):
        word_list = stopwords_list(SW_PATH)
        sw_list = []
        for lang, word in word_list.iteritems():
            for each_word in word:
                sw_list.append(
                    Stopword(type='stopword', lang=lang, word=each_word))
        #return sw_list
        COUCHDB.update(sw_list)

    def get_all_stopwords(self):
        options = {"include_docs": True}
        result = Stopword.view(COUCHDB, 'stopword/by_stopword', **options)
        return result
Esempio n. 7
0
 class UsageData(Document):
     """
     Document that represents the stored data.
     """
     url = TextField()
     ua_browser = TextField()
     ua_language = TextField()
     ua_platform = TextField()
     ua_version = TextField()
     blueprint = TextField()
     view_args = TextField()
     status = IntegerField()
     remote_addr = TextField()
     authorization = BooleanField()
     ip_info = TextField()
     path = TextField()
     speed = FloatField()
     datetime = DateTimeField(default=datetime.now)
     username = TextField()
     track_var = TextField()
     by_date = ViewField(
         'start-end', '''function(doc, req) {
         if (!doc._conflicts) {
             emit(doc.datetime, doc);
         }
     }''')
Esempio n. 8
0
class Invoice(Document):
    date = DateField()
    bill_to = TextField()
    project = TextField()
    rate = DecimalField()
    tax = DecimalField(default=0.0)

    _all_invoices = ViewField(
        'invoices', '''\
            function(doc) {
                if (doc._id != "no invoice") {
                    emit(doc.date, doc);
                }
            }''')
    _by_project = ViewField(
        'by_project', '''\
            function(doc) {
                if (doc._id != "no invoice") {
                    emit(doc.project, doc);
                }
            }''')

    def store(self, db=invoices):
        # default database
        super(Invoice, self).store(db)

    @classmethod
    def all_invoices(cls):
        return cls._all_invoices(invoices, descending=True)

    @classmethod
    def for_project(cls, project):
        return cls._by_project(invoices, key=project)

    @classmethod
    def load(cls, id, db=invoices):
        return super(Invoice, cls).load(db, id)

    @classmethod
    def next_invoice_number(cls):
        result = last_invoice_num(invoices)
        if result.rows:
            return result.rows[0]['value'] + 1
        return 1
Esempio n. 9
0
def V(design, name, reduce=None):
    """
    Shortcut for ViewField and EnhancedViewField

    @author BrendonCrawford
    """
    if reduce is None:
        return ViewField.define(design=design, name=name)
    else:
        return EnhancedViewField.define(design=design,
                                        name=name,
                                        reduce=reduce)
Esempio n. 10
0
def V(design, name, reduce=None):
    """
    Shortcut for ViewField and EnhancedViewField

    @author BrendonCrawford
    """
    if reduce is None:
        return ViewField.define(design=design, name=name)
    else:
        return EnhancedViewField.define(design=design,
                                        name=name,
                                        reduce=reduce)
Esempio n. 11
0
class Object(Document):
    object_name = TextField()
    description = TextField()
    tags = ListField(TextField())
    author_name = TextField()
    author_email = TextField()
    added = DateTimeField(default=datetime.now)
    Type = TextField(default="Object")

    all = ViewField(
        'objects', '''\
        function(doc) {
            if(doc.Type == "Object")
                emit(doc.object_name,doc)
        }
    ''')
    by_object_name = ViewField(
        'objects', '''\
        function(doc) {
            if(doc.Type == "Object")
                emit(doc.object_name, doc)
        }
    ''')

    by_tag = ViewField(
        'objects', '''\
        function(doc) {
            if(doc.Type == "Object")
                for( tag in doc.tags )
                {
                    emit(doc.tags[tag], doc)
                }
        }
    ''')

    @classmethod
    def sync(cls, db):
        cls.all.sync(db)
        cls.by_object_name.sync(db)
        cls.by_tag.sync(db)
Esempio n. 12
0
class Donator(BasicCouchOperations):
    _id = TextField()
    name = TextField()
    doc_type = TextField(default="donator")

    all = ViewField(
        "donator", '''function(doc){
        if(doc.doc_type == "donator"){
            emit(doc._id, doc);
        }
    }''')

    @classmethod
    def sync(cls, db):
        cls.all.sync(db)
Esempio n. 13
0
class User(Document):
    name = TextField()
    password = TextField()
    exclusions = ListField(TextField())
    dataInstances = ListField(
        DictField(
            Mapping.build(task_name=TextField(),
                          start_time=DateTimeField(),
                          end_time=DateTimeField(),
                          suggested_time=IntegerField(),
                          pre_energy=IntegerField(),
                          user_feedback=IntegerField(),
                          auto_rating=BooleanField())))
    breakInstances = ListField(
        DictField(
            Mapping.build(break_start_time=DateTimeField(),
                          break_end_time=DateTimeField())))

    usernameView = ViewField(
        'users', '''\
         function(doc) {
             emit(doc.name, doc);
         }''')

    def get_favorites(self):
        favs = []
        for inst in self.dataInstances:
            if inst.task_name not in favs and inst.task_name not in self.exclusions:
                favs.append(inst.task_name)
        return favs

    @property
    def is_active(self):
        return True

    @property
    def is_authenticated(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.name)
Esempio n. 14
0
class Timesheet(Document):
    date = DateField()
    duration = DecimalField()
    project = TextField()
    type = TextField(default='')
    description = TextField()
    invoice = TextField(default='')
    archived_rate = DecimalField()
    _all_timesheets = ViewField(
        'timesheets', '''\
            function(doc) {
                emit(doc.project, doc);
            }''')
    _by_date = ViewField(
        'by_date', '''\
            function(doc) {
                emit(doc.date, doc);
            }''')
    _by_project = ViewField(
        'by_project', '''\
            function(doc) {
                emit(doc.project, doc);
            }''')
    _by_date_unbilled = ViewField(
        'by_date', '''\
            function(doc) {
                if (!doc.invoice) {
                    emit(doc.date, doc);
                }
            }''')
    _by_project_unbilled = ViewField(
        'by_project', '''\
            function(doc) {
                if (!doc.invoice) {
                    emit(doc.project, doc);
                }
            }''')
    _by_invoice = ViewField(
        'by_invoice', '''\
            function(doc) {
                if(doc.invoice) {
                    emit(doc.invoice, doc);
                }
            }''')

    @classmethod
    def load(cls, id, db=timesheets):
        return super(Timesheet, cls).load(db, id)

    @classmethod
    def all_timesheets(cls, unbilled=False):
        if not unbilled:
            return cls._all_timesheets(timesheets)
        else:
            return cls._by_date_unbilled(timesheets, descending=True)

    @classmethod
    def for_date(cls, date):
        if isinstance(date, datetime.date):
            date = datetime.date.strftime("%Y-m-%d")
        return cls._by_date(timesheets, key=date)

    @classmethod
    def for_month(cls, year, month):
        month = int(month)
        return cls._by_date(timesheets,
                            startkey="%s-%#02d-01" % (year, month),
                            endkey="%s-%#02d-00" % (year, month + 1))

    @classmethod
    def for_project(cls, project, unbilled=False):
        if not unbilled:
            projects = cls._by_project(timesheets, key=project)
        else:
            projects = cls._by_project_unbilled(timesheets, key=project)

        return sorted(projects, key=lambda s: s.date, reverse=True)

    @classmethod
    def for_invoice(cls, invoice):
        return sorted(cls._by_invoice(timesheets, key=invoice),
                      key=lambda s: s.date,
                      reverse=True)

    def store(self, db=timesheets):
        # default database
        super(Timesheet, self).store(db)

    @property
    def rate(self):
        project = Project.load(self.project)
        if self.archived_rate:
            return self.archived_rate
        if self.invoice:
            invoice = Invoice.load(self.invoice)
            if invoice and invoice.rate:
                return invoice.rate
        if self.type:
            type = ProjectType.load_or_create(self.project, self.type)
            if type and type.rate:
                return type.rate
        if project:
            return project.rate
        return 0

    @property
    def fee(self):
        return self.rate * self.duration
Esempio n. 15
0
class Model(Document):
    object_id = TextField()
    model_params = TextField()
    Type = TextField(default="Model")
    method = TextField()

    # this dictionary will include several views: the key will be the type of objectmodel
    by_object_id_and = {}
    by_object_id = ViewField(
        'models', '''\
        function(doc) {
            if (doc.Type == "Model")
                emit(doc.object_id, doc)
        }
    ''')
    all = ViewField(
        'models', '''\
        function(doc) {
            if(doc.Type == "Model")
                emit(null,doc)
        }''')

    @classmethod
    def sync(cls, db):
        cls.by_object_id.sync(db)
        cls.all.sync(db)
        # figure out the different models in the DB
        models = [
            m.value for m in db.query(
                '''
        function(doc) {
            if (doc.Type == "Model") {
                if (doc.method) {
                    emit(doc.method, doc.method);
                } else {
                    if (doc.ModelType) {
                        emit(doc.ModelType, doc.ModelType);
                    }
                }
            }
        }
        ''', '''
        function(keys, values, rereduce) {
        log(keys);
            var o = {}, key, key_id;
            if (rereduce) {
                for (v in values) {
                    for(j in values[v]) {
                        o[values[v][j]] = values[v][j];
                    }
                }
            } else {
                for (key_id in keys) {
                    key = keys[key_id][0];
                    o[key] = key;
                }
            }
            var r = [];
            for (key in o) {
                r.push(o[key]);
            }
            return r;
        }
        ''')
        ]

        if not models:
            return
        else:
            models = models[0]
        # for each, create a view and sync it with the DB
        for model in models:
            cls.by_object_id_and[model] = ViewDefinition('models',
                                                         'by_object_id_and_' +
                                                         model,
                                                         '''\
                function(doc) {
                    if (doc.Type == "Model")
                        if ((doc.method == "%s") || (doc.ModelType == "%s"))
                            emit(doc.object_id, doc)
                }
            ''' % (model, model),
                                                         wrapper=cls._wrap_row)
            cls.by_object_id_and[model].sync(db)