Example #1
0
def touch_saved_reports_views(user, domain):
    """
    Hit the saved reports views so stale=update_after doesn't cause the user to
    see old or deleted data after a change when they next load the reports
    homepage.

    """
    ReportConfig.by_domain_and_owner(domain, user._id, limit=1, stale=False)
    ReportNotification.by_domain_and_owner(domain, user._id, limit=1, stale=False)
Example #2
0
def touch_saved_reports_views(user, domain):
    """
    Hit the saved reports views so stale=update_after doesn't cause the user to
    see old or deleted data after a change when they next load the reports
    homepage.

    """
    ReportConfig.by_domain_and_owner(domain, user._id, limit=1).all()
    ReportNotification.by_domain_and_owner(domain, user._id, limit=1).all()
Example #3
0
def saved_reports(request, domain, template="reports/reports_home.html"):
    user = request.couch_user
    if not (request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports()):
        raise Http404

    configs = ReportConfig.by_domain_and_owner(domain, user._id)

    def _is_valid(rn):
        # the _id check is for weird bugs we've seen in the wild that look like
        # oddities in couch.
        return hasattr(rn, "_id") and rn._id and (not hasattr(rn, "report_slug") or rn.report_slug != "admin_domains")

    scheduled_reports = [rn for rn in ReportNotification.by_domain_and_owner(domain, user._id) if _is_valid(rn)]
    scheduled_reports = sorted(scheduled_reports, key=lambda rn: rn.configs[0].name)

    context = dict(
        couch_user=request.couch_user,
        domain=domain,
        configs=configs,
        scheduled_reports=scheduled_reports,
        report=dict(
            title=_("My Saved Reports"),
            show=user.can_view_reports() or user.get_viewable_reports(),
            slug=None,
            is_async=True,
            section_name=ProjectReport.section_name,
        ),
    )

    if request.couch_user:
        util.set_report_announcements_for_user(request, user)

    return render(request, template, context)
Example #4
0
def saved_reports(request, domain, template="reports/reports_home.html"):
    user = request.couch_user
    if not (request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports()):
        raise Http404

    configs = ReportConfig.by_domain_and_owner(domain, user._id).all()
    scheduled_reports = [s for s in ReportNotification.by_domain_and_owner(domain, user._id).all()
                         if not hasattr(s, 'report_slug') or s.report_slug != 'admin_domains']

    context = dict(
        couch_user=request.couch_user,
        domain=domain,
        configs=configs,
        scheduled_reports=scheduled_reports,
        report=dict(
            title="Select a Report to View",
            show=user.can_view_reports() or user.get_viewable_reports(),
            slug=None,
            is_async=True,
            section_name=ProjectReport.section_name,
        ),
    )

    if request.couch_user:
        util.set_report_announcements_for_user(request, user)

    return render(request, template, context)
Example #5
0
    def balance_email_reports(self):
        EWSMigrationProblem.objects.filter(domain=self.domain).delete()
        reports = set()
        reports_count = 0
        for web_user in WebUser.by_domain(self.domain):
            notifications = ReportNotification.by_domain_and_owner(self.domain, web_user.get_id)
            for notification in notifications:
                config_id = notification.config_ids[0] if notification.config_ids else None

                if not config_id:
                    continue

                config = ReportConfig.get(config_id)
                location_id = config.filters.get('location_id')
                if not location_id:
                    # report is not migrated from ews
                    continue
                reports_count += 1
                report_slug = config.report_slug
                code = SQLLocation.objects.get(location_id=location_id).site_code
                report_tuple = (
                    web_user.username, notification.day, notification.hour,
                    code, report_slug, notification.interval
                )
                external_id = '{}-{}-{}-{}-{}-{}'.format(*report_tuple)
                if not notification.send_to_owner and not notification.recipient_emails:
                    migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                        domain=self.domain,
                        object_id=web_user.username,
                        object_type='email_report_send_to_owner',
                        external_id=external_id
                    )
                    migration_problem.description = 'send_to_owner not set to true'
                    migration_problem.save()

                reports.add(report_tuple)

        total_count = 0

        for report in self.endpoint.get_daily_reports(limit=1000)[1]:
            if self._check_report(report, reports, 1, 'daily'):
                total_count += 1

        for report in self.endpoint.get_weekly_reports(limit=1000)[1]:
            if self._check_report(report, reports, report.day_of_week, 'weekly'):
                total_count += 1

        for report in self.endpoint.get_monthly_reports(limit=1000)[1]:
            if self._check_report(report, reports, report.day_of_month, 'monthly'):
                total_count += 1

        if total_count != reports_count:
            migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                domain=self.domain,
                object_id=None,
                object_type='email_report',
                external_id='email-report-count'
            )
            migration_problem.description = '{} / {}'.format(reports_count, total_count)
            migration_problem.save()
Example #6
0
def saved_reports(request, domain, template="reports/reports_home.html"):
    user = request.couch_user
    if not (request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports()):
        raise Http404

    configs = ReportConfig.by_domain_and_owner(domain, user._id)

    def _is_valid(rn):
        # the _id check is for weird bugs we've seen in the wild that look like
        # oddities in couch.
        return hasattr(rn, "_id") and rn._id and (not hasattr(rn, 'report_slug') or rn.report_slug != 'admin_domains')

    scheduled_reports = [rn for rn in ReportNotification.by_domain_and_owner(domain, user._id) if _is_valid(rn)]
    scheduled_reports = sorted(scheduled_reports, key=lambda rn: rn.configs[0].name)

    context = dict(
        couch_user=request.couch_user,
        domain=domain,
        configs=configs,
        scheduled_reports=scheduled_reports,
        report=dict(
            title=_("My Saved Reports"),
            show=user.can_view_reports() or user.get_viewable_reports(),
            slug=None,
            is_async=True,
            section_name=ProjectReport.section_name,
        ),
    )

    if request.couch_user:
        util.set_report_announcements_for_user(request, user)

    return render(request, template, context)
