def remove_duplicate_quests(self): skip = 0 while True: query = 'MATCH (official:PublicOfficial) ' \ 'RETURN DISTINCT official ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for official in [PublicOfficial.inflate(row[0]) for row in res]: query = 'MATCH (official:PublicOfficial {object_uuid:"%s"})-' \ '[:IS_HOLDING]->(a:Quest) ' \ 'OPTIONAL MATCH (a)-[r]-() ' \ 'DELETE a, r' % official.object_uuid db.cypher_query(query) quest = Quest(about=official.bio, youtube=official.youtube, twitter=official.twitter, website=official.website, first_name=official.first_name, last_name=official.last_name, owner_username=official.object_uuid, title="%s %s" % (official.first_name, official.last_name), profile_pic="%s/representative_images/225x275/" "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN, official.bioguideid)).save() official.quest.connect(quest) cache.set(self.cache_key, True)
def setUp(self): self.email = "*****@*****.**" create_user_util_test(self.email) self.pleb = Pleb.nodes.get(email=self.email) self.user = User.objects.get(email=self.email) self.pleb.save() self.president = PublicOfficial(title='President', bioguideid=str(uuid1())).save() self.senator = PublicOfficial(state="MI", district=0, bioguideid=str(uuid1())).save() self.house_rep = PublicOfficial(state="MI", district=11, bioguideid=str(uuid1())).save() self.address = Address(street="3295 Rio Vista St", city="Commerce Township", state="MI", postal_code="48382", country="US", congressional_district="11").save() self.pleb.address.connect(self.address)
def test_rep_already_exists(self): try: official = PublicOfficial.nodes.get(bioguideid="MIL000290") except (PublicOfficial.DoesNotExist, DoesNotExist): official = PublicOfficial(bioguideid="MIL000290", first_name="Kathy", last_name="Crawford", state_district="38", state_chamber="lower", state="mi").save() res = create_and_attach_state_level_reps.apply_async( kwargs={'rep_data': self.json_data}) while not res.ready(): time.sleep(1) self.assertTrue(res.result) camp = official.get_quest() self.assertTrue(camp in official.quest) official.delete() camp.delete()
def president(self, request, username=None): president = cache.get("%s_president" % username) if president is None: query = 'MATCH (p:Pleb {username:"******"})-[:HAS_PRESIDENT]->' \ '(o:PublicOfficial) RETURN o' % username res, _ = db.cypher_query(query) try: president = PublicOfficial.inflate(res[0][0]) cache.set("%s_president" % username, president, timeout=1800) except IndexError: return Response({}, status=status.HTTP_200_OK) return Response(PublicOfficialSerializer(president).data, status=status.HTTP_200_OK)
def senators(self, request, username=None): # TODO this may be able to be refined to [:REPRESENTED_BY]->(s:Senator) senators = cache.get("%s_senators" % username) if senators is None: query = "MATCH (a:Pleb {username: '******'})-[:HAS_SENATOR]->" \ "(s:PublicOfficial) RETURN s" % username res, col = db.cypher_query(query) [row[0].pull() for row in res] senators = [PublicOfficial.inflate(row[0]) for row in res] cache.set("%s_senators" % username, senators, timeout=1800) if len(senators) == 0: return Response([], status=status.HTTP_200_OK) return Response(PublicOfficialSerializer(senators, many=True).data, status=status.HTTP_200_OK)
def is_authorized_as(self): from sb_public_official.neo_models import PublicOfficial official = cache.get("%s_official" % self.username) if official is None: query = 'MATCH (p:Pleb {username: "******"})-[r:IS_AUTHORIZED_AS]->' \ '(o:PublicOfficial) WHERE r.active=true RETURN o' \ % self.username res, _ = db.cypher_query(query) try: official = PublicOfficial.inflate(res[0][0]) cache.set("%s_official" % self.username, official) except IndexError: official = None return official
def house_representative(self, request, username=None): # TODO this may be able to be refined to # [:REPRESENTED_BY]->(s:HouseRepresentative) house_rep = cache.get("%s_house_representative" % username) if house_rep is None: query = "MATCH (a:Pleb {username: '******'})-" \ "[:HAS_HOUSE_REPRESENTATIVE]->" \ "(s:PublicOfficial) RETURN s" % username res, col = db.cypher_query(query) try: house_rep = PublicOfficial.inflate(res[0][0]) cache.set("%s_house_representative" % username, house_rep, timeout=1800) except IndexError: return Response({}, status=status.HTTP_200_OK) return Response(PublicOfficialSerializer(house_rep).data, status=status.HTTP_200_OK)
def setUp(self): query = 'MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r' db.cypher_query(query) self.unit_under_test_name = 'address' self.email = "*****@*****.**" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) PublicOfficial(bioguideid=str(uuid1()), title="President", gt_id=str(uuid1())).save() self.address = Address(street="3295 Rio Vista St", city="Commerce Township", state="MI", postal_code="48382", country="US", congressional_district="11") self.address.save() self.pleb.address.connect(self.address) self.url = "http://testserver"
def remove_duplicate_quests(self): skip = 0 while True: query = 'MATCH (official:PublicOfficial) ' \ 'RETURN DISTINCT official ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for official in [PublicOfficial.inflate(row[0]) for row in res]: new_query = 'MATCH (a:Quest {owner_username: "******"}) ' \ 'RETURN a' % official.object_uuid new_res, _ = db.cypher_query(new_query) if not new_res.one: quest = Quest( about=official.bio, youtube=official.youtube, twitter=official.twitter, website=official.website, first_name=official.first_name, last_name=official.last_name, owner_username=official.object_uuid, title="%s %s" % (official.first_name, official.last_name), profile_pic="%s/representative_images/225x275/" "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN, official.bioguideid)).save() official.quest.connect(quest) duplicate_query = 'MATCH (a:PublicOfficial ' \ '{object_uuid:"%s"})-' \ '[:IS_HOLDING]->(q:Quest) ' \ 'WHERE NOT q.owner_username="******" RETURN q' % \ (official.object_uuid, official.object_uuid) new_res, _ = db.cypher_query(duplicate_query) duplicate_quests = [ Quest.inflate(duplicate[0]) for duplicate in new_res if duplicate[0] is not None ] if duplicate_quests: skip = 0 for dup in duplicate_quests: relationship_query = 'MATCH (a:Quest ' \ '{object_uuid:"%s"})-[r]-' \ '(p:Pleb) RETURN ' \ 'p.username as username, ' \ 'type(r) as type' \ % dup.object_uuid rel_res, _ = db.cypher_query(relationship_query) if rel_res.one: for row in rel_res: try: re_query = 'MATCH (a:Quest ' \ '{owner_username:"******"}), ' \ '(p:Pleb {username:"******"}) ' \ 'CREATE UNIQUE (p)-[r:%s]->(a)' \ % (official.object_uuid, row.username, row.type) db.cypher_query(re_query) except ConstraintViolation: # handle potential constraint violations pass query = 'MATCH (a:Quest {object_uuid: "%s"}) ' \ 'OPTIONAL MATCH (a)-[r]-() ' \ 'DELETE a, r' % dup.object_uuid db.cypher_query(query) cache.set("removed_duplicate_quests", True)
def create_prepopulated_reps(self): count = 0 while True: query = 'MATCH (role:GTRole) ' \ 'RETURN role SKIP %s LIMIT 25' % count res, _ = db.cypher_query(query) if not res.one: break count += 24 for role in [GTRole.inflate(row[0]) for row in res]: if role.current: query = 'MATCH (role:GTRole {role_id: %s})' \ '-[:IS]->(person:GTPerson) ' \ 'RETURN person' % role.role_id res, _ = db.cypher_query(query) if res.one is None: continue else: person = GTPerson.inflate(res.one) try: rep = PublicOfficial.nodes.get(gt_id=person.gt_id) except (DoesNotExist, PublicOfficial.DoesNotExist): try: state = dict(US_STATES)[role.state] except KeyError: state = role.state rep = PublicOfficial(first_name=person.firstname, last_name=person.lastname, gender=person.gender, date_of_birth=person.birthday, name_mod=person.namemod, current=role.current, bio=role.description, district=role.district, state=state, title=role.title, website=role.website, start_date=role.startdate, end_date=role.enddate, full_name=person.name, twitter=person.twitterid, youtube=person.youtubeid, gt_id=person.gt_id, gov_phone=role.phone, bioguideid=person.bioguideid) rep.save() except (CypherException, IOError) as e: logger.exception(e) continue quest = rep.get_quest() if not quest: quest = Quest( about=rep.bio, youtube=rep.youtube, twitter=rep.twitter, website=rep.website, first_name=rep.first_name, last_name=rep.last_name, owner_username=rep.object_uuid, title="%s %s" % (rep.first_name, rep.last_name), profile_pic="%s/representative_images/225x275/" "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN, rep.bioguideid)).save() rep.quest.connect(quest) else: quest.profile_pic = \ "%s/representative_images/225x275/" \ "%s.jpg" % (settings.LONG_TERM_STATIC_DOMAIN, rep.bioguideid) quest.save() cache.set('%s_quest' % quest.object_uuid, quest) rep.gt_person.connect(person) rep.gt_role.connect(role) task_data = { "object_uuid": quest.object_uuid, "label": 'quest', } spawn_task(update_search_object, task_data) populate_term_data()