def test_get_organization_by_id(self): o1 = Organization(name="Google") o2 = Organization(name="Yahoo") o3 = Organization(name="Ubisoft") o1.save() o2.save() o3.save() #test typical cases self.assertIsNotNone(get_organization_by_id(o1.id)) self.assertIsNotNone(get_organization_by_id(o2.id)) self.assertIsNotNone(get_organization_by_id(o3.id)) self.assertEqual(get_organization_by_id(o1.id), o1) self.assertEqual(get_organization_by_id(o2.id), o2) self.assertEqual(get_organization_by_id(o3.id), o3) self.assertIsNone(get_organization_by_id(100)) self.assertIsNone(get_organization_by_id(200)) self.assertIsNone(get_organization_by_id(300)) self.assertNotEqual(get_organization_by_id(100), o1) self.assertNotEqual(get_organization_by_id(200), o1) self.assertNotEqual(get_organization_by_id(300), o1) self.assertNotEqual(get_organization_by_id(100), o2) self.assertNotEqual(get_organization_by_id(200), o2) self.assertNotEqual(get_organization_by_id(300), o2) self.assertNotEqual(get_organization_by_id(100), o3) self.assertNotEqual(get_organization_by_id(200), o3) self.assertNotEqual(get_organization_by_id(300), o3)
def test_get_organizations_ordered_by_name(self): o1 = Organization(name="Google") o2 = Organization(name="Yahoo") o3 = Organization(name="Ubisoft") o4 = Organization(name="IBM") o5 = Organization(name="Cisco") o1.save() o2.save() o3.save() o4.save() o5.save() #test typical cases organization_list = get_organizations_ordered_by_name() self.assertIsNotNone(organization_list) self.assertIn(o1, organization_list) self.assertIn(o2, organization_list) self.assertIn(o3, organization_list) self.assertIn(o4, organization_list) self.assertIn(o5, organization_list) self.assertEqual(len(organization_list), 5) #test order self.assertEqual(organization_list[0], o5) self.assertEqual(organization_list[1], o1) self.assertEqual(organization_list[2], o4) self.assertEqual(organization_list[3], o3) self.assertEqual(organization_list[4], o2)
def test_get_organization_by_name(self): o1 = Organization(name="Google") o2 = Organization(name="Yahoo") o3 = Organization(name="Ubisoft") o1.save() o2.save() o3.save() #test typical cases self.assertIsNotNone(get_organization_by_name(o1.name)) self.assertIsNotNone(get_organization_by_name(o2.name)) self.assertIsNotNone(get_organization_by_name(o3.name)) self.assertEqual(get_organization_by_name(o1.name), o1) self.assertEqual(get_organization_by_name(o2.name), o2) self.assertEqual(get_organization_by_name(o3.name), o3) self.assertIsNone(get_organization_by_name("Apple")) self.assertIsNone(get_organization_by_name("IBM")) self.assertIsNone(get_organization_by_name("Cisco")) self.assertNotEqual(get_organization_by_name("Apple"), o1) self.assertNotEqual(get_organization_by_name("IBM"), o1) self.assertNotEqual(get_organization_by_name("Cisco"), o1) self.assertNotEqual(get_organization_by_name("Apple"), o2) self.assertNotEqual(get_organization_by_name("IBM"), o2) self.assertNotEqual(get_organization_by_name("Cisco"), o2) self.assertNotEqual(get_organization_by_name("Apple"), o3) self.assertNotEqual(get_organization_by_name("IBM"), o3) self.assertNotEqual(get_organization_by_name("Cisco"), o3)
def setup_test_data(cls): cls.o1 = Organization(name="Google") cls.o2 = Organization(name="Yahoo") cls.o3 = Organization(name="Ubisoft") cls.o1.save() cls.o2.save() cls.o3.save()
def setup_test_data(cls): cls.o1 = Organization(name="Google") cls.o2 = Organization(name="Yahoo") cls.o1.save() cls.o2.save() volunteer_1 = [ 'Yoshi', "Yoshi", "Turtle", "Mario Land", "Nintendo Land", "Nintendo State", "Nintendo Nation", "2374983247", "*****@*****.**" ] cls.v1 = create_volunteer_with_details(volunteer_1, cls.o2)
def setUp(self): self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.client.login(username='******', password='******') self.organization = Organization(name='Foo Org') self.organization.save() assign_admin_perms(self.organization, self.user) self.url = reverse('api_retrieve_organization', args=(self.organization.id,))
def save_obj(vals): # prepare data nome = ' '.join([ get_field(vals, 'S'), get_field(vals, 'T').title(), get_field(vals, 'U').title() ]) desc = desc_localization(vals) desc += desc_atendimento(vals) tags = [ 'Escola', get_field(vals, 'S'), get_field(vals, 'H').title(), get_field(vals, 'J').title() ] contato = desc_contato(vals) user = User.objects.get(username='******') now = datetime.now() # save data o = Organization() o.name = nome o.description = desc o.contact = contato o.creator = user o.creation_date = now o.save() # save m2m relations o.categories.add(category_id) for tag in tags: o.tags.add(tag)
def edit_organization(request, id='', *arg, **kwargs): organization = get_object_or_None(Organization, pk=id) or Organization() geojson = create_geojson([organization], convert=False) if geojson and geojson.get('features'): geojson['features'][0]['properties']['userCanEdit'] = True geojson = json.dumps(geojson) def on_get(request, form): form = FormOrganizationGeoRef(instance=organization) form.helper.form_action = reverse('edit_organization', kwargs={'id': organization.id}) return form def on_after_save(request, obj): return { 'redirect': reverse('view_organization', kwargs={'id': obj.id}) } return { 'on_get': on_get, 'on_after_save': on_after_save, 'geojson': geojson, 'organization': organization }
def registration_view(request): if request.method == 'POST': if request.data.get('name', "") == "": return Response({"name": "Can't be empty"}, status=status.HTTP_400_BAD_REQUEST) num_count = Organization.objects.filter( name=request.data.get('name', "")).count() if num_count != 0: return Response({"name": "Already Exists"}, status=status.HTTP_400_BAD_REQUEST) new_org = Organization(name=request.data.get('name', "")) serializer = OrganizationSerializer(new_org, data=request.data) data = {} if serializer.is_valid(): temp_org = serializer.save() profile = get_object_or_404(Profile, user=request.user) profile.organizations.add(temp_org) data['response'] = "Successfully Signed Up" data['id'] = temp_org.id data['name'] = temp_org.name data['title'] = temp_org.title data['date_created'] = temp_org.date_created.astimezone( timezone('Asia/Kolkata')).strftime("%Y-%m-%d %H:%M:%S") data['unique_code'] = temp_org.unique_code return Response(data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_create_invite(self): self.org = Organization(name='Foo Org') self.org.save() assign_admin_perms(self.org, self.user) url = reverse('api_invite_user', args=(self.org.id, )) data = {"email": "*****@*****.**"} resp = self.client.post(url, data, format='json') self.assertEquals(20, len(resp.data['token'])) self.assertEquals(resp.data['user']['id'], self.user.id)
def post(self): form = AddOrganizationForm() if not form.validate_on_submit(): return self.get() org = Organization(date=datetime.datetime.now().date(), owner_id=current_user.id) form.populate_obj(org) org.save(add=True) return redirect(url_for('organization.organizations'))
def setUp(self): self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.org = Organization(name='Foo Org') self.org.save() assign_admin_perms(self.org, self.user) self.invite = OrganizationInvite(email='*****@*****.**', organization=self.org, user=self.user) self.invite.token = get_random_string( length=20, allowed_chars='ABCDEFGHJKMNPQRST23456789') self.invite.save()
def post(self): args = self.parser.parse_args() org = Organization(name=args['name'], owner_id=self.authorized_user.id, org_type=args['org_type'], org_desc=args['org_desc']) org.save(add=True) return jsonify({ 'adding': 'OK', 'organization': org.to_dict(only=('id', 'name', 'creation_date', 'owner_id', 'org_type', 'org_desc', 'api_token')) })
def setup_test_data(cls): cls.o1 = Organization(name="Google") cls.o2 = Organization(name="Yahoo") cls.o1.save() cls.o2.save() city_name = 'Bothell' state_name = 'Washington' country_name = 'United States' country = get_country_by_name(country_name) state = get_state_by_name(state_name) city = get_city_by_name(city_name) volunteer_1 = { 'username': '******', 'first_name': "Yoshi", 'last_name': "Turtle", 'address': "Mario Land", 'city': city, 'state': state, 'country': country, 'phone_number': "2374983247", 'email': "*****@*****.**" } cls.v1 = create_volunteer_with_details(volunteer_1, cls.o2)
def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(request.POST['organization_id']) organization_name = request.POST['organization_name'] # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: # organization_query = Organization.objects.all() # organization_query = organization_query.filter(id=organization_id) organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e) try: if organization_on_stage_found: # Update organization_on_stage.name = organization_name organization_on_stage.save() messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new organization_on_stage = Organization(name=organization_name, ) organization_on_stage.save() messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: handle_record_not_saved_exception(e) messages.add_message(request, messages.ERROR, 'Could not save organization.') return HttpResponseRedirect( reverse('organization:organization_list', args=()))
def setUp(self): self.organization = Organization(name='Foo Org') self.organization.save() self.phone_number = PhoneNumber(organization=self.organization, twilio_sid='xxx', phone_number='+15550009999') self.phone_number.save() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.user.userprofile.phone_number = "5556667777" self.user.userprofile.save() self.conference = Conference(organization=self.organization, name='My Conf', phone_number=self.phone_number) self.conference.save()
def setUp(self): self.organization = Organization(name='Foo Org') self.organization.save() self.phone_number = PhoneNumber(organization=self.organization, twilio_sid='xxx', phone_number='+15550009999') self.phone_number.save() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.user.userprofile.phone_number = "5556667777" self.user.userprofile.save() self.client.login(usernam='foo', password='******') self.url = reverse('api_create_conference', args=(self.organization.id, ))
def save_obj(vals): tipo, name, endereco, contato = vals contact = "{}\n{}".format(endereco, contato) o = Organization() o.name = name.title().replace('Emei', 'EMEI').replace('Emef', 'EMEF').replace('Ee', 'EE') o.contact = contact o.description = '' o.creator = elaste o.creation_date = datetime.now() o.save() for t in [tipo, 'Jacareí']: o.tags.add(t) o.community.add(jacarei)
def addOrganizationPageView(request): if request.method == 'POST': new_organization = Organization() new_organization.company_name = request.POST.get('company_name') new_organization.email = request.POST.get('company_email') new_organization.address = request.POST.get('company_streetadd') new_organization.organization_id = random.randint(210, 10000) new_organization.save() organization_data = Organization.objects.all() context = { 'all_organizations': organization_data, 'new_organization': new_organization } return render(request, 'organization/vieworganization.html', context) else: return HttpResponse("NOT FOUND")
def registration_view(request): # For signup if request.method == 'POST': serializer = RegistrationSerializer(data=request.data) data = {} if serializer.is_valid(): user = serializer.save() refresh = RefreshToken.for_user(user) profile = get_object_or_404(Profile, user=user) org = Organization(name=user.username) org.save() profile.organizations.add(org) data['response'] = "Successfully Signed Up" data['username'] = user.username data['userid'] = user.id data['refresh'] = str(refresh) data['access'] = str(refresh.access_token) return Response(data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_search_volunteers(self): o1 = Organization(name="Apple") o2 = Organization(name="Google") o1.save() o2.save() self.v1.organization = o1 self.v2.organization = o2 self.v3.unlisted_organization = "Government of Canada" self.v1.save() self.v2.save() self.v3.save() # if no search parameters are given, # it returns all volunteers search_list = search_volunteers("", "", "", "", "", "") self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 3) self.assertIn(self.v1, search_list) self.assertIn(self.v2, search_list) self.assertIn(self.v3, search_list) search_list = search_volunteers(None, None, None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 3) self.assertIn(self.v1, search_list) self.assertIn(self.v2, search_list) self.assertIn(self.v3, search_list) # test exact search search_list = search_volunteers("Yoshi", "Turtle", "Nintendo Land", "Nintendo State", "Nintendo Nation", "Apple") self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 1) self.assertIn(self.v1, search_list) self.assertNotIn(self.v2, search_list) self.assertNotIn(self.v3, search_list) # test partial search search_list = search_volunteers("Yoshi", None, None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 1) self.assertIn(self.v1, search_list) self.assertNotIn(self.v2, search_list) self.assertNotIn(self.v3, search_list) search_list = search_volunteers(None, "Doe", None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 2) self.assertIn(self.v3, search_list) self.assertIn(self.v2, search_list) # test no search matches search_list = search_volunteers("Billy", "Doe", "Montreal", "Quebec", "Canada", "Ubisoft") self.assertEqual(len(search_list), 0) self.assertNotIn(self.v1, search_list) self.assertNotIn(self.v2, search_list) self.assertNotIn(self.v3, search_list)
def save_org(vals): e = get_field(vals, 'E').strip() d = get_field(vals, 'D').strip() if d: name = '{} - {}'.format(e, d) else: name = e if not name: return distrito = get_field(vals, 'M').strip() if distrito: desc = """ #### Localização {C} fica {M} {N}, {O} do município {Q}, {R}. """.format(**format_fields(vals, 'C', 'M', 'N', 'O', 'Q', 'R')) else: desc = """ #### Localização {C} fica no bairro {L} do município {Q}, {R}. """.format(**format_fields(vals, 'C', 'L', 'Q', 'R')) desc += """ #### Serviços e Programas {AM} {AN} #### Funcionamento * Horário: {BG} * Situação: {BJ} #### Área de abrangência {BF} #### Registros e certificações """.format(**format_fields(vals, 'AM', 'AN', 'BG', 'BJ', 'BF')) g = get_field(vals, 'G') bm = get_field(vals, 'BM') h = get_field(vals, 'H') bh = get_field(vals, 'BH') z = get_field(vals, 'Z') if g: desc += """ - Código da Escola (INEP): {G} """.format(G=g) if bm: desc += """ - Código Municipal da Escola: {BM} """.format(BM=bm) if h: desc += """ - Cadastro Nacional de Estabelecimentos de Saúde (CNES): {H} """.format(H=h) if bh: desc += """ - Código do Conselho Tutelar: {BH} """.format(BH=bh) if z: desc += """ - CNPJ: {Z} """.format(Z=z) desc += """ #### Órgão superior - {BO} #### Pessoas de contato {W}, {X} #### Outros contatos Fax: ({S}) {U} #### Referências - [{AE}]({AF} "{AG}"), consultado em {AH} """.format(**format_fields(vals, 'BO', 'W', 'X', 'S', 'U', 'AE', 'AF', 'AG', 'AH')) k = get_field(vals, 'K').strip() if k: k = ', ' + k contact = """ Endereço: {I}, {J} {K} | {L}, {P} | {Q}-{R} Telefone: ({S}) {T} E-mail: {V} """.format(K=k, **format_fields(vals, 'I', 'J', 'L', 'P', 'Q', 'R', 'S', 'T', 'V')) link = get_field(vals, 'Y') tags = [ get_field(vals, f) for f in [ 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'BN', 'BO', 'BP', 'BQ' ] ] tags = filter(bool, tags) # Público-Alvo public = [get_field(vals, f) for f in ['AY', 'AZ', 'BA', 'BB', 'BC', 'BD']] public = filter(bool, public) now = datetime.now() user_id = get_field(vals, 'B').split('/')[-1] creator = User.objects.get(pk=user_id) categories = [ get_field(vals, column) for column in ['AA', 'AB', 'AC', 'AD'] ] categories = filter(bool, categories) categorias = [] for cat in categories: c = OrganizationCategoryTranslation.objects.filter(name__icontains=cat) if c.count(): categorias.append(c[0].category) longitude, latitude = get_field(vals, 'BL'), get_field(vals, 'BK') if longitude and latitude: x, y = map(float, [ c.replace(',', '.').replace(' ', '') for c in (longitude, latitude) ]) geom = [] geom.append('%s,%s' % (x + 0.0005, y + 0.0005)) geom.append('%s,%s' % (x + 0.0005, y - 0.0005)) geom.append('%s,%s' % (x - 0.0005, y - 0.0005)) geom.append('%s,%s' % (x - 0.0005, y + 0.0005)) geom.append('%s,%s' % (x + 0.0005, y + 0.0005)) coords = '' for i, coord in enumerate(geom): coord = coord.split(',') coords += '%s %s' % (coord[1], coord[0]) if not i == len(geom) - 1: coords += ', ' geometry = "GEOMETRYCOLLECTION( POLYGON (( %s )))" % coords else: geometry = '' o = Organization() while Organization.objects.filter(name=name).count(): name += ' ' o.name = name o.description = desc o.contact = contact o.link = link o.creation_date = now o.creator = creator o.save() if geometry: OrganizationBranch.objects.create(name='sede', geometry=geometry, organization=o) for t in tags: o.tags.add(t) for p in public: p, c = TargetAudience.objects.get_or_create(name=p) o.target_audiences.add(p) for cat in categorias: o.categories.add(cat) proj_slug = get_field(vals, 'BE').split('/')[-1] if proj_slug: proj = Project.objects.get(slug=proj_slug) ProjectRelatedObject.objects.get_or_create( project=proj, content_type=ct_organization, object_id=o.id) print 'OK - ', name
def test_search_volunteers(self): u1 = User.objects.create_user('Yoshi') u2 = User.objects.create_user('Ashley') u3 = User.objects.create_user('Zelda') o1 = Organization(name="Apple") o2 = Organization(name="Google") o1.save() o2.save() v1 = Volunteer(first_name="Yoshi", last_name="Doe", address="7 Oak Street", city="Elmgrove", state="California", country="USA", phone_number="23454545", organization=o1, email="*****@*****.**", user=u1) v2 = Volunteer(first_name="Ashley", last_name="Doe", address="7 Alpine Street", city="Maplegrove", state="Wyoming", country="USA", phone_number="23454545", organization=o2, email="*****@*****.**", user=u2) v3 = Volunteer(id=999, first_name="Zelda", last_name="Doe", address="7 Elm Street", city="Oakgrove", state="California", country="USA", phone_number="23454545", unlisted_organization="Government of Canada", email="*****@*****.**", user=u3) v1.save() v2.save() v3.save() #if no search parameters are given, it returns all volunteers search_list = search_volunteers("", "", "", "", "", "") self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 3) self.assertIn(v1, search_list) self.assertIn(v2, search_list) self.assertIn(v3, search_list) search_list = search_volunteers(None, None, None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 3) self.assertIn(v1, search_list) self.assertIn(v2, search_list) self.assertIn(v3, search_list) #test exact search search_list = search_volunteers("Yoshi", "Doe", "Elmgrove", "California", "USA", "Apple") self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 1) self.assertIn(v1, search_list) self.assertNotIn(v2, search_list) self.assertNotIn(v3, search_list) #test partial search search_list = search_volunteers("Yoshi", None, None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 1) self.assertIn(v1, search_list) self.assertNotIn(v2, search_list) self.assertNotIn(v3, search_list) search_list = search_volunteers(None, "Doe", None, None, None, None) self.assertNotEqual(search_list, False) self.assertEqual(len(search_list), 3) self.assertIn(v1, search_list) self.assertIn(v2, search_list) self.assertIn(v3, search_list) #test no search matches search_list = search_volunteers("Billy", "Doe", "Montreal", "Quebec", "Canada", "Ubisoft") self.assertEqual(len(search_list), 0) self.assertNotIn(v1, search_list) self.assertNotIn(v2, search_list) self.assertNotIn(v3, search_list)
def import_we_vote_organizations_from_json(request, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ if load_from_uri: # Request json file from We Vote servers logger.info("Loading Organizations from We Vote Master servers") request = requests.get( ORGANIZATIONS_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable }) structured_json = json.loads(request.text) else: # Load saved json from local file logger.info("Loading organizations from local file") with open(ORGANIZATIONS_JSON_FILE) as json_data: structured_json = json.load(json_data) for one_organization in structured_json: logger.debug( u"id_we_vote: {id_we_vote}, name: {name}, url: {url}".format( **one_organization)) # Make sure we have the minimum required variables if len(one_organization["id_we_vote"]) == 0 or len( one_organization["name"]) == 0: continue # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: if len(one_organization["id_we_vote"]) > 0: organization_query = Organization.objects.filter( id_we_vote=one_organization["id_we_vote"]) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True elif len(one_organization["name"]) > 0: organization_query = Organization.objects.filter( name=one_organization["name"]) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if organization_on_stage_found: # Update organization_on_stage.id_we_vote = one_organization[ "id_we_vote"] organization_on_stage.name = one_organization["name"] organization_on_stage.url = one_organization["url"] organization_on_stage.save() messages.add_message( request, messages.INFO, u"Organization updated: {name}".format( name=one_organization["name"])) else: # Create new organization_on_stage = Organization( id_we_vote=one_organization["id_we_vote"], name=one_organization["name"], url=one_organization["url"], ) organization_on_stage.save() messages.add_message( request, messages.INFO, u"New organization imported: {name}".format( name=one_organization["name"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message( request, messages.ERROR, u"Could not save Organization, id_we_vote: {id_we_vote}, name: {name}, url: {url}" .format( id_we_vote=one_organization["id_we_vote"], name=one_organization["name"], url=one_organization["url"], ))
def related_items(request, id=''): organization = get_object_or_None(Organization, pk=id) or Organization() geojson = create_geojson(organization.related_items + [organization]) return {'organization': organization, 'geojson': geojson}
def save_org(vals): name = "{} {}".format(get_field(vals, 'D'), get_field(vals, 'E')) desc = """ ####Localização {C} fica no distrito {K} da Subprefeitura {L} da capital paulista. ####Horário de Atendimento {Y} """.format( C=get_field(vals, 'C'), K=get_field(vals, 'K'), L=get_field(vals, 'L'), Y=get_field(vals, 'Y'), ) if get_field(vals, 'G') or get_field(vals, 'S'): desc += """ ####Registros e Certificações """ if get_field(vals, 'G'): desc += """ - Código INEP da Instituição de Ensino: {G}""".format(G=get_field(vals, 'G')) if get_field(vals, 'S'): desc += """ - Cadastro Nacional de Pessoa Jurídica (CNPJ): {S}""".format( S=get_field(vals, 'S')) desc += """ ####Referência - [{U}]({V} "{W}"), {X} """.format( U=get_field(vals, 'U'), V=get_field(vals, 'V'), W=get_field(vals, 'W'), X=get_field(vals, 'X'), ) contact = """ {H}, {I} | {J} CEP: {M}, São Paulo-SP Fone: {O} {P} Email: {Q} """.format( H=get_field(vals, 'H'), I=get_field(vals, 'I'), J=get_field(vals, 'J'), M=get_field(vals, 'M'), O=get_field(vals, 'O'), P=get_field(vals, 'P'), Q=get_field(vals, 'Q'), ) link = get_field(vals, 'R') cat = get_field(vals, 'T') if cat == 'Cultura': cat = 'Cultura e Arte' categoria = '' if cat: cat = OrganizationCategoryTranslation.objects.filter( name__icontains=cat) if cat.count(): categoria = cat[0].category # Palavras-Chave tags = [get_field(vals, f) for f in ['Z', 'AA', 'AB', 'AC', 'AD', 'AE']] # Público-Alvo public = [ get_field(vals, f) for f in ['AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP'] ] now = datetime.now() o = Organization() o.name = name o.description = desc o.contact = contact o.link = link o.category = categoria o.creation_date = now o.creator = mariarita o.save() for t in tags: o.tags.add(t) for p in public: p, c = TargetAudience.objects.get_or_create(name=p) o.target_audiences.add(p) print 'OK'
def organizations_import_from_structured_json(structured_json): organizations_saved = 0 organizations_updated = 0 organizations_not_processed = 0 for one_organization in structured_json: # We have already removed duplicate organizations # Make sure we have the minimum required variables if not positive_value_exists(one_organization["we_vote_id"]) or \ not positive_value_exists(one_organization["organization_name"]): organizations_not_processed += 1 continue # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: if positive_value_exists(one_organization["we_vote_id"]): organization_query = Organization.objects.filter(we_vote_id=one_organization["we_vote_id"]) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Organization.DoesNotExist: # No problem that we aren't finding existing organization pass except Exception as e: handle_record_not_found_exception(e, logger=logger) # We want to skip to the next org continue try: we_vote_id = one_organization["we_vote_id"] organization_name = one_organization["organization_name"] \ if 'organization_name' in one_organization else False organization_website = one_organization["organization_website"] \ if 'organization_website' in one_organization else False organization_email = one_organization["organization_email"] \ if 'organization_email' in one_organization else False organization_contact_name = one_organization["organization_contact_name"] \ if 'organization_contact_name' in one_organization else False organization_facebook = one_organization["organization_facebook"] \ if 'organization_facebook' in one_organization else False organization_image = one_organization["organization_image"] \ if 'organization_image' in one_organization else False state_served_code = one_organization["state_served_code"] \ if 'state_served_code' in one_organization else False vote_smart_id = one_organization["vote_smart_id"] \ if 'vote_smart_id' in one_organization else False organization_description = one_organization["organization_description"] \ if 'organization_description' in one_organization else False organization_address = one_organization["organization_address"] \ if 'organization_address' in one_organization else False organization_city = one_organization["organization_city"] \ if 'organization_city' in one_organization else False organization_state = one_organization["organization_state"] \ if 'organization_state' in one_organization else False organization_zip = one_organization["organization_zip"] \ if 'organization_zip' in one_organization else False organization_phone1 = one_organization["organization_phone1"] \ if 'organization_phone1' in one_organization else False organization_phone2 = one_organization["organization_phone2"] \ if 'organization_phone2' in one_organization else False organization_fax = one_organization["organization_fax"] \ if 'organization_fax' in one_organization else False twitter_user_id = one_organization["twitter_user_id"] \ if 'twitter_user_id' in one_organization else False organization_twitter_handle = one_organization["organization_twitter_handle"] \ if 'organization_twitter_handle' in one_organization else False twitter_name = one_organization["twitter_name"] \ if 'twitter_name' in one_organization else False twitter_location = one_organization["twitter_location"] \ if 'twitter_location' in one_organization else False twitter_followers_count = one_organization["twitter_followers_count"] \ if 'twitter_followers_count' in one_organization else False twitter_profile_image_url_https = one_organization["twitter_profile_image_url_https"] \ if 'twitter_profile_image_url_https' in one_organization else False twitter_profile_background_image_url_https = \ one_organization["twitter_profile_background_image_url_https"] \ if 'twitter_profile_background_image_url_https' in one_organization else False twitter_profile_banner_url_https = one_organization["twitter_profile_banner_url_https"] \ if 'twitter_profile_banner_url_https' in one_organization else False twitter_description = one_organization["twitter_description"] \ if 'twitter_description' in one_organization else False wikipedia_page_id = one_organization["wikipedia_page_id"] \ if 'wikipedia_page_id' in one_organization else False wikipedia_page_title = one_organization["wikipedia_page_title"] \ if 'wikipedia_page_title' in one_organization else False wikipedia_thumbnail_url = one_organization["wikipedia_thumbnail_url"] \ if 'wikipedia_thumbnail_url' in one_organization else False wikipedia_thumbnail_width = one_organization["wikipedia_thumbnail_width"] \ if 'wikipedia_thumbnail_width' in one_organization else False wikipedia_thumbnail_height = one_organization["wikipedia_thumbnail_height"] \ if 'wikipedia_thumbnail_height' in one_organization else False wikipedia_photo_url = one_organization["wikipedia_photo_url"] \ if 'wikipedia_photo_url' in one_organization else False ballotpedia_page_title = one_organization["ballotpedia_page_title"] \ if 'ballotpedia_page_title' in one_organization else False ballotpedia_photo_url = one_organization["ballotpedia_photo_url"] \ if 'ballotpedia_photo_url' in one_organization else False organization_type = one_organization["organization_type"] \ if 'organization_type' in one_organization else False if organization_on_stage_found: # Update existing organization in the database if we_vote_id is not False: organization_on_stage.we_vote_id = we_vote_id if organization_name is not False: organization_on_stage.organization_name = organization_name else: # Create new organization_on_stage = Organization( we_vote_id=one_organization["we_vote_id"], organization_name=one_organization["organization_name"], ) # Now save all of the fields in common to updating an existing entry vs. creating a new entry if organization_website is not False: organization_on_stage.organization_website = organization_website if organization_email is not False: organization_on_stage.organization_email = organization_email if organization_contact_name is not False: organization_on_stage.organization_contact_name = organization_contact_name if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_image is not False: organization_on_stage.organization_image = organization_image if state_served_code is not False: organization_on_stage.state_served_code = state_served_code if vote_smart_id is not False: organization_on_stage.vote_smart_id = vote_smart_id if organization_description is not False: organization_on_stage.organization_description = organization_description if organization_address is not False: organization_on_stage.organization_address = organization_address if organization_city is not False: organization_on_stage.organization_city = organization_city if organization_state is not False: organization_on_stage.organization_state = organization_state if organization_zip is not False: organization_on_stage.organization_zip = organization_zip if organization_phone1 is not False: organization_on_stage.organization_phone1 = organization_phone1 if organization_phone2 is not False: organization_on_stage.organization_phone2 = organization_phone2 if organization_fax is not False: organization_on_stage.organization_fax = organization_fax if twitter_user_id is not False: organization_on_stage.twitter_user_id = twitter_user_id if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if twitter_name is not False: organization_on_stage.twitter_name = twitter_name if twitter_location is not False: organization_on_stage.twitter_location = twitter_location if twitter_followers_count is not False: organization_on_stage.twitter_followers_count = twitter_followers_count if twitter_profile_image_url_https is not False: organization_on_stage.twitter_profile_image_url_https = twitter_profile_image_url_https if twitter_profile_background_image_url_https is not False: organization_on_stage.twitter_profile_background_image_url_https = \ twitter_profile_background_image_url_https if twitter_profile_banner_url_https is not False: organization_on_stage.twitter_profile_banner_url_https = twitter_profile_banner_url_https if twitter_description is not False: organization_on_stage.twitter_description = twitter_description if wikipedia_page_id is not False: organization_on_stage.wikipedia_page_id = wikipedia_page_id if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_thumbnail_url is not False: organization_on_stage.wikipedia_thumbnail_url = wikipedia_thumbnail_url if wikipedia_thumbnail_width is not False: organization_on_stage.wikipedia_thumbnail_width = wikipedia_thumbnail_width if wikipedia_thumbnail_height is not False: organization_on_stage.wikipedia_thumbnail_height = wikipedia_thumbnail_height if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if ballotpedia_page_title is not False: organization_on_stage.ballotpedia_page_title = ballotpedia_page_title if ballotpedia_photo_url is not False: organization_on_stage.ballotpedia_photo_url = ballotpedia_photo_url if organization_type is not False: organization_on_stage.organization_type = organization_type organization_on_stage.save() if organization_on_stage_found: organizations_updated += 1 else: organizations_saved += 1 except Exception as e: organizations_not_processed += 1 organizations_results = { 'success': True, 'status': "ORGANIZATION_IMPORT_PROCESS_COMPLETE", 'saved': organizations_saved, 'updated': organizations_updated, 'not_processed': organizations_not_processed, } return organizations_results
def save_obj(vals): imp, transf, comunidade, name, desc, folder, tipo, geom = vals geom = eval(geom) print type(geom) print 'saving: ', vals if ',' in comunidade: comunidade = comunidade.split(',')[0] if transf == 'sim': tipo = 'polys' x, y, z = map(float, geom[0].split(',')) geom = [] geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z)) geom.append('%s,%s,%s' % (x + 0.0005, y - 0.0005, z)) geom.append('%s,%s,%s' % (x - 0.0005, y - 0.0005, z)) geom.append('%s,%s,%s' % (x - 0.0005, y + 0.0005, z)) geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z)) if tipo == 'polys': coords = '' for i, coord in enumerate(geom): x, y, z = coord.split(',') coords += '%s %s' % (y, x) if not i == len(geom) - 1: coords += ', ' geo_ref = 'GEOMETRYCOLLECTION ( POLYGON (( %s )))' % coords elif tipo == 'point': x, y, z = geom[0].split(',') geo_ref = 'GEOMETRYCOLLECTION ( POINT ( %s %s))' % (y, x) if imp == 'R': r = Resource() r.name = name r.description = desc if comunidade: c = Community.objects.get(slug=comunidade) r.community = c r.geometry = geo_ref r.save() elif imp == 'N': n = Need() n.title = name n.description = desc if comunidade: c = Community.objects.get(slug=comunidade) n.community = c n.geometry = geo_ref # n.target_audiences.add(1) n.save() elif imp == 'O': if name: o = Organization() o.name = name o.description = desc o.save() if comunidade: c = Community.objects.get(slug=comunidade) o.community.add(c) b = OrganizationBranch() b.name = name + ' - sede' b.geometry = geo_ref b.organization = o b.save()
def organizations_import_from_sample_file( request=None, load_from_uri=False): # TODO FINISH BUILDING/TESTING THIS """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # logger.info("Loading Organizations from We Vote Master servers") # request = requests.get(ORGANIZATIONS_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text) # else: # Load saved json from local file logger.info("Loading organizations from local file") with open( 'organization/import_data/organizations_sample.json') as json_data: structured_json = json.load(json_data) organizations_saved = 0 organizations_updated = 0 organizations_not_processed = 0 for one_organization in structured_json: logger.debug( u"we_vote_id: {we_vote_id}, organization_name: {organization_name}, " u"organization_website: {organization_website}".format( **one_organization)) # Make sure we have the minimum required variables if not positive_value_exists(one_organization["we_vote_id"]) or \ not positive_value_exists(one_organization["organization_name"]): organizations_not_processed += 1 continue # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: if positive_value_exists(one_organization["we_vote_id"]): organization_query = Organization.objects.filter( we_vote_id=one_organization["we_vote_id"]) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True elif positive_value_exists(one_organization["organization_name"]): organization_query = Organization.objects.filter( organization_name=one_organization["organization_name"]) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Organization.DoesNotExist: # No problem that we aren't finding existing organization pass except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if organization_on_stage_found: # Update organization_on_stage.we_vote_id = one_organization[ "we_vote_id"] organization_on_stage.organization_name = one_organization[ "organization_name"] organization_on_stage.organization_website = one_organization[ "organization_website"] organization_on_stage.organization_twitter_handle = one_organization[ "organization_twitter_handle"] organization_on_stage.save() # messages.add_message(request, messages.INFO, u"Organization updated: {organization_name}".format( # organization_name=one_organization["organization_name"])) organizations_updated += 1 else: # Create new organization_on_stage = Organization( we_vote_id=one_organization["we_vote_id"], organization_name=one_organization["organization_name"], organization_twitter_handle=one_organization[ "organization_twitter_handle"], organization_website=one_organization[ "organization_website"], organization_email=one_organization["organization_email"] if 'organization_email' in one_organization else '', organization_facebook=one_organization[ "organization_facebook"] if 'organization_facebook' in one_organization else '', organization_image=one_organization["organization_image"] if 'organization_image' in one_organization else '', ) organization_on_stage.save() organizations_saved += 1 # messages.add_message(request, messages.INFO, u"New organization imported: {organization_name}".format( # organization_name=one_organization["organization_name"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) if request is not None: messages.add_message( request, messages.ERROR, "Could not save Organization, we_vote_id: {we_vote_id}, " "organization_name: {organization_name}, " "organization_website: {organization_website}".format( we_vote_id=one_organization["we_vote_id"], organization_name=one_organization[ "organization_name"], organization_website=one_organization[ "organization_website"], )) organizations_not_processed += 1 organizations_results = { 'saved': organizations_saved, 'updated': organizations_updated, 'not_processed': organizations_not_processed, } return organizations_results
def save_obj(vals): imp, transf, comunidade, name, desc, folder, tipo, geom = vals geom = eval(geom) print 'saving: ', vals user = User.objects.get(username='******') now = datetime.now() # Transform geometry data type if transf == 'sim': tipo = 'polys' x, y, z = map(float, geom[0].split(',')) geom = [] geom.append('%s,%s,%s' % (x + 0.0003, y + 0.0003, z)) geom.append('%s,%s,%s' % (x + 0.0003, y - 0.0003, z)) geom.append('%s,%s,%s' % (x - 0.0003, y - 0.0003, z)) geom.append('%s,%s,%s' % (x - 0.0003, y + 0.0003, z)) geom.append('%s,%s,%s' % (x + 0.0003, y + 0.0003, z)) # build geomtry info if tipo == 'polys': coords = '' for i, coord in enumerate(geom): x, y, z = coord.split(',') coords += '%s %s' % (y, x) if not i == len(geom) - 1: coords += ', ' geo_ref = 'GEOMETRYCOLLECTION ( POLYGON (( %s )))' % coords elif tipo == 'point': x, y, z = geom[0].split(',') geo_ref = 'GEOMETRYCOLLECTION ( POINT ( %s %s))' % (y, x) # save Resource if imp == 'R': #fix comunidade tags = [] if ',' in comunidade: split = [c.strip() for c in comunidade.split(',')] comunidade, tags = split[0], split[1:] r = Resource() r.name = name r.description = desc # comunity is the firt if comunidade: c = Community.objects.get(slug=comunidade) r.community = c r.creator = user r.creation_date = now r.geometry = geo_ref r.save() # load remaining comunities as tags for tag in tags: r.tags.add(tag) # save Need elif imp == 'N': #fix comunidade tags = [] if ',' in comunidade: split = [c.strip() for c in comunidade.split(',')] comunidade, tags = split[0], split[1:] n = Need() n.title = name n.description = desc if comunidade: c = Community.objects.get(slug=comunidade) n.community = c n.creator = user n.creation_date = now n.geometry = geo_ref # n.target_audiences.add(1) n.save() # load remaining comunities as tags for tag in tags: n.tags.add(tag) # save organization elif imp == 'O': if name: o = Organization() o.name = name o.description = desc o.creation_date = now o.creator = user o.save() for comm in [c.strip() for c in comunidade.split(',')]: c = Community.objects.get(slug=comm) o.community.add(c) b = OrganizationBranch() b.name = name + ' - sede' b.geometry = geo_ref b.organization = o b.creator = user b.creation_date = now b.save()