Example #7
0
def set_send_to_owner_field_task(domain):
    for web_user in WebUser.by_domain(domain):
        notifications = ReportNotification.by_domain_and_owner(domain, web_user.get_id)
        for notification in notifications:
            if not notification.send_to_owner and not notification.recipient_emails:
                notification.send_to_owner = True
                notification.save()
    balance_migration_task.delay(domain)
Example #8
0
    def _report_notfication_sync(self, report, interval, day):
        if not report.users or report.report not in self.REPORT_MAP:
            return
        user_id = report.users[0]
        recipients = report.users[1:]
        location_code = report.view_args.split()[1][1:-2]

        user = WebUser.get_by_username(user_id)
        if not user:
            return

        try:
            location = SQLLocation.active_objects.get(site_code=location_code, domain=self.domain)
        except SQLLocation.DoesNotExist:
            return

        notifications = ReportNotification.by_domain_and_owner(self.domain, user.get_id)
        for n in notifications:
            if len(n.config_ids) == 1:
                # Migrated reports have only one config
                config = ReportConfig.get(n.config_ids[0])
                location_id = config.filters.get('location_id')
                slug = self.REPORT_MAP[report.report]
                report_slug = config.report_slug
                if (n.day, location_id, report_slug, n.interval) == (day, location.location_id, slug, interval):
                    if not n.send_to_owner and not n.recipient_emails:
                        n.send_to_owner = True
                        n.save()
                    return

        saved_config = ReportConfig(
            report_type='custom_project_report', name=report.report, owner_id=user.get_id,
            report_slug=self.REPORT_MAP[report.report], domain=self.domain,
            filters={'filter_by_program': 'all', 'location_id': location.location_id}
        )
        saved_config.save()
        saved_notification = ReportNotification(
            hour=report.hours, day=day, interval=interval, owner_id=user.get_id, domain=self.domain,
            recipient_emails=recipients, config_ids=[saved_config.get_id], send_to_owner=True
        )
        saved_notification.save()
        return saved_notification
Example #9
0
def saved_reports(request, domain, template="reports/reports_home.html"):
    user = request.couch_user
    if not (request.couch_user.can_view_reports()
            or request.couch_user.get_viewable_reports()):
        raise Http404

    configs = ReportConfig.by_domain_and_owner(domain, user._id)

    def _is_valid(rn):
        # the _id check is for weird bugs we've seen in the wild that look like
        # oddities in couch.
        return hasattr(rn, "_id") and rn._id and (not hasattr(rn, 'report_slug') or rn.report_slug != 'admin_domains')

    scheduled_reports = [rn for rn in ReportNotification.by_domain_and_owner(domain, user._id) if _is_valid(rn)]
    scheduled_reports = sorted(scheduled_reports, key=lambda rn: rn.configs[0].name)
    for report in scheduled_reports:
        time_difference = get_timezone_difference(domain)
        (report.hour, day_change) = recalculate_hour(report.hour, int(time_difference[:3]), int(time_difference[3:]))
        report.minute = 0
        if day_change:
            report.day = calculate_day(report.interval, report.day, day_change)

    context = dict(
        couch_user=request.couch_user,
        domain=domain,
        configs=configs,
        scheduled_reports=scheduled_reports,
        report=dict(
            title=_("My Saved Reports"),
            show=user.can_view_reports() or user.get_viewable_reports(),
            slug=None,
            is_async=True,
            section_name=ProjectReport.section_name,
        ),
    )

    if request.couch_user:
        util.set_report_announcements_for_user(request, user)

    return render(request, template, context)
Example #10
0
    def _report_notfication_sync(self, report, interval, day):
        if not report.users:
            return
        user_id = report.users[0]
        recipients = report.users[1:]
        location_code = report.view_args.split()[1][1:-2]

        user = WebUser.get_by_username(user_id)
        if not user:
            return

        try:
            location = SQLLocation.objects.get(site_code=location_code, domain=self.domain)
        except SQLLocation.DoesNotExist:
            return

        notifications = ReportNotification.by_domain_and_owner(self.domain, user.get_id)
        reports = []
        for n in notifications:
            for config_id in n.config_ids:
                config = ReportConfig.get(config_id)
                reports.append((config.filters.get('location_id'), config.report_slug, interval))

        if report.report not in self.REPORT_MAP or (location.location_id, self.REPORT_MAP[report.report],
                                                    interval) in reports:
            return

        saved_config = ReportConfig(
            report_type='custom_project_report', name=report.report, owner_id=user.get_id,
            report_slug=self.REPORT_MAP[report.report], domain=self.domain,
            filters={'filter_by_program': 'all', 'location_id': location.location_id}
        )
        saved_config.save()
        saved_notification = ReportNotification(
            hour=report.hours, day=day, interval=interval, owner_id=user.get_id, domain=self.domain,
            recipient_emails=recipients, config_ids=[saved_config.get_id]
        )
        saved_notification.save()
        return saved_notification