def setUp(self):
     for report in ReportNotification.view(
         'reportconfig/all_notifications',
         include_docs=True,
         reduce=False,
     ).all():
         report.delete()
예제 #2
0
def daily_reports():
    # this should get called every hour by celery
    reps = ReportNotification.view("reportconfig/daily_notifications",
                                   key=datetime.utcnow().hour,
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #3
0
def daily_reports():    
    # this should get called every hour by celery
    reps = ReportNotification.view("reportconfig/daily_notifications",
                                   key=datetime.utcnow().hour,
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #4
0
 def setUp(self):
     for report in ReportNotification.view(
         'reportconfig/all_notifications',
         include_docs=True,
         reduce=False,
     ).all():
         report.delete()
예제 #5
0
def monthly_reports():
    now = datetime.utcnow()
    reps = ReportNotification.view("reportconfig/all_notifications",
                                   key=["monthly", now.hour, now.day],
                                   reduce=False,
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #6
0
def weekly_reports():
    # this should get called every hour by celery
    now = datetime.utcnow()
    reps = ReportNotification.view("reportconfig/weekly_notifications",
                                   key=[now.weekday(), now.hour],
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #7
0
def weekly_reports():    
    # this should get called every hour by celery
    now = datetime.utcnow()
    reps = ReportNotification.view("reportconfig/weekly_notifications",
                                   key=[now.weekday(), now.hour],
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #8
0
def monthly_reports():
    now = datetime.utcnow()
    reps = ReportNotification.view("reportconfig/all_notifications",
                                   key=["monthly", now.hour, now.day],
                                   reduce=False,
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #9
0
def daily_reports():
    # this should get called every hour by celery
    reps = ReportNotification.view("reportconfig/all_notifications",
                                   startkey=["daily", datetime.utcnow().hour],
                                   endkey=["daily", datetime.utcnow().hour, {}],
                                   reduce=False,
                                   include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #10
0
def daily_reports():
    # this should get called every hour by celery
    reps = ReportNotification.view(
        "reportconfig/all_notifications",
        startkey=["daily", datetime.utcnow().hour],
        endkey=["daily", datetime.utcnow().hour, {}],
        reduce=False,
        include_docs=True).all()
    for rep in reps:
        send_report.delay(rep._id)
예제 #11
0
def get_scheduled_report_ids(period, as_of=None):
    as_of = as_of or datetime.utcnow()
    assert period in ('daily', 'weekly', 'monthly'), period

    def _keys(period, as_of):
        minute = guess_reporting_minute(as_of)
        if minute == 0:
            # for legacy purposes, on the hour also include reports that didn't have a minute set
            minutes = (None, minute)
        else:
            minutes = (minute,)

        if period == 'daily':
            for minute in minutes:
                yield {
                    'startkey': [period, as_of.hour, minute],
                    'endkey': [period, as_of.hour, minute, {}],
                }
        elif period == 'weekly':
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.weekday()],
                }
        else:
            # monthly
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.day]
                }
            if as_of.day == monthrange(as_of.year, as_of.month)[1]:
                for day in range(as_of.day + 1, 32):
                    for minute in minutes:
                        yield {
                            'key': [period, as_of.hour, minute, day]
                        }

    try:
        keys = _keys(period, as_of)
    except ValueError:
        _soft_assert(False, "Celery was probably down for a while. Lots of reports are getting dropped!")
        raise StopIteration

    for key in keys:
        for result in ReportNotification.view(
            "reportconfig/all_notifications",
            reduce=False,
            include_docs=False,
            **key
        ).all():
            yield result['id']
예제 #12
0
def get_scheduled_report_ids(period, as_of=None):
    as_of = as_of or datetime.utcnow()
    assert period in ('daily', 'weekly', 'monthly'), period

    def _keys(period, as_of):
        minute = guess_reporting_minute(as_of)
        if minute == 0:
            # for legacy purposes, on the hour also include reports that didn't have a minute set
            minutes = (None, minute)
        else:
            minutes = (minute, )

        if period == 'daily':
            for minute in minutes:
                yield {
                    'startkey': [period, as_of.hour, minute],
                    'endkey': [period, as_of.hour, minute, {}],
                }
        elif period == 'weekly':
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute,
                            as_of.weekday()],
                }
        else:
            # monthly
            for minute in minutes:
                yield {'key': [period, as_of.hour, minute, as_of.day]}
            if as_of.day == monthrange(as_of.year, as_of.month)[1]:
                for day in range(as_of.day + 1, 32):
                    for minute in minutes:
                        yield {'key': [period, as_of.hour, minute, day]}

    try:
        keys = _keys(period, as_of)
    except ValueError:
        _soft_assert(
            False,
            "Celery was probably down for a while. Lots of reports are getting dropped!"
        )
        raise StopIteration

    for key in keys:
        for result in ReportNotification.view("reportconfig/all_notifications",
                                              reduce=False,
                                              include_docs=False,
                                              **key).all():
            yield result['id']
예제 #13
0
def get_scheduled_reports(period, as_of=None):
    as_of = as_of or datetime.utcnow()
    assert period in ('daily', 'weekly', 'monthly')

    def _keys(period, as_of):
        minute = guess_reporting_minute(as_of)
        if minute == 0:
            # for legacy purposes, on the hour also include reports that didn't have a minute set
            minutes = (None, minute)
        else:
            minutes = (minute,)

        if period == 'daily':
            for minute in minutes:
                yield {
                    'startkey': [period, as_of.hour, minute],
                    'endkey': [period, as_of.hour, minute, {}],
                }
        elif period == 'weekly':
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.weekday()],
                }
        else:
            # monthly
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.day]
                }
            if as_of.day == monthrange(as_of.year, as_of.month)[1]:
                for day in range(as_of.day + 1, 32):
                    for minute in minutes:
                        yield {
                            'key': [period, as_of.hour, minute, day]
                        }

    for keys in _keys(period, as_of):
        for report in ReportNotification.view("reportconfig/all_notifications",
            reduce=False,
            include_docs=True,
            **keys
        ).all():
            yield report
