def test_unendorse_pleb(self): res = Mission.endorse(self.mission.object_uuid, self.owner.username, "profile") self.assertTrue(res) res = Mission.unendorse(self.mission.object_uuid, self.owner.username, "profile") self.assertTrue(res)
def setUp(self): query = "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r" db.cypher_query(query) cache.clear() self.unit_under_test_name = 'quest' self.email = "*****@*****.**" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.url = "http://testserver" self.quest = Quest(about='Test Bio', owner_username=self.pleb.username).save() self.quest.editors.connect(self.pleb) self.quest.moderators.connect(self.pleb) self.address = Address(street="125 Glenwood Drive", city="Walled Lake", state="Michigan", postal_code="48390", country="USA", county="Oakland", congressional_district=11, validated=True).save() self.quest.address.connect(self.address) cache.clear() self.mission = Mission(owner_username=self.pleb.username, title=str(uuid1()), focus_name="advocacy", location_name="11").save() self.quest.missions.connect(self.mission) self.giftlist = self.mission.get_giftlist() if not self.giftlist: self.giftlist = Giftlist().save() self.giftlist.mission.connect(self.mission) self.email2 = "*****@*****.**" self.pleb2 = create_user_util_test(self.email2) self.user2 = User.objects.get(email=self.email2)
def test_get_mission(self): quest = Quest(owner_username=self.pleb.username).save() quest.owner.connect(self.pleb) mission = Mission(owner_username=self.pleb.username).save() quest.missions.connect(mission) mission.associated_with.connect(self.question) res = Question.get_mission(self.question.object_uuid) self.assertEqual(res['id'], mission.object_uuid) mission.delete() quest.delete()
def setUp(self): self.email = "*****@*****.**" create_user_util_test(self.email) self.owner = Pleb.nodes.get(email=self.email) self.quest = Quest(owner_username=self.owner.username, object_uuid=str(uuid1())).save() self.quest.owner.connect(self.owner) self.mission = Mission(owner_username=self.owner.username, title=str(uuid1())).save() self.quest.missions.connect(self.mission)
def perform_create(self, serializer): donor = Pleb.get(self.request.user.username) if "/v1/missions/" in self.request.path: mission = Mission.get(object_uuid=self.kwargs[self.lookup_field]) quest = Mission.get_quest( object_uuid=self.kwargs[self.lookup_field]) else: mission = None quest = Quest.get(owner_username=self.kwargs[self.lookup_field]) serializer.save(mission=mission, donor=donor, quest=quest, owner_username=donor.username)
def perform_create(self, serializer): object_uuid = str(uuid1()) if self.request.data.get('about_type') == "mission" \ or 'mission' in self.request.path: about = Mission.get(self.kwargs[self.lookup_field]) quest = Quest.get(about.owner_username) url = reverse('mission_updates', kwargs={ 'object_uuid': self.kwargs[self.lookup_field], 'slug': slugify(about.get_mission_title()) }, request=self.request) else: # If all else fails assume this update is about the Quest itself quest = Quest.get(self.kwargs[self.lookup_field]) # TODO update quest url generation when we have an updates for # quest url = None about = quest serializer.save(quest=quest, about=about, url=url, object_uuid=object_uuid, href=reverse('update-detail', kwargs={'object_uuid': object_uuid}, request=self.request))
def setUp(self): cache.clear() self.unit_under_test_name = 'goal' self.email = "*****@*****.**" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.email2 = "*****@*****.**" self.pleb2 = create_user_util_test(self.email2) self.user2 = User.objects.get(email=self.email2) self.url = "http://testserver" self.quest = Quest( about='Test Bio', owner_username=self.pleb.username).save() self.quest.editors.connect(self.pleb) self.quest.moderators.connect(self.pleb) cache.clear() self.stripe = stripe self.stripe.api_key = settings.STRIPE_SECRET_KEY self.stripe.api_version = settings.STRIPE_API_VERSION self.mission = Mission(owner_username=self.pleb.username, title=str(uuid1()), focus_name="advocacy").save() self.volunteer = Volunteer(activities=["get_out_the_vote"], mission_id=self.mission.object_uuid, owner_username=self.pleb2.username).save() self.quest.missions.connect(self.mission) self.volunteer.mission.connect(self.mission) self.volunteer.volunteer.connect(self.pleb2)
def test_get_donors_order(self): order = Order(completed=True, owner_username=self.owner.username).save() order.mission.connect(self.mission) cache.clear() res = Mission.get_donors(self.mission.object_uuid) self.assertEqual(res[0], self.owner.username)
def create(self, request, *args, **kwargs): if self.request.data.get('about_type') == "mission": mission = Mission.get(self.kwargs[self.lookup_field]) quest = Quest.get(mission.owner_username) if quest is None: return Response( { "status_code": status.HTTP_404_NOT_FOUND, "detail": "Sorry we couldn't find the Quest you were " "attempting to create an update for." }, status=status.HTTP_404_NOT_FOUND) if quest.owner_username == request.user.username: return super(UpdateListCreate, self).create(request, *args, **kwargs) if request.user.username not in \ Quest.get_quest_helpers(self.kwargs[self.lookup_field]): return Response( { "status_code": status.HTTP_403_FORBIDDEN, "detail": "You are not authorized to access " "this page." }, status=status.HTTP_403_FORBIDDEN) return super(UpdateListCreate, self).create(request, *args, **kwargs)
def list(self, request, *args, **kwargs): """ Currently we limit this endpoint to only be accessible to owners/accountants of campaigns, this is because it displays all donations not just the completed ones. We will eventually want to add some functionality to this that will allow people who are not the owner/accountant of the campaign to view all completed donations. :param request: :param args: :param kwargs: :return: """ if "mission" in self.request.path: moderators = Mission.get( object_uuid=self.kwargs[self.lookup_field]) else: moderators = Quest.get( owner_username=self.kwargs[self.lookup_field]) if not (request.user.username in moderators.get_moderators( moderators.owner_username)): return Response( { "status_code": status.HTTP_403_FORBIDDEN, "detail": "You are not authorized to access " "this page." }, status=status.HTTP_403_FORBIDDEN) return super(DonationListCreate, self).list(request, *args, **kwargs)
def test_get_can_downvote_mission_owner(self): quest = Quest(owner_username=self.pleb.username).save() quest.owner.connect(self.pleb) mission = Mission(owner_username=self.pleb.username).save() quest.missions.connect(mission) question = Question(content='test content title', title=str(uuid1())).save() mission.associated_with.connect(question) factory = APIRequestFactory() request = factory.get('') request.user = self.user res = VotableContentSerializer( question, context={'request': request}).data self.assertTrue(res['can_downvote']['status']) mission.delete() quest.delete()
def mission_updates(request, object_uuid, slug=None): # Only need to check that at least one update exists here to mark that # updates are available for this mission. query = 'MATCH (quest:Quest)-[:EMBARKS_ON]->' \ '(mission:Mission {object_uuid: "%s"}) ' \ 'WITH quest, mission ' \ 'OPTIONAL MATCH (mission)<-[:ABOUT]-(updates:Update) ' \ 'RETURN quest, mission, ' \ 'CASE WHEN count(updates) > 0 ' \ 'THEN true ELSE false END AS update' % object_uuid res, _ = db.cypher_query(query) if res.one is None: return redirect("404_Error") # Instead of doing inflation and serialization of all the updates here # without pagination lets just indicate if we have any or not and then # hit the endpoint to gather the actual updates. quest = Quest.inflate(res.one.quest) mission_obj = Mission.inflate(res.one.mission) return render( request, 'mission/updates.html', { "updates": res.one.update, "mission": MissionSerializer(mission_obj, context={ "request": request }).data, "slug": slugify(mission_obj.get_mission_title()), "quest": QuestSerializer(quest).data })
def get_mission(self): from sb_missions.neo_models import Mission query = 'MATCH (g:Giftlist {object_uuid:"%s"})-[:LIST_FOR]->' \ '(m:Mission) RETURN m' % self.object_uuid res, _ = db.cypher_query(query) [row[0].pull() for row in res] return Mission.inflate(res.one)
def create(self, validated_data): request = self.context["request"] # Note that DRF requires us to use the source as the key here but # tags prior to serializing mission = None tags = validated_data.pop('get_tags', []) owner = Pleb.get(request.user.username) mission_id = validated_data.get('mission', '') if mission_id: mission = Mission.get(mission_id) validated_data['owner_username'] = owner.username uuid = str(uuid1()) validated_data['content'] = render_content( validated_data.get('content', "")) url = reverse('question_detail_page', kwargs={ 'question_uuid': uuid, "slug": slugify(validated_data['title']) }, request=request) href = reverse('question-detail', kwargs={'object_uuid': uuid}, request=request) soup = BeautifulSoup(validated_data['content'], "lxml").get_text() question = Question(url=url, href=href, object_uuid=uuid, summary=smart_truncate(soup), **validated_data).save() question.owned_by.connect(owner) if mission is not None: mission.associated_with.connect(question) for tag in tags: query = 'MATCH (t:Tag {name:"%s"}) WHERE NOT t:AutoTag ' \ 'RETURN t' % slugify(tag) res, _ = db.cypher_query(query) if not res.one: if (request.user.username == "devon_bleibtrey" or request.user.username == "tyler_wiersing" or owner.reputation >= 1250): tag_obj = Tag(name=slugify(tag)).save() question.tags.connect(tag_obj) else: continue else: tag_obj = Tag.inflate(res.one) question.tags.connect(tag_obj) spawn_task(task_func=update_tags, task_param={"tags": tags}) if validated_data.get('external_location_id', None) is not None: spawn_task( task_func=create_location_tree, task_param={"external_id": question.external_location_id}) spawn_task(task_func=add_auto_tags_to_question_task, task_param={"object_uuid": question.object_uuid}) spawn_task(task_func=create_question_summary_task, task_param={'object_uuid': question.object_uuid}) question.refresh() cache.set(question.object_uuid, question) return question
def mission_endorsements(request, object_uuid, slug=None): # Just check if there are any endorsements either from a Pleb or a Quest query = 'MATCH (quest:Quest)-[:EMBARKS_ON]->' \ '(mission:Mission {object_uuid:"%s"}) ' \ 'WITH quest, mission ' \ 'OPTIONAL MATCH (mission)<-[:ENDORSES]-(endorsement) ' \ 'RETURN endorsement, quest, mission' % object_uuid res, _ = db.cypher_query(query) # Instead of doing inflation and serialization of all the updates here # without pagination lets just indicate if we have any or not and then # hit the endpoint to gather the actual updates. quest = Quest.inflate(res.one.quest) mission_obj = Mission.inflate(res.one.mission) return render( request, 'mission/endorsements.html', { "quest": QuestSerializer(quest).data, "mission": MissionSerializer(mission_obj, context={ "request": request }).data, "slug": slugify(mission_obj.get_mission_title()), "endorsements": True if res.one.endorsement else False })
def repopulate_elasticsearch(self): # Profiles skip = 0 while True: query = 'MATCH (profile:Pleb) RETURN DISTINCT profile ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for profile in [Pleb.inflate(row[0]) for row in res]: update_search_object.apply_async(kwargs={ "object_uuid": profile.object_uuid, "label": "pleb" }) # Questions skip = 0 while True: query = 'MATCH (question:Question) RETURN DISTINCT question ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for question in [Question.inflate(row[0]) for row in res]: update_search_object.apply_async(kwargs={ "object_uuid": question.object_uuid, "label": "question" }) # Quests skip = 0 while True: query = 'MATCH (quest:Quest) RETURN DISTINCT quest ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for quest in [Quest.inflate(row[0]) for row in res]: update_search_object.apply_async(kwargs={ "object_uuid": quest.object_uuid, "label": "quest" }) # Missions skip = 0 while True: query = 'MATCH (mission:Mission) RETURN DISTINCT mission ' \ 'SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for mission in [Mission.inflate(row[0]) for row in res]: update_search_object.apply_async(kwargs={ "object_uuid": mission.object_uuid, "label": "mission" })
def setUp(self): db.cypher_query('MATCH (a) OPTIONAL MATCH (a)-[r]-() DELETE a, r') self.factory = APIRequestFactory() self.client = Client() self.email = "*****@*****.**" self.password = "******" create_user_util_test(self.email) self.pleb = Pleb.nodes.get(email=self.email) self.user = User.objects.get(email=self.email) self.pleb.email_verified = True self.pleb.save() cache.clear() self.mission = Mission(owner_username=self.user.username, title=str(uuid1())).save() self.quest = Quest(owner_username=self.pleb.username).save() self.quest.missions.connect(self.mission) self.quest.owner.connect(self.pleb)
def test_get_donations(self): donation = Donation(amount=100).save() mission = Mission().save() self.quest.missions.connect(mission) donation.mission.connect(mission) donation.quest.connect(self.quest) res = Quest.get_donations(self.quest.owner_username) self.assertEqual(donation.object_uuid, res[0].object_uuid)
def has_object_permission(self, request, view, obj): if request.user.username in \ Quest.get_moderators(owner_username=obj) or \ request.user.username == obj or request.user.username in \ Mission.get_moderators(owner_username=obj): return True else: return False
def endorsed(self, request, owner_username): query = 'MATCH (q:Quest {owner_username:"******"})-' \ '[:ENDORSES]->(m:Mission) RETURN m' % owner_username res, _ = db.cypher_query(query) page = self.paginate_queryset( [Mission.inflate(mission[0]) for mission in res]) serializer = self.serializer_class(page, many=True, context={'request': request}) return self.get_paginated_response(serializer.data)
def test_get_donors(self): donation = Donation(amount=500, completed=True, owner_username=self.owner.username).save() donation.owned_by.connect(self.owner) donation.mission.connect(self.mission) cache.clear() res = Mission.get_donors(self.mission.object_uuid) self.assertEqual(res[0], self.owner.username)
def test_get_total_donation_amount(self): donation1 = Donation(amount=100).save() donation2 = Donation(amount=200).save() mission = Mission().save() self.quest.missions.connect(mission) donation1.mission.connect(mission) donation2.mission.connect(mission) res = self.quest.get_total_donation_amount() self.assertEqual(res, "2.25")
def get_mission(self): from sb_missions.neo_models import Mission query = 'MATCH (o:Order {object_uuid:"%s"})-[:GIFTED_TO]->' \ '(m:Mission) RETURN m' % self.object_uuid res, _ = db.cypher_query(query) if res.one: res.one.pull() return Mission.inflate(res.one) return None
class TestMissionUpdateView(TestCase): def setUp(self): self.client = Client() self.email = "*****@*****.**" self.password = "******" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.pleb.email_verified = True self.pleb.save() self.update = Update().save() self.quest = Quest(about='Test Bio', owner_username=self.pleb.username).save() self.quest.editors.connect(self.pleb) self.quest.moderators.connect(self.pleb) cache.clear() self.mission = Mission(owner_username=self.pleb.username, title=str(uuid1()), focus_name="advocacy").save() self.quest.missions.connect(self.mission) self.update.mission.connect(self.mission) cache.set(self.pleb.username, self.pleb) def test_edit_update(self): self.client.login(username=self.user.username, password=self.password) url = reverse("mission_edit_update", kwargs={ "object_uuid": self.mission.object_uuid, "edit_id": self.update.object_uuid, "slug": slugify(self.mission.get_mission_title()) }) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) def test_edit_update_doesnt_exist(self): self.client.login(username=self.user.username, password=self.password) url = reverse("mission_edit_update", kwargs={ "object_uuid": self.mission.object_uuid, "edit_id": self.update.object_uuid, "slug": slugify(self.mission.get_mission_title()) }) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK)
def setUp(self): self.factory = APIRequestFactory() self.client = Client() self.email = "*****@*****.**" self.password = "******" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.email2 = "*****@*****.**" self.pleb2 = create_user_util_test(self.email2) self.user2 = User.objects.get(email=self.email2) self.pleb.email_verified = True self.pleb.save() cache.clear() self.mission = Mission(owner_username=self.user.username, title=str(uuid1())).save() self.quest = Quest(owner_username=self.pleb.username).save() self.quest2 = Quest(owner_username=self.pleb2.username).save() self.quest.missions.connect(self.mission) self.quest.owner.connect(self.pleb)
def get_missions(self, obj): from sb_missions.neo_models import Mission from sb_missions.serializers import MissionSerializer expand = self.context.get('expand', 'false').lower() query = 'MATCH (quest:Quest {owner_username: "******"})-[:EMBARKS_ON]->' \ '(mission:Mission) RETURN mission' % obj.owner_username res, _ = db.cypher_query(query) if res.one is None: return None if expand == 'true': return [ MissionSerializer(Mission.inflate(row[0])).data for row in res ] return [ reverse( 'mission-detail', kwargs={'object_uuid': Mission.inflate(row[0]).object_uuid}, request=self.context.get('request', None)) for row in res ]
def test_create_with_mission(self): from sb_quests.neo_models import Quest from sb_missions.neo_models import Mission self.client.force_authenticate(user=self.user) url = reverse("question-solutions", kwargs={'object_uuid': self.question.object_uuid}) quest = Quest(owner_username=self.pleb.username).save() quest.owner.connect(self.pleb) mission = Mission(owner_username=self.pleb.username).save() quest.missions.connect(mission) mission.associated_with.connect(self.question) data = { "question": self.question.object_uuid, "content": self.solution.content } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) mission.delete() quest.delete()
def migrate_to_new_editor(self): skip = 0 while True: query = 'MATCH (m:Mission) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for mission in [Mission.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(mission.epic.replace( '>', '>')).replace('<a', '<a target="_blank"')) mission.epic = rendered mission.temp_epic = rendered mission.save() skip = 0 while True: query = 'MATCH (m:Question) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for question in [Question.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(question.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) question.content = rendered question.save() skip = 0 while True: query = 'MATCH (m:Solution) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for solution in [Solution.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(solution.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) solution.content = rendered solution.save() skip = 0 while True: query = 'MATCH (m:Update) RETURN m SKIP %s LIMIT 25' % skip skip += 24 res, _ = db.cypher_query(query) if not res.one: break for update in [Update.inflate(row[0]) for row in res]: rendered = render_content( markdown.markdown(update.content.replace( '>', '>')).replace('<a', '<a target="_blank"')) update.content = rendered update.save() cache.set("migrated_to_new_editor", True)
def setUp(self): self.client = Client() self.email = "*****@*****.**" self.password = "******" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.pleb.email_verified = True self.pleb.save() self.update = Update().save() self.quest = Quest(about='Test Bio', owner_username=self.pleb.username).save() self.quest.editors.connect(self.pleb) self.quest.moderators.connect(self.pleb) cache.clear() self.mission = Mission(owner_username=self.pleb.username, title=str(uuid1()), focus_name="advocacy").save() self.quest.missions.connect(self.mission) self.update.mission.connect(self.mission) cache.set(self.pleb.username, self.pleb)
def get_about(self, obj): from sb_missions.neo_models import Mission from sb_missions.serializers import MissionSerializer from sb_quests.neo_models import Quest from sb_quests.serializers import QuestSerializer if obj.about_type == "mission": return MissionSerializer(Mission.get(obj.about_id)).data elif obj.about_type == "quest": return QuestSerializer(Quest.get(obj.about_id)).data else: return None