def send_message(fxf, status=None, comment=None, comment_url=None): roles = UserRole.objects.filter(site=fxf.site, ended_at=None, group__name="Site Supervisor") emails = [r.user.email for r in roles] Device = get_device_model() is_delete = True if status is None and fxf.fsform is not None else False is_deployed = True if fxf.is_deployed: status_message = "New Form Deployed" else: is_deployed = False status_message = "Form Undeployed" message = { 'notify_type': 'Form', 'is_delete': is_delete, 'form_id': fxf.id, 'comment': comment, 'form_name': fxf.xf.title, 'xfid': fxf.xf.id_string, 'form_type': fxf.form_type(), 'form_type_id': fxf.form_type_id(), 'status': status_message, 'is_deployed': is_deployed, 'comment_url': comment_url, 'site': { 'name': fxf.site.name, 'id': fxf.site.id } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_message_un_deploy_project(fxf): roles = UserRole.objects.filter( site__project=fxf.project, ended_at=None, group__name="Site Supervisor").distinct('user') emails = [r.user.email for r in roles] Device = get_device_model() message = { 'notify_type': 'Form Altered Project', 'is_delete': False, 'form_id': fxf.id, 'is_deployed': fxf.is_deployed, 'form_name': fxf.xf.title, 'xfid': fxf.xf.id_string, 'form_type': fxf.form_type(), 'form_type_id': fxf.form_type_id(), 'site': {}, 'project': { 'name': fxf.project.name, 'id': fxf.project.id } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def create_messages(sender, instance, created, **kwargs): if created and instance.site is not None and instance.group.name in ["Site Supervisor"]: Device = get_device_model() if Device.objects.filter(name=instance.user.email).exists(): message = {'notify_type':'Assign Site', 'site':{'name': instance.site.name, 'id': instance.site.id}, 'project':{'name': instance.project.name, 'id': instance.project.id}} try: email = instance.user.email save_notification(message, [str(email)]) Device.objects.filter(name=email).send_message(message) except: pass if created and instance.group.name == "Project Manager": from onadata.apps.fsforms.tasks import created_manager_form_share task_obj = CeleryTaskProgress.objects.create( user=instance.user, description='Share all forms', task_type=18, content_object=instance ) if task_obj: try: with transaction.atomic(): created_manager_form_share.apply_async(kwargs={'userrole': instance.id, 'task_id': task_obj.id}, countdown=5) except IntegrityError: pass roles = UserRole.get_active_roles(instance.user) # roles = Role.objects.filter(user=request.user).select_related('group', 'organization') if roles: cache.set('roles_{}'.format(instance.user.id), roles, 2 * 60) if roles.filter(group__name="Super Admin").exists(): cache.set('admin_{}'.format(instance.user.id), True, 2 * 60) else: cache.set('admin_{}'.format(instance.user.id), False, 2 * 60)
def notify_koboform_updated(xform): from onadata.apps.fsforms.models import FieldSightXF project_ids = FieldSightXF.objects.filter(xf=xform).values_list( 'project_id', flat=True).distinct().order_by() site_ids = FieldSightXF.objects.filter(xf=xform).values_list( 'site_id', flat=True).distinct().order_by() project_ids = [v for v in project_ids if v] site_ids = [v for v in site_ids if v] emails = UserRole.objects.filter( ended_at=None, group__name__in=["Site Supervisor", "Region Supervisor"]).filter( Q(site__id__in=site_ids) | Q(site__project__id__in=project_ids)).values_list( 'user__email', flat=True).distinct().order_by() Device = get_device_model() is_delete = False message = { 'notify_type': 'Form', 'is_delete': is_delete, 'form_name': xform.title, 'xfid': xform.id_string, 'form_type': "", 'form_type_id': "", 'status': "Form Updated", 'site': {} } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_message_stages(site): roles = UserRole.objects.filter(site=site, ended_at=None, group__name="Site Supervisor") emails = [r.user.email for r in roles] Device = get_device_model() message = { 'notify_type': 'Stages Ready', 'is_delete': True, 'site': { 'name': site.name, 'id': site.id } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_bulk_message_stages_deployed_site(site): roles = UserRole.objects.filter(site=site, ended_at=None, group__name="Site Supervisor") emails = [r.user.email for r in roles] Device = get_device_model() message = { 'notify_type': 'deploy_all', 'is_delete': True, 'is_project': 0, 'description': "Stages Ready in Site {}".format(site.name), 'site': { 'name': site.name, 'id': site.id } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_bulk_message_stages_deployed_project(project): roles = UserRole.objects.filter( site__project=project, ended_at=None, group__name="Site Supervisor").distinct('user') emails = [r.user.email for r in roles] Device = get_device_model() message = { 'notify_type': 'deploy_all', 'is_delete': True, 'is_project': 1, 'description': u"Stages Ready in Project {}".format(project.name), 'project': { 'name': project.name, 'id': project.id } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_message_xf_changed(fxf=None, form_type=None, id=None): roles = UserRole.objects.filter( site=fxf.site, ended_at=None, group__name="Site Supervisor").distinct('user') emails = [r.user.email for r in roles] Device = get_device_model() message = { 'notify_type': 'Kobo Form Changed', 'is_delete': True, 'site': { 'name': fxf.site.name, 'id': fxf.site.id }, 'form': { 'xfid': fxf.xf.id_string, 'form_id': fxf.id, 'form_type': form_type, 'form_source_id': id, 'form_name': fxf.xf.title } } save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def send_message_flagged(fi=None, comment=None, comment_url=None): if fi.submitted_by: emails = [fi.submitted_by.email] Device = get_device_model() is_delete = False message = { 'notify_type': 'Form_Flagged', 'is_delete': is_delete, 'form_id': fi.fsxf.id, 'project_form_id': fi.fsxf.id, 'comment': comment, 'form_name': fi.fsxf.xf.title, 'xfid': fi.fsxf.xf.id_string, 'form_type': fi.fsxf.form_type(), 'form_type_id': fi.fsxf.form_type_id(), 'status': FORM_STATUS.get(fi.form_status, "New Form"), 'comment_url': comment_url, 'submission_date_time': str(fi.date), 'submission_id': fi.id, 'version': fi.version } if fi.site: message['site'] = { 'name': fi.site.name, 'id': fi.site.id, 'identifier': fi.site.identifier } if fi.project: message['project'] = {'name': fi.project.name, 'id': fi.project.id} if fi.fsxf.site: message['site_level_form'] = True else: message['site_level_form'] = False save_notification(message, emails) Device.objects.filter(name__in=emails, is_active=True).send_message(message)
def handle(self, *args, **options): last_date = datetime.datetime(2019, 5, 26, 10, 56, 5, 310000) total_changed = InstanceStatusChanged.objects.filter( finstance__project_id=options['project'], date__lt=last_date).count() page_size = 50 page = 0 while total_changed > 0: time.sleep(2) changed = InstanceStatusChanged.objects.filter( finstance__project_id=options['project'], date__lt=last_date)[page * page_size:(page + 1) * page_size] print("Creating message", page * page_size, (page + 1) * page_size) for fi in changed: if fi.finstance.submitted_by: comment_url = "{}/forms/api/instance/change_status-detail/{}".format( settings.KOBOCAT_URL, fi.id) emails = [fi.finstance.submitted_by.email] is_deleted = False message = { 'notify_type': 'Form_Flagged', 'is_delete': is_deleted, 'form_id': fi.finstance.fsxf.id, 'project_form_id': fi.finstance.fsxf.id, 'comment': fi.message, 'form_name': fi.finstance.fsxf.xf.title, 'xfid': fi.finstance.fsxf.xf.id_string, 'form_type': fi.finstance.fsxf.form_type(), 'form_type_id': fi.finstance.fsxf.form_type_id(), 'status': FORM_STATUS.get(fi.finstance.form_status, "New Form"), 'comment_url': comment_url, 'submission_date_time': str(fi.finstance.date), 'submission_id': fi.finstance.id, 'version': fi.finstance.version } if fi.finstance.site: message['site'] = { 'name': fi.finstance.site.name, 'id': fi.finstance.site.id, 'identifier': fi.finstance.site.identifier } if fi.finstance.project: message['project'] = { 'name': fi.finstance.project.name, 'id': fi.finstance.project.id } if fi.finstance.fsxf.site: message['site_level_form'] = True else: message['site_level_form'] = False save_notification(message, emails, date=fi.date) total_changed -= page_size page += 1 print('Successfully logged status change submissions.')