def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') super(UserForm, self).__init__(*args, **kwargs) if self.user.get_org(): self.fields['regions'].queryset = Region.get_all(self.user.get_org()).order_by('name')
def kwargs_from_temba(cls, org, temba_contact): name = temba_contact.name or "" org_region_uuids = [r.uuid for r in Region.get_all(org)] region_uuids = intersection(org_region_uuids, temba_contact.groups) region = Region.objects.get(org=org, uuid=region_uuids[0]) if region_uuids else None if not region: # pragma: no cover raise ValueError("No region with UUID in %s" % ", ".join(temba_contact.groups)) org_group_uuids = [g.uuid for g in Group.get_all(org)] group_uuids = intersection(org_group_uuids, temba_contact.groups) group = Group.objects.get(org=org, uuid=group_uuids[0]) if group_uuids else None facility_code = temba_contact.fields.get(org.facility_code_field, None) return { 'org': org, 'name': name, 'urn': temba_contact.urns[0], 'region': region, 'group': group, 'language': temba_contact.language, 'facility_code': facility_code, 'uuid': temba_contact.uuid, }
def sync_org_contacts(org_id): """ Syncs all contacts for the given org """ from tracpro.groups.models import Region, Group from tracpro.orgs_ext.constants import TaskType from .models import Contact org = Org.objects.get(pk=org_id) logger.info('Starting contact sync task for org #%d' % org.id) sync_groups = [r.uuid for r in Region.get_all(org)] + [g.uuid for g in Group.get_all(org)] most_recent_contact = Contact.objects.by_org(org).active().exclude(temba_modified_on=None) most_recent_contact = most_recent_contact.order_by('-temba_modified_on').first() if most_recent_contact: last_time = most_recent_contact.temba_modified_on else: last_time = None created, updated, deleted, failed = sync_pull_contacts( org, Contact, fields=(), groups=sync_groups, last_time=last_time, delete_blocked=True) task_result = dict(time=datetime_to_ms(timezone.now()), counts=dict(created=len(created), updated=len(updated), deleted=len(deleted), failed=len(failed))) org.set_task_result(TaskType.sync_contacts, task_result) logger.info("Finished contact sync for org #%d (%d created, " "%d updated, %d deleted, %d failed)" % (org.id, len(created), len(updated), len(deleted), len(failed)))
def sync_org_contacts(org_id): """ Syncs all contacts for the given org """ from tracpro.groups.models import Region, Group from tracpro.orgs_ext import TaskType from .models import Contact org = Org.objects.get(pk=org_id) logger.info('Starting contact sync task for org #%d' % org.id) sync_groups = [r.uuid for r in Region.get_all(org)] + [g.uuid for g in Group.get_all(org)] created, updated, deleted, failed = sync_pull_contacts(org, Contact, fields=(), groups=sync_groups) task_result = dict(time=datetime_to_ms(timezone.now()), counts=dict(created=len(created), updated=len(updated), deleted=len(deleted), failed=len(failed))) org.set_task_result(TaskType.sync_contacts, task_result) logger.info("Finished contact sync for org #%d (%d created, %d updated, %d deleted, %d failed)" % (org.id, len(created), len(updated), len(deleted), len(failed)))
def sync_org_contacts(org_id): """ Syncs all contacts for the given org """ from tracpro.groups.models import Region, Group from tracpro.orgs_ext.constants import TaskType from .models import Contact org = Org.objects.get(pk=org_id) logger.info('Starting contact sync task for org #%d' % org.id) sync_groups = [r.uuid for r in Region.get_all(org)] + [g.uuid for g in Group.get_all(org)] created, updated, deleted, failed = sync_pull_contacts( org, Contact, fields=(), groups=sync_groups) task_result = dict(time=datetime_to_ms(timezone.now()), counts=dict(created=len(created), updated=len(updated), deleted=len(deleted), failed=len(failed))) org.set_task_result(TaskType.sync_contacts, task_result) logger.info("Finished contact sync for org #%d (%d created, " "%d updated, %d deleted, %d failed)" % (org.id, len(created), len(updated), len(deleted), len(failed)))
def push_contact_change(contact_id, change_type): """ Task to push a local contact change to RapidPro """ from tracpro.groups.models import Group, Region from .models import Contact contact = Contact.objects.select_related('org', 'region').get(pk=contact_id) org = contact.org logger.info("Pushing %s change to contact %s" % (change_type.name.upper(), contact.uuid)) region_uuids = set([r.uuid for r in Region.get_all(org)]) group_uuids = set([r.uuid for r in Group.get_all(org)]) sync_push_contact(org, contact, change_type, [region_uuids, group_uuids])
def kwargs_from_temba(cls, org, temba_contact): name = temba_contact.name if temba_contact.name is not None else "" org_region_uuids = [r.uuid for r in Region.get_all(org)] region_uuids = intersection(org_region_uuids, temba_contact.groups) region = Region.objects.get(org=org, uuid=region_uuids[0]) if region_uuids else None if not region: # pragma: no cover raise ValueError("No region with UUID in %s" % ", ".join(temba_contact.groups)) org_group_uuids = [g.uuid for g in Group.get_all(org)] group_uuids = intersection(org_group_uuids, temba_contact.groups) group = Group.objects.get(org=org, uuid=group_uuids[0]) if group_uuids else None facility_code = temba_contact.fields.get(org.get_facility_code_field(), None) return dict(org=org, name=name, urn=temba_contact.urns[0], region=region, group=group, language=temba_contact.language, facility_code=facility_code, uuid=temba_contact.uuid)
def sync(self, org): recent_contacts = Contact.objects.by_org(org).active() recent_contacts = recent_contacts.exclude(temba_modified_on=None) recent_contacts = recent_contacts.order_by('-temba_modified_on') most_recent = recent_contacts.first() sync_regions = [r.uuid for r in Region.get_all(org)] sync_groups = [g.uuid for g in Group.get_all(org)] created, updated, deleted, failed = sync_pull_contacts( org=org, contact_class=Contact, fields=(), delete_blocked=True, groups=sync_regions + sync_groups, last_time=most_recent.temba_modified_on if most_recent else None) org.set_task_result(TaskType.sync_contacts, { 'time': datetime_to_ms(timezone.now()), 'counts': { 'created': len(created), 'updated': len(updated), 'deleted': len(deleted), 'failed': len(failed), }, })
def calculate(): # org admins have implicit access to all regions if user.is_admin_for(org): return Region.get_all(org) else: return user.regions.filter(is_active=True)
def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') super(UserForm, self).__init__(*args, **kwargs) if self.user.get_org(): regions = Region.get_all(self.user.get_org()) self.fields['regions'].queryset = regions
def test_get_all(self): self.assertEqual(len(Region.get_all(self.unicef)), 3) self.assertEqual(len(Region.get_all(self.nyaruka)), 1)