Commits

Andy Mikhailenko committed f9e805f

Upgraded to the new Doqu API.

Comments (0)

Files changed (19)

orgtool/ext/actors/__init__.py

+# -*- coding: utf-8 -*-
 """
 Actors management
 =================

orgtool/ext/actors/schema.py

 # -*- coding: utf-8 -*-
 
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from werkzeug import cached_property
 from orgtool.ext.tracking import TrackedDocument
 

orgtool/ext/contacts/commands.py

 
 def find_actors(query):
     db = default_storage()
-    return Actor.objects(db).where(name__matches_caseless=query)
+    return db.find(Actor, name__matches_caseless=query)
 
 def bright(string):
     return Style.BRIGHT + unicode(string) + Style.NORMAL
         raise CommandError(u'No actor matches "{0}".'.format(args.actor))
 
     db = default_storage()
-    qs = Contact.objects(db)
+    qs = db.find(Contact)
     dupes = qs.where(actor=actor, kind=args.type, value=args.value)
     if dupes.count():
         raise CommandError('Such contact already exists.')
     assert args.from_actor or args.query, 'specify actor or auery'
 
     db = default_storage()
-    contacts = Contact.objects(db).where_not(actor=new_actor.pk)
+    contacts = db.find(Contact).where_not(actor=new_actor.pk)
     if args.type:
         contacts = contacts.where(type__in=args.type)
     if args.query:

orgtool/ext/contacts/schema.py

 # -*- coding: utf-8 -*-
 
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from werkzeug import cached_property
 from orgtool.ext.tracking import TrackedDocument
 from orgtool.ext.actors.schema import Actor

orgtool/ext/delicious/commands.py

 import datetime
 import os
 import xml.dom.minidom
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from tool import app
 from tool.cli import command
 from tool.ext.templating import register_templates

orgtool/ext/events/schema.py

 from dateutil.rrule import rrule
 from werkzeug import cached_property
 
-from doqu import Document, Field as f
-from doqu import validators
+from doqu import Document
+from doqu.ext.fields import Field as f
 
 from orgtool.ext.tracking import TrackedDocument
 
+
 def informal_rrule(*args, **kwargs):
     "A wrapper for informal_rrule that does lazy import"
     from orgtool.utils.dates import informal_rrule

orgtool/ext/finances/schema.py

 import logging
 from werkzeug import cached_property
 import dark
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from orgtool.ext.events import Event, Plan
 from orgtool.ext.events.admin import PlanAdmin
 from orgtool.ext.tracking import TrackedDocument

orgtool/ext/finances/views.py

 from tool.ext.documents import default_storage
 from tool.ext.templating import as_html
 from tool.ext.breadcrumbs import entitled
+from tool.ext.who import requires_auth
 
 from schema import Contract, Payment
 from utils import (
 
 
 @url('/')
+@requires_auth
 @entitled(u'Finances')
 @as_html('finances/dashboard.html')
 def dashboard(request):
     db = default_storage()
-    contracts = Contract.objects(db).order_by('next_date_time')
-    payments = Payment.objects(db).order_by('date_time')
+    contracts = db.find(Contract).order_by('next_date_time')
+    payments = db.find(Payment).order_by('date_time')
     return {
         'contracts': contracts,
         'payments': payments,
     }
 
 @url('/contracts/')
+@requires_auth
 @entitled(u'Contracts')
 @as_html('finances/plan_index.html')
 def plan_index(request):
     db = default_storage()
-    plans = Contract.objects(db).order_by('next_date_time')
+    plans = db.find(Contract).order_by('next_date_time')
     return {
         'object_list': plans,
 #        'total_monthly_fee': sum(p.actual_monthly_fee for p in plans)
     }
 
 @url('/contracts/<string:pk>')
+@requires_auth
 @entitled(lambda pk: u'{0}'.format(default_storage().get(Contract,pk)))
 @as_html('finances/plan_detail.html')
 def plan(request, pk):
 @url('/payments/<int(4):year>/')
 @url('/payments/<int(4):year>/<int(2):month>/')
 @url('/payments/<int(4):year>/<int(2):month>/<int(2):day>/')
+@requires_auth
 @entitled(u'Payments')
 @as_html('finances/event_index.html')
 def event_index(request, year=None, month=None, day=None):
     db = default_storage()
-    events = Payment.objects(db).order_by('date_time', reverse=True)
+    events = db.find(Payment).order_by('date_time', reverse=True)
     if year:
         events = events.where(date_time__year=year)
         if month:

orgtool/ext/hamster/schema.py

-from doqu import Field as f
+from datetime import datetime
+from doqu.ext.fields import Field as f
 from orgtool.ext.events import Event
 
+SOURCE = u'orgtool.ext.hamster'
+
 
 class HamsterFact(Event):
     tags = f(list)
     x_hamster_activity_id = f(int, essential=True)
     x_hamster_delta = f(unicode)  # '0:18:00', basically just cached timedelta
 
-    source = f(unicode, required=True,
-               default=u'hamster-client', choices=[u'hamster-client'])
+    # these are to ensure that user can restore possibly mistaken TZ later
+    # (we convert all dates to UTC using custom timezone from config which may
+    # be out of sync at the time of import for certain or all facts being
+    # imported)
+    x_hamster_timezone = f(unicode)
+    x_hamster_orig_datetime = f(datetime)
 
+    source = f(unicode, required=True, default=SOURCE, choices=[SOURCE])
 
+

orgtool/ext/mobile/commands.py

     search_fields.update({'{0}__in'.format(k): my_numbers.values()})
 
     db = app.get_feature('document_storage').default_db
-    if not GammuSMS.objects(db).where(**search_fields).count():
+    if not db.find(GammuSMS, **search_fields).count():
         #print 'NOT FOUND:', search_fields
         print u'SAVING {0} {1} → {2} {3}{4}'.format(
             fields['date_time'], sent_by, sent_to, fields['summary'][:20],
 
             def _contact_exists(contact):
                 conditions = {'kind': contact.kind, 'value': contact.value}
-                duplicates = GammuContact.objects(db).where(**conditions)
+                duplicates = db.find(GammuContact, **conditions)
                 return bool(duplicates.count())
 
             contacts = [c for c in contacts if not _contact_exists(c)]
         last_imported_date = None
         yield 'Importing all messages from the phone...'
     else:
-        msgs = GammuSMS.objects(db).order_by('date_time', reverse=True)
+        msgs = db.find(GammuSMS).order_by('date_time', reverse=True)
         last_imported_date = msgs[0].date_time.date() if msgs.count() else None
         yield 'Importing messages since {0}...'.format(last_imported_date)
 

orgtool/ext/mobile/schema.py

 import datetime
-from doqu import Field as f
+from doqu.ext.fields import Field as f
 from orgtool.ext.talks import Message
 from orgtool.ext.actors.schema import Actor
 from orgtool.ext.contacts import Contact

orgtool/ext/needs/schema.py

 # -*- coding: utf-8 -*-
 
-from doqu import Field as f, Many
+from doqu import Many
+from doqu.ext.fields import Field as f
 
 from orgtool.ext.tracking import TrackedDocument
 #from orgtool.ext.events.schema import HierarchicalPlan as Plan, Event
 from orgtool.ext.actors.schema import Actor
-from orgtool.ext.events.schema import HierarchicalPlan, Plan, Event
+from orgtool.ext.events.schema import Plan, Event
 
 
 __all__ = [

orgtool/ext/needs/views.py

 from orgtool.ext.finances import Payment
 
 from schema import (
-    HierarchicalPlan, Plan, ReasonedPlan, Event, Need,
+    Plan, ReasonedPlan, Event, Need,
     SystemUnderDevelopment
 )
 
 @as_html('needs/plan_detail.html')
 def plan(request, pk):
     db = default_storage()
-    # TODO: drop hierarchy, stick to semantics (reference to Need document)
-    plan = HierarchicalPlan.object(db, pk)
+    plan = db.get(Plan, pk)
 
     # task-related stats
     # TODO aggregate data from nested plans
     total_amount = sum(p.get_amount_as() for p in payments)
 
     try:
-        reasoned = ReasonedPlan.object(db, pk)
+        reasoned = db.get(ReasonedPlan, pk)
         outcome = reasoned.outcome
     except ValidationError:
         outcome = None

orgtool/ext/notes/schema.py

 import datetime
 from dateutil.rrule import rrule
 
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from doqu import validators
 
 from orgtool.ext.tracking import TrackedDocument

orgtool/ext/papers/commands.py

     args_to_unicode(args, ['summary', 'details'])
     db = app.get_feature('document_storage').default_db
 
-    pages = Page.objects(db)
+    pages = db.find(Page)
 
     if args.summary:
         pages = pages.where(summary__matches_caseless=args.summary)
         fingerprint = get_file_hash(open(path, 'rb'))
 
         # check file hash uniqueness
-        if Page.objects(db).where(source_fingerprint=fingerprint):
+        if db.find(Page, source_fingerprint=fingerprint):
             yield '...already in the database.'
             continue
 

orgtool/ext/papers/schema.py

 # -*- coding: utf-8 -*-
 from tool import app
 from doqu import Document, Many
-from doqu.fields import *
+from doqu.ext.fields import *
 
 from orgtool.ext.tracking.schema import TrackedDocument
 
     image = ImageField(get_image_base_path, required=True)
     #attachment = FileField(get_image_base_path, required=True)
     language = Field(unicode)  # "en", "cze", etc. — mainly for OCR (CuneiForm)
+    source = Field(unicode)
     source_fingerprint = Field(str)  # ETL: not necessarily current file
 
     def __unicode__(self):

orgtool/ext/talks/schema.py

 # -*- coding: utf-8 -*-
 
 import datetime
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 from tool.ext import admin
 from orgtool.ext.events import Event
 

orgtool/ext/talks/views.py

 @as_html('talks/message_index.html')
 def message_index(request, year=None, month=None, day=None):
     db = default_storage()
-    messages = Message.objects(db).order_by('date_time', reverse=True)
+    messages = db.find(Message).order_by('date_time', reverse=True)
     if year:
         messages = messages.where(date_time__year=year)
         if month:

orgtool/ext/tracking/schema.py

 # -*- coding: utf-8 -*-
 
 import datetime
-from doqu import Document, Field as f
+from doqu import Document
+from doqu.ext.fields import Field as f
 
 
 class TrackedDocument(Document):
                           default=datetime.datetime.utcnow)
 
     def save(self, *args, **kwargs):
-        self['date_time_updated'] = datetime.datetime.utcnow()
+        now = datetime.datetime.utcnow()
+        self['date_time_updated'] = now
+        # FIXME: Doqu's field defaults seem to be broken so we duplicate them
+        # for date_time_created:
+        self['date_time_created'] = self['date_time_created'] or now
         return super(TrackedDocument, self).save(*args, **kwargs)