def test_get_scheduled_report_response(self): domain = 'test-scheduled-reports' user = WebUser.create( domain=domain, username='******', password='******', ) report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": user._id, }) report_config.save() report = ReportNotification( hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id] ) report.save() response = get_scheduled_report_response( couch_user=user, domain=domain, scheduled_report_id=report._id )[0] self.assertEqual(200, response.status_code) self.assertTrue(user.username in response.serialize())
def test_get_scheduled_report_response(self): domain = self.domain user = WebUser.create( domain=domain, username='******', password='******', ) report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": user._id, }) report_config.save() report = ReportNotification(hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id]) report.save() response = get_scheduled_report_response( couch_user=user, domain=domain, scheduled_report_id=report._id)[0] self.assertTrue(user.username in response)
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
def test_get_scheduled_report_response(self): domain = self.domain report_config = ReportConfig.wrap({ "date_range": "last30", "days": 30, "domain": domain, "report_slug": "worker_activity", "report_type": "project_report", "owner_id": self.user._id, }) report_config.save() report = ReportNotification(hour=12, minute=None, day=30, interval='monthly', config_ids=[report_config._id]) report.save() response = get_scheduled_report_response( couch_user=self.user, domain=domain, scheduled_report_id=report._id)[0] self.assertTrue(self.user.username in response.decode('utf-8'))
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
def edit_scheduled_report(request, domain, scheduled_report_id=None, template="reports/edit_scheduled_report.html"): from corehq.apps.users.models import WebUser from corehq.apps.reports.forms import ScheduledReportForm context = { 'form': None, 'domain': domain, 'report': { 'show': request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports(), 'slug': None, 'default_url': reverse('reports_home', args=(domain,)), 'is_async': False, 'section_name': ProjectReport.section_name, } } user_id = request.couch_user._id configs = ReportConfig.by_domain_and_owner(domain, user_id) config_choices = [(c._id, c.full_name) for c in configs if c.report and c.report.emailable] if not config_choices: return render(request, template, context) web_users = WebUser.view('users/web_users_by_domain', reduce=False, key=domain, include_docs=True).all() web_user_emails = [u.get_email() for u in web_users] if scheduled_report_id: instance = ReportNotification.get(scheduled_report_id) time_difference = get_timezone_difference(domain) (instance.hour, day_change) = recalculate_hour(instance.hour, int(time_difference[:3]), int(time_difference[3:])) instance.minute = 0 if day_change: instance.day = calculate_day(instance.interval, instance.day, day_change) if instance.owner_id != user_id or instance.domain != domain: raise HttpResponseBadRequest() else: instance = ReportNotification(owner_id=user_id, domain=domain, config_ids=[], hour=8, minute=0, send_to_owner=True, recipient_emails=[]) is_new = instance.new_document initial = instance.to_json() initial['recipient_emails'] = ', '.join(initial['recipient_emails']) kwargs = {'initial': initial} args = (request.POST,) if request.method == "POST" else () form = ScheduledReportForm(*args, **kwargs) form.fields['config_ids'].choices = config_choices form.fields['recipient_emails'].choices = web_user_emails form.fields['hour'].help_text = "This scheduled report's timezone is %s (%s GMT)" % \ (Domain._get_by_name(domain)['default_timezone'], get_timezone_difference(domain)[:3] + ':' + get_timezone_difference(domain)[3:]) if request.method == "POST" and form.is_valid(): for k, v in form.cleaned_data.items(): setattr(instance, k, v) time_difference = get_timezone_difference(domain) (instance.hour, day_change) = calculate_hour(instance.hour, int(time_difference[:3]), int(time_difference[3:])) instance.minute = int(time_difference[3:]) if day_change: instance.day = calculate_day(instance.interval, instance.day, day_change) instance.save() if is_new: messages.success(request, "Scheduled report added!") else: messages.success(request, "Scheduled report updated!") touch_saved_reports_views(request.couch_user, domain) return HttpResponseRedirect(reverse('reports_home', args=(domain,))) context['form'] = form context['day_value'] = getattr(instance, "day", 1) context['weekly_day_options'] = ReportNotification.day_choices() context['monthly_day_options'] = [(i, i) for i in range(1, 32)] if is_new: context['form_action'] = "Create a new" context['report']['title'] = "New Scheduled Report" else: context['form_action'] = "Edit" context['report']['title'] = "Edit Scheduled Report" return render(request, template, context)
def edit_scheduled_report(request, domain, scheduled_report_id=None, template="reports/edit_scheduled_report.html"): from corehq.apps.users.models import WebUser from corehq.apps.reports.forms import ScheduledReportForm context = { 'form': None, 'domain': domain, 'report': { 'show': request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports(), 'slug': None, 'default_url': reverse('reports_home', args=(domain, )), 'is_async': False, 'section_name': ProjectReport.section_name, } } user_id = request.couch_user._id configs = ReportConfig.by_domain_and_owner(domain, user_id) config_choices = [(c._id, c.full_name) for c in configs if c.report and c.report.emailable] if not config_choices: return render(request, template, context) web_users = WebUser.view('users/web_users_by_domain', reduce=False, key=domain, include_docs=True).all() web_user_emails = [u.get_email() for u in web_users] if scheduled_report_id: instance = ReportNotification.get(scheduled_report_id) if instance.owner_id != user_id or instance.domain != domain: raise HttpResponseBadRequest() else: instance = ReportNotification(owner_id=user_id, domain=domain, config_ids=[], hour=8, send_to_owner=True, recipient_emails=[]) is_new = instance.new_document initial = instance.to_json() initial['recipient_emails'] = ', '.join(initial['recipient_emails']) kwargs = {'initial': initial} args = (request.POST, ) if request.method == "POST" else () form = ScheduledReportForm(*args, **kwargs) form.fields['config_ids'].choices = config_choices form.fields['recipient_emails'].choices = web_user_emails if request.method == "POST" and form.is_valid(): for k, v in form.cleaned_data.items(): setattr(instance, k, v) instance.save() if is_new: messages.success(request, "Scheduled report added!") else: messages.success(request, "Scheduled report updated!") touch_saved_reports_views(request.couch_user, domain) return HttpResponseRedirect(reverse('reports_home', args=(domain, ))) context['form'] = form context['day_value'] = getattr(instance, "day", 1) context['weekly_day_options'] = ReportNotification.day_choices() context['monthly_day_options'] = [(i, i) for i in range(1, 32)] if is_new: context['form_action'] = "Create a new" context['report']['title'] = "New Scheduled Report" else: context['form_action'] = "Edit" context['report']['title'] = "Edit Scheduled Report" return render(request, template, context)
def edit_scheduled_report(request, domain, scheduled_report_id=None, template="reports/edit_scheduled_report.html"): from corehq.apps.users.models import WebUser from corehq.apps.reports.forms import ScheduledReportForm context = { 'form': None, 'domain': domain, 'report': { 'show': request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports(), 'slug': None, 'default_url': reverse('reports_home', args=(domain,)), 'is_async': False, 'section_name': ProjectReport.section_name, } } user_id = request.couch_user._id configs = ReportConfig.by_domain_and_owner(domain, user_id).all() config_choices = [(c._id, c.full_name) for c in configs if c.report and c.report.emailable] if not config_choices: return render(request, template, context) web_users = WebUser.view('users/web_users_by_domain', reduce=False, key=domain, include_docs=True).all() web_user_emails = [u.get_email() for u in web_users] if scheduled_report_id: instance = ReportNotification.get(scheduled_report_id) if instance.owner_id != user_id or instance.domain != domain: raise HttpResponseBadRequest() else: instance = ReportNotification(owner_id=user_id, domain=domain, config_ids=[], day_of_week=-1, hours=8, send_to_owner=True, recipient_emails=[]) is_new = instance.new_document initial = instance.to_json() initial['recipient_emails'] = ', '.join(initial['recipient_emails']) kwargs = {'initial': initial} args = (request.POST,) if request.method == "POST" else () form = ScheduledReportForm(*args, **kwargs) form.fields['config_ids'].choices = config_choices form.fields['recipient_emails'].choices = web_user_emails if request.method == "POST" and form.is_valid(): for k, v in form.cleaned_data.items(): setattr(instance, k, v) instance.save() if is_new: messages.success(request, "Scheduled report added!") else: messages.success(request, "Scheduled report updated!") return HttpResponseRedirect(reverse('reports_home', args=(domain,))) context['form'] = form if is_new: context['form_action'] = "Create a new" context['report']['title'] = "New Scheduled Report" else: context['form_action'] = "Edit" context['report']['title'] = "Edit Scheduled Report" return render(request, template, context)
def edit_scheduled_report(request, domain, scheduled_report_id=None, template="reports/edit_scheduled_report.html"): from corehq.apps.users.models import WebUser from corehq.apps.reports.forms import ScheduledReportForm context = { "form": None, "domain": domain, "report": { "show": request.couch_user.can_view_reports() or request.couch_user.get_viewable_reports(), "slug": None, "default_url": reverse("reports_home", args=(domain,)), "is_async": False, "section_name": ProjectReport.section_name, }, } user_id = request.couch_user._id configs = ReportConfig.by_domain_and_owner(domain, user_id) config_choices = [(c._id, c.full_name) for c in configs if c.report and c.report.emailable] if not config_choices: return render(request, template, context) web_users = WebUser.view("users/web_users_by_domain", reduce=False, key=domain, include_docs=True).all() web_user_emails = [u.get_email() for u in web_users] if scheduled_report_id: instance = ReportNotification.get(scheduled_report_id) if instance.owner_id != user_id or instance.domain != domain: raise HttpResponseBadRequest() else: instance = ReportNotification( owner_id=user_id, domain=domain, config_ids=[], hour=8, send_to_owner=True, recipient_emails=[] ) is_new = instance.new_document initial = instance.to_json() initial["recipient_emails"] = ", ".join(initial["recipient_emails"]) kwargs = {"initial": initial} args = (request.POST,) if request.method == "POST" else () form = ScheduledReportForm(*args, **kwargs) form.fields["config_ids"].choices = config_choices form.fields["recipient_emails"].choices = web_user_emails if request.method == "POST" and form.is_valid(): for k, v in form.cleaned_data.items(): setattr(instance, k, v) instance.save() if is_new: messages.success(request, "Scheduled report added!") else: messages.success(request, "Scheduled report updated!") touch_saved_reports_views(request.couch_user, domain) return HttpResponseRedirect(reverse("reports_home", args=(domain,))) context["form"] = form context["day_value"] = getattr(instance, "day", 1) context["weekly_day_options"] = ReportNotification.day_choices() context["monthly_day_options"] = [(i, i) for i in range(1, 32)] if is_new: context["form_action"] = "Create a new" context["report"]["title"] = "New Scheduled Report" else: context["form_action"] = "Edit" context["report"]["title"] = "Edit Scheduled Report" return render(request, template, context)