def handle(self, *args, **kwargs): sbm_url = kwargs['sbm_url'] sbm_token = kwargs['sbm_token'] check_subscription = kwargs['check_subscription'] query = kwargs['query'] start_date = kwargs['start_date'] if check_subscription: if not sbm_url: raise CommandError( 'Please make sure either the STAGE_BASED_MESSAGING_URL ' 'environment variable or --sbm-url is set.') if not sbm_token: raise CommandError( 'Please make sure either the STAGE_BASED_MESSAGING_TOKEN ' 'environment variable or --sbm-token is set.') client = StageBasedMessagingApiClient(sbm_token, sbm_url) filters = {"validated": True} if query: try: query_key, query_value = query.split(":", 1) except ValueError: raise CommandError( "Please use the format 'key':'value' for --reg-query") filters[query_key] = query_value if start_date: filters['created_at__gte'] = start_date registrations = Registration.objects.filter(**filters) for reg in registrations: requests = reg.get_subscription_requests() if requests.exists(): continue if check_subscription and self.count_subscriptions(client, reg): self.log(('Registration %s without Subscription Requests ' 'already has subscription (identity: %s). ' 'Skipping.') % (reg.pk, reg.registrant_id)) continue """ validate_subscribe() ensures no invalid registrations get subscriptions and creates the Subscription Request """ add_personally_identifiable_fields(reg) reg.save() validate_subscribe.apply_async( kwargs={"registration_id": str(reg.id)}) self.log("Attempted to repopulate subscriptions for registration " "%s" % (reg.id))
def handle(self, *args, **kwargs): registrations = Registration.objects.filter( validated=False, reg_type__in=("whatsapp_pmtct_prebirth", "pmtct_prebirth", "whatsapp_pmtct_postbirth", "pmtct_postbirth")).iterator() updates = 0 common_fields = ('language', 'mom_dob', 'operator_id') fields = { 'pmtct_prebirth': common_fields + ('edd', ), 'pmtct_postbirth': common_fields + ('baby_dob', ), 'whatsapp_pmtct_prebirth': common_fields + ('edd', ), 'whatsapp_pmtct_postbirth': common_fields + ('baby_dob', ), } for registration in registrations: add_personally_identifiable_fields(registration) related_regs = Registration.objects.filter( validated=True, registrant_id=registration.registrant_id ).exclude(reg_type__in=( "whatsapp_pmtct_prebirth", "pmtct_prebirth", "whatsapp_pmtct_postbirth", "pmtct_postbirth")).\ order_by('-created_at') resubmit = True for field in set(fields[registration.reg_type]): if registration.data.get(field): continue related_reg = related_regs.filter(data__has_key=field).first() if related_reg: registration.data[field] = related_reg.data[field] else: resubmit = False if resubmit: registration.data.pop("invalid_fields", None) registration.save() validate_subscribe.apply_async( kwargs={"registration_id": str(registration.id)}) updates += 1 self.log("%s registrations fixed and validated." % (updates))
def handle(self, *args, **kwargs): sbm_url = kwargs['sbm_url'] sbm_token = kwargs['sbm_token'] invalid_field = kwargs['invalid_field'] batch_size = kwargs['batch_size'] if not sbm_url: raise CommandError( 'Please make sure either the STAGE_BASED_MESSAGING_URL ' 'environment variable or --sbm-url is set.') if not sbm_token: raise CommandError( 'Please make sure either the STAGE_BASED_MESSAGING_TOKEN ' 'environment variable or --sbm-token is set.') client = StageBasedMessagingApiClient(sbm_token, sbm_url) registrations = Registration.objects.filter( validated=False, data__invalid_fields__contains=[invalid_field]) if batch_size is not None: registrations = registrations[:batch_size] count = 0 for reg in registrations.iterator(): self.log("Validating registration %s" % reg.id) if self.count_subscriptions(client, reg): self.log(('Identity %s already has subscription. Skipping.') % (reg.registrant_id)) continue # validate_subscribe() checks all data for the registration is # valid and creates the Subscription Request if not reg.data.get('msisdn_device', ''): add_personally_identifiable_fields(reg) reg.save() result = validate_subscribe(registration_id=str(reg.id)) if not result: reg.refresh_from_db() "Registration %s still invalid" % reg.id else: count += 1 self.log("Successfully revalidated %s registrations" % count)
def handle(self, *args, **options): from registrations.models import Registration from registrations.tasks import BasePushRegistrationToJembi since = options['since'] until = options['until'] source = options['source'] registration_uuids = options['registration'] if not (all([since, until]) or registration_uuids): raise CommandError( 'At a minimum please specify --since and --until ' 'or use --registration to specify one or more registrations') registrations = Registration.objects.all() if since and until: registrations = registrations.filter( created_at__gte=since, created_at__lte=until, validated=True) if source is not None: registrations = registrations.filter(source__pk=source) if registration_uuids: registrations = registrations.filter(pk__in=registration_uuids) self.stdout.write( 'Submitting %s registrations.' % (registrations.count(),)) for registration in registrations: add_personally_identifiable_fields(registration) registration.save() jembi_task = BasePushRegistrationToJembi\ .get_jembi_task_for_registration(registration) task = chain( jembi_task.si(str(registration.pk)), remove_personally_identifiable_fields.si(str(registration.pk)) ) task.delay() self.stdout.write(str(registration.pk)) self.stdout.write('Done.')
def handle(self, *options, **kwargs): self.identity_cache = {} hub_token = kwargs['hub_token'] hub_url = kwargs['hub_url'] id_store_token = kwargs['identity_store_token'] id_store_url = kwargs['identity_store_url'] group = kwargs['group'] headers = ['risk', 'count'] if group == 'msisdn': if not id_store_token or not id_store_url: raise CommandError( 'Please make sure the --identity-store-url and ' '--identity-store-token is set.') ids_client = IdentityStoreApiClient(id_store_token, id_store_url) headers = ['msisdn', 'risk'] output = self.stdout if kwargs['output']: output = open(kwargs['output'], 'w') results = defaultdict(int) def add_to_result(risk, reg): if group == "msisdn": identity = self.get_identity(ids_client, reg) if identity: details = identity.get('details', {}) default_addr_type = details.get('default_addr_type') if default_addr_type: addresses = details.get('addresses', {}) msisdns = addresses.get(default_addr_type, {}).keys() else: msisdns = [] results[', '.join(msisdns)] = 1 if risk == "high" else 0 else: results[risk] += 1 if hub_token and hub_url: hub_client = HubApiClient(hub_token, hub_url) for source in (1, 3): registrations = hub_client.get_registrations({ "source": source, "validated": True })['results'] for registration in registrations: mom_dob = registration["data"].get("mom_dob") if not mom_dob: identity = self.get_identity( ids_client, registration["registrant_id"]) if not identity: continue mom_dob = identity["details"].get("mom_dob") risk = get_risk_status(registration["reg_type"], mom_dob, registration["data"].get("edd")) add_to_result(risk, registration['registrant_id']) else: registrations = Registration.objects.filter( Q(reg_type='pmtct_postbirth') | Q(reg_type='pmtct_prebirth') | Q(reg_type='whatsapp_pmtct_postbirth') | Q(reg_type='whatsapp_pmtct_prebirth'), validated=True) for registration in registrations.iterator(): add_personally_identifiable_fields(registration) risk = get_risk_status(registration.reg_type, registration.data["mom_dob"], registration.data.get("edd")) add_to_result(risk, registration.registrant_id) writer = csv.DictWriter(output, headers) writer.writeheader() for risk, count in results.items(): writer.writerow({headers[0]: risk, headers[1]: count})