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 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 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 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 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
def list(self, request, *args, **kwargs): moderators = Mission.get(object_uuid=self.kwargs["object_uuid"]) if not (request.user.username in moderators.get_moderators( moderators.owner_username) and request.method == "GET"): return Response( { "status_code": status.HTTP_403_FORBIDDEN, "detail": "You are not authorized to access " "this page." }, status=status.HTTP_403_FORBIDDEN) return super(VolunteerViewSet, self).list(request, *args, **kwargs)
def mission_redirect_page(request, object_uuid=None): try: mission_obj = Mission.get(object_uuid) except (Mission.DoesNotExist, DoesNotExist): return redirect("404_Error") except (CypherException, ClientError, IOError): # pragma: no cover return redirect("500_Error") return redirect("mission", object_uuid=object_uuid, slug=slugify(mission_obj.get_mission_title()), permanent=True)
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 get_mission(self, obj): from sb_missions.neo_models import Mission from sb_missions.serializers import MissionSerializer request, expand, _, relation, _ = gather_request_data(self.context) mission = Donation.get_mission(obj.object_uuid) if mission is None: return None if expand == 'true': return MissionSerializer(Mission.get( object_uuid=mission)).data if relation == "hyperlink" and mission is not None: return reverse('mission-detail', kwargs={"object_uuid": mission}, request=request) return mission
def get(self, request, object_uuid=None, slug=None): try: mission_obj = Mission.get(object_uuid) except (Mission.DoesNotExist, DoesNotExist): return redirect("404_Error") except (CypherException, ClientError, IOError): return redirect("500_Error") mission_dict = MissionSerializer(mission_obj, context={ 'request': request }).data return render( request, self.template_name, { "mission": mission_dict, "quest": QuestSerializer(Quest.get(mission_obj.owner_username)).data })
def get_url(self, obj): # removed this currently because this view doesn't exist # TODO create view to display quest updates """ elif obj.about_type == "quest": about = Quest.get(obj.about_id) return reverse('quest_updates', kwargs={'object_uuid': about.owner_username}, request=request) """ from sb_missions.neo_models import Mission request, _, _, _, _ = gather_request_data(self.context) if obj.about_type == "mission": about = Mission.get(obj.about_id) return reverse('mission_updates', kwargs={ 'object_uuid': about.object_uuid, 'slug': slugify(about.get_mission_title()) }, request=request) else: return None
def test_get_not_cached(self): cache.clear() res = Mission.get(self.mission.object_uuid) self.assertEqual(res.object_uuid, self.mission.object_uuid)
def get(self, request, object_uuid=None, slug=None): # Do a second optional match to get the list of missions, # the first is just to make sure we're dealing with the actual # owner of the Mission. query = 'MATCH (pleb:Pleb {username: "******"})-[:IS_WAGING]->' \ '(quest:Quest) WITH quest ' \ 'OPTIONAL MATCH (quest)-[:EMBARKS_ON]->(missions:Mission) ' \ 'RETURN missions, quest ' \ 'ORDER BY missions.created DESC' % request.user.username res, _ = db.cypher_query(query) if res.one is None: return redirect("404_Error") if res.one.missions is None: return redirect("select_mission") if object_uuid is None: mission_obj = Mission.inflate(res[0].missions) return redirect('mission_settings', object_uuid=mission_obj.object_uuid, slug=slug) mission_obj = Mission.get(object_uuid) if self.template_name == "manage/epic.html" and \ not mission_obj.saved_for_later and \ not mission_obj.submitted_for_review: if mission_obj.epic is None: return redirect('must_finish_epic', object_uuid=mission_obj.object_uuid, slug=slug) return redirect("submit_mission_for_review", object_uuid=mission_obj.object_uuid, slug=slug) missions = [ MissionSerializer(Mission.inflate(row.missions)).data for row in res ] quest = Quest.inflate(res.one.quest) if mission_obj.owner_username != quest.owner_username: return redirect("404_Error") onboarding_sort, completed_count = order_tasks(object_uuid) if len(onboarding_sort) != 0: onboarding_done = int( (float(completed_count) / float(len(onboarding_sort))) * 100) else: onboarding_done = 0 return render( request, self.template_name, { "missions": missions, "mission": MissionSerializer(mission_obj, context={ "request": request }).data, "quest": QuestSerializer(quest, context={ "request": request }).data, "slug": slugify(mission_obj.get_mission_title()), "epic_template": render_to_string("manage/placeholder_epic.html"), "update_placeholder": render_to_string("updates/placeholder.html"), "onboarding_top_3": onboarding_sort[:3], "onboarding_rest": onboarding_sort[3:], "onboarding_done": onboarding_done })
def test_get_dne(self): try: Mission.get(str(uuid1())) except DoesNotExist: # this is the best way to test this, maybe? self.assertTrue(True)
def perform_create(self, serializer): volunteer = Pleb.get(self.request.user.username) mission = Mission.get(object_uuid=self.kwargs["object_uuid"]) serializer.save(mission=mission, volunteer=volunteer, owner_username=volunteer.username)
def test_get_cached(self): cache.set("%s_mission" % self.mission.object_uuid, self.mission) res = Mission.get(self.mission.object_uuid) self.assertEqual(res.object_uuid, self.mission.object_uuid)
def perform_update(self, serializer): mission_id = self.request.data['mission'] mission = Mission.get(mission_id) quest = Mission.get_quest(object_uuid=mission_id) serializer.save(mission=mission, quest=quest)
def perform_create(self, serializer): mission_id = self.request.data['mission'] mission = Mission.get(mission_id) serializer.save(mission=mission)
def get_mission(self, obj): request = self.context.get('request', None) return MissionSerializer(Mission.get(obj.mission_id), context={ 'request': request }).data