예제 #14
0
def get_scheduled_reports(period, as_of=None):
    as_of = as_of or datetime.utcnow()
    assert period in ('daily', 'weekly', 'monthly')

    def _keys(period, as_of):
        minute = guess_reporting_minute(as_of)
        if minute == 0:
            # for legacy purposes, on the hour also include reports that didn't have a minute set
            minutes = (None, minute)
        else:
            minutes = (minute,)

        if period == 'daily':
            for minute in minutes:
                yield {
                    'startkey': [period, as_of.hour, minute],
                    'endkey': [period, as_of.hour, minute, {}],
                }
        elif period == 'weekly':
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.weekday()],
                }
        else:
            # monthly
            for minute in minutes:
                yield {
                    'key': [period, as_of.hour, minute, as_of.day]
                }
            if as_of.day == monthrange(as_of.year, as_of.month)[1]:
                for day in range(as_of.day + 1, 32):
                    for minute in minutes:
                        yield {
                            'key': [period, as_of.hour, minute, day]
                        }

    for keys in _keys(period, as_of):
        for report in ReportNotification.view("reportconfig/all_notifications",
            reduce=False,
            include_docs=True,
            **keys
        ).all():
            yield report
예제 #15
0
 def get_scheduled_reports(self):
     return ReportNotification.view("reports/user_notifications", key=self.get_id, include_docs=True).all()
예제 #16
0
    def handle(self, *args, **options):
        old_couch_users = old_couch_user_models.CouchUser.view('users/old_users', include_docs=True)
        print "Loaded all (old) CouchUser docs into memory."
        for old_couch_user in old_couch_users:
            try:
                couch_user = CouchUser.from_old_couch_user(old_couch_user)
                couch_user.old_couch_user_id = old_couch_user.get_id
                couch_user.save(force_update=True)
            except Exception as e:
                print "There was an error migrating CouchUser with _id %s: %s" % (
                    old_couch_user._id.encode('utf-8'),
                    str(e)
                )
            else:
                print "Migrated %s (%s)" % (couch_user.username.encode('utf-8'), couch_user.user_id.encode('utf-8'))

        print "Creating old => new user _id map"
        couch_users = CouchUser.all()
        id_map = {}
        for couch_user in couch_users:
            try:
                old_id = couch_user.old_couch_user_id
            except KeyError:
                pass
            else:
                id_map[old_id] = couch_user.user_id
            try:
                couch_user.save()
            except Exception as e:
                print 'Failed to save %s: %s' % (couch_user.user_id.encode('utf-8'), str(e))

        print "Cleaning up references..."

        print "* Group"
        group_ids = set()
        for group in Group.view('groups/by_user', keys=id_map.keys(), include_docs=True):
            if group._id in group_ids:
                continue
            for i, user_id in enumerate(group.users):
                if user_id in id_map:
                    group.users[i] = id_map[user_id]
            group.save()
            group_ids.add(group._id)

        print "* ReportNotification"
        notification_ids = set()
        for notification in ReportNotification.view('reports/user_notifications',
                                                    keys=id_map.keys(),
                                                    include_docs=True):
            if notification._id in notification_ids:
                continue
            for i, user_id in enumerate(notification.user_ids):
                if user_id in id_map:
                    notification.user_ids[i] = id_map[user_id]
            notification.save()
            notification_ids.add(notification._id)

        print "* MessageLog"
        messages = MessageLog.objects.all()
        count = 0
        for message in messages:
            if message.couch_recipient in id_map:
                message.couch_recipient = id_map[message.couch_recipient]
                count += 1
        print "    %s/%s changed (just in case you were curious)" % (count, messages.count())