def create_web_users(org_id, reporter_details, language_code): duplicate_entries = {} [duplicate_entries.update({item[0]: item[1]}) for item in reporter_details.items() if [val for val in reporter_details.values()].count(item[1]) > 1] errors = [] if len(duplicate_entries) > 0: content = json.dumps({'success': False, 'errors': errors, 'duplicate_entries': duplicate_entries}) organization = Organization.objects.get(org_id=org_id) dbm = get_database_manager_for_org(organization) existent_email_addresses = User.objects.filter(email__in=reporter_details.values()).values('email') if len(existent_email_addresses) > 0: for duplicate_email in existent_email_addresses: errors.append("User with email %s already exists" % duplicate_email['email']) content = json.dumps({'success': False, 'errors': errors, 'duplicate_entries': duplicate_entries}) if errors.__len__() == 0 and duplicate_entries.keys().__len__() == 0: for reporter_id, email in reporter_details.iteritems(): reporter_entity = contact_by_short_code(dbm, reporter_id) reporter_email = email.lower() set_email_for_contact(dbm, reporter_entity, email=reporter_email) user = User.objects.create_user(reporter_email, reporter_email, 'test123') group = Group.objects.filter(name="Data Senders")[0] user.groups.add(group) user.first_name = reporter_entity.value(NAME_FIELD) user.save() profile = NGOUserProfile(user=user, org_id=org_id, title="Mr", reporter_id=reporter_id.lower()) profile.save() send_email_to_data_sender(user, language_code, organization=organization) content = json.dumps({'success': True, 'message': "Users has been created"}) return content
def _set_contacts_email_address(dbm, request): web_user_id_email_map = {} for item in json.loads(request.POST['post_data']): contact = contact_by_short_code(dbm, item['reporter_id']) if contact.is_contact: set_email_for_contact(dbm, contact, item['email']) else: web_user_id_email_map.update({item['reporter_id']: item['email']}) return web_user_id_email_map
def test_should_add_email_to_datasender(self): email = "*****@*****.**" entity = Entity(self.manager, entity_type=REPORTER_ENTITY_TYPE, short_code="short_code") entity.save() delete_and_create_form_model(self.manager, GLOBAL_REGISTRATION_FORM_CODE) set_email_for_contact(self.manager, entity, email=email) entity_values = entity.data self.assertEquals(entity_values["email"]["value"], email)
def create_web_users(org_id, reporter_details, language_code): organization = Organization.objects.get(org_id=org_id) dbm = get_database_manager_for_org(organization) for reporter_id, email in reporter_details.iteritems(): reporter_entity = contact_by_short_code(dbm, reporter_id) reporter_email = email.lower() set_email_for_contact(dbm, reporter_entity, email=reporter_email) user = User.objects.create_user(reporter_email, reporter_email, 'test123') group = Group.objects.filter(name="Data Senders")[0] user.groups.add(group) user.first_name = reporter_entity.value(NAME_FIELD) user.save() profile = NGOUserProfile(user=user, org_id=org_id, title="Mr", reporter_id=reporter_id.lower()) profile.save() send_email_to_data_sender(user, language_code, organization=organization) return json.dumps({'success': True, 'message': "Users has been created"})