예제 #1
0
    def handle(self, *args, **options):
        domain = args[0]
        db = Domain.get_db()
        doc_ids = [
            r['id'] for r in db.view(
                'domain/docs',
                startkey=[domain, 'RepeatRecord'],
                endkey=[domain, 'RepeatRecord', {}],
                reduce=False,
            )
        ]
        count = len(doc_ids)
        print 'found %s doc ids' % count
        latest = datetime.min
        latest_doc = None
        for i, doc in enumerate(iter_docs(db, doc_ids)):
            wrapped = RepeatRecord.wrap(doc)
            if i % 100 == 0:
                print 'checked %s / %s' % (i, count)
            if wrapped.last_checked and wrapped.last_checked > latest:
                latest = wrapped.last_checked
                latest_doc = wrapped
                print 'new latest: %s' % latest

        if latest_doc:
            print 'latest repeater date is %s' % latest
            print 'latest repeater is %s' % latest_doc._id
        else:
            print 'no relevant repeaters found'
예제 #2
0
def _copy(config):
    # unfortunately the only couch view we have for this needs to go by domain
    # will be a bit slow
    database = Domain.get_db()
    assert database.uri == config.source_db.uri, 'can only use "copy" with the main HQ DB as the source'
    domain_names = Domain.get_all_names()
    for domain in domain_names:
        for doc_type in config.doc_types:
            ids_of_this_type = [row['id'] for row in database.view(
                'domain/docs',
                startkey=[domain, doc_type],
                endkey=[domain, doc_type, {}],
                reduce=False,
                include_docs=False,
            )]
            if ids_of_this_type:
                new_revs = dict([
                    (row['id'], row['value']['rev'])
                    for row in config.dest_db.view('_all_docs', keys=ids_of_this_type, include_docs=False)
                    if 'error' not in row
                ])
                for id_group in chunked(ids_of_this_type, 500):
                    docs = get_docs(database, id_group)
                    for doc in docs:
                        if doc['_id'] in new_revs:
                            doc['_rev'] = new_revs[doc['_id']]
                    config.dest_db.bulk_save(docs)

            print 'copied {} {}s from {}'.format(len(ids_of_this_type), doc_type, domain)
    print 'copy docs complete'
예제 #3
0
 def query(self):
     db = Domain.get_db()
     startkey = self.search_string
     endkey = "{}Z".format(self.search_string) if startkey else ''
     query = db.view(
         'domain/domains',
         reduce=False,
         startkey=startkey,
         endkey=endkey,
         limit=20,
     )
     return query
예제 #4
0
 def query(self):
     db = Domain.get_db()
     startkey = self.search_string
     endkey = "{}Z".format(self.search_string) if startkey else ''
     query = db.view(
         'domain/domains',
         reduce=False,
         startkey=startkey,
         endkey=endkey,
         limit=20,
     )
     return query
    def handle(self, *args, **options):
        domain = args[0]
        old_db = Domain.get_db()
        new_db = RepeatRecord.get_db()
        assert old_db.dbname != new_db.dbname
        doc_ids = get_doc_ids(domain, 'RepeatRecord', old_db)
        count = len(doc_ids)
        chunksize = 250

        for i, docs in enumerate(chunked(iter_docs(old_db, doc_ids, chunksize), chunksize)):
            for doc in docs:
                if '_rev' in doc:
                    del doc['_rev']
            new_db.bulk_save(docs, new_edits=False)
            print 'checked %s / %s' % (i * chunksize, count)
예제 #6
0
    def handle(self, *args, **options):
        db = Domain.get_db()

        def get_doc_ids():
            for result in db.view('domain/domains', reduce=False).all():
                yield result['id']
            for result in ApplicationBase.get_db().view(
                    'app_manager/applications',
                    startkey=[None],
                    endkey=[None, {}],
                    reduce=False):
                yield result['id']

        for doc in iter_docs(db, get_doc_ids()):
            if 'secure_submissions' not in doc:
                print 'Updated', doc.get('doc_type'), doc.get('_id')
                doc['secure_submissions'] = False
                db.save_doc(doc)
예제 #7
0
    def handle(self, doc_types, *args, **options):

        input = raw_input('\n'.join([
            '\n\nReally delete documents of the following types: {}?',
            'This operation is not reversible. Enter a number N to delete the first '
            'N found, or type "delete all" to delete everything.',
            '',
        ]).format(doc_types))
        if input == 'delete all':
            remaining = None
        else:
            try:
                remaining = int(input)
            except ValueError:
                print 'aborting'
                sys.exit()

        doc_types = doc_types.split(',')
        deleted = 0

        # unfortunately the only couch view we have for this needs to go by domain
        # will be a bit slow
        domain_names = Domain.get_all_names()
        database = Domain.get_db()
        for domain in domain_names:
            for doc_type in doc_types:
                docs = [row['doc'] for row in database.view(
                    'domain/docs',
                    startkey=[domain, doc_type],
                    endkey=[domain, doc_type, {}],
                    reduce=False,
                    include_docs=True,
                )][:remaining]
                if docs:
                    count = len(docs)
                    print 'deleting {} {}s from {}'.format(count, doc_type, domain)
                    database.delete_docs(docs)
                    deleted += count
                    if remaining is not None:
                        remaining -= count
                        if remaining <= 0:
                            return

        print 'successfully deleted {} documents'.format(deleted)
    def handle(self, *args, **options):
        domain = args[0]
        old_db = Domain.get_db()
        new_db = RepeatRecord.get_db()
        assert old_db.dbname != new_db.dbname
        doc_ids = [
            r["id"]
            for r in old_db.view(
                "domain/docs", startkey=[domain, "RepeatRecord"], endkey=[domain, "RepeatRecord", {}], reduce=False
            )
        ]
        count = len(doc_ids)
        chunksize = 250

        for i, docs in enumerate(chunked(iter_docs(old_db, doc_ids, chunksize), chunksize)):
            for doc in docs:
                if "_rev" in doc:
                    del doc["_rev"]
            new_db.bulk_save(docs, new_edits=False)
            print "checked %s / %s" % (i * chunksize, count)
    def handle(self, *args, **options):
        db = Domain.get_db()

        def get_doc_ids():
            for result in db.view(
                    'domain/domains',
                    reduce=False).all():
                yield result['id']
            for result in ApplicationBase.get_db().view(
                    'app_manager/applications',
                    startkey=[None],
                    endkey=[None, {}],
                    reduce=False):
                yield result['id']

        for doc in iter_docs(db, get_doc_ids()):
            if 'secure_submissions' not in doc:
                print 'Updated', doc.get('doc_type'), doc.get('_id')
                doc['secure_submissions'] = False
                db.save_doc(doc)
예제 #10
0
    def handle(self, *args, **options):
        domain = args[0]
        db = Domain.get_db()
        doc_ids = get_doc_ids(domain, "RepeatRecord", db)
        count = len(doc_ids)
        print "found %s doc ids" % count
        latest = datetime.min
        latest_doc = None
        for i, doc in enumerate(iter_docs(db, doc_ids)):
            wrapped = RepeatRecord.wrap(doc)
            if i % 100 == 0:
                print "checked %s / %s" % (i, count)
            if wrapped.last_checked and wrapped.last_checked > latest:
                latest = wrapped.last_checked
                latest_doc = wrapped
                print "new latest: %s" % latest

        if latest_doc:
            print "latest repeater date is %s" % latest
            print "latest repeater is %s" % latest_doc._id
        else:
            print "no relevant repeaters found"
    def handle(self, *args, **options):
        domain = args[0]
        old_db = Domain.get_db()
        new_db = RepeatRecord.get_db()
        assert old_db.dbname != new_db.dbname
        doc_ids = [
            r['id'] for r in old_db.view(
                'domain/docs',
                startkey=[domain, 'RepeatRecord'],
                endkey=[domain, 'RepeatRecord', {}],
                reduce=False,
            )
        ]
        count = len(doc_ids)
        chunksize = 250

        for i, docs in enumerate(
                chunked(iter_docs(old_db, doc_ids, chunksize), chunksize)):
            for doc in docs:
                if '_rev' in doc:
                    del doc['_rev']
            new_db.bulk_save(docs, new_edits=False)
            print 'checked %s / %s' % (i * chunksize, count)
예제 #12
0
def generate_invoices(based_on_date=None):
    """
    Generates all invoices for the past month.
    """
    today = based_on_date or datetime.date.today()
    invoice_start, invoice_end = utils.get_previous_month_date_range(today)
    invoiceable_subscriptions = Subscription.objects.filter(date_start__lt=invoice_end,
                                                            date_end__gt=invoice_start).all()

    def _create_invoice(sub):
        invoice_factory = SubscriptionInvoiceFactory(invoice_start, invoice_end, sub)
        invoice_factory.create()

    invoiced_orgs = []
    orgs = Organization.get_db().view('orgs/by_name', group=True, group_level=1).all()
    org_names = [o['key'] for o in orgs]
    for org_name in org_names:
        try:
            subscription = invoiceable_subscriptions.get(subscriber__organization=org_name)
        except ObjectDoesNotExist:
            continue
        _create_invoice(subscription)
        invoiced_orgs.append(org_name)

    all_domain_ids = [d['id'] for d in Domain.get_all(include_docs=False)]
    for domain_doc in iter_docs(Domain.get_db(), all_domain_ids):
        domain_name = domain_doc['name']
        domain_org = domain_doc['organization']
        try:
            subscription = invoiceable_subscriptions.get(subscriber__domain=domain_name)
        except ObjectDoesNotExist:
            if domain_org not in invoiced_orgs:
                domain = Domain.wrap(domain_doc)
                invoice_factory = CommunityInvoiceFactory(invoice_start, invoice_end, domain)
                invoice_factory.create()
            continue
        _create_invoice(subscription)