def post(self, request): if 'alias' not in request.POST or 'target' not in request.POST: return HttpResponseBadRequest() alias = request.POST.get('alias') target = request.POST.get('target') title = request.POST.get('title') try: target = int(target) except ValueError: hash_id = Session.parse_hash_from_link(target) target = Session.id_from_hash(hash_id)[0] session = get_object_or_404(Session, id=target) if title: session.title = title session.save() SessionAlias.objects.create(alias=alias, session=session, user=request.user, title=session.title) return HttpResponse(status=201)
def post(self, request): if 'alias' not in request.POST or 'target' not in request.POST: return HttpResponseBadRequest() alias = request.POST.get('alias') target = request.POST.get('target') title = request.POST.get('title') try: target = int(target) except ValueError: hash_id = Session.parse_hash_from_link(target) target = Session.id_from_hash(hash_id)[0] session = get_object_or_404(Session, id=target) if title: session.title = title session.save() SessionAlias.objects.create(alias=alias, session=session, user=request.user, title=session.title) return HttpResponse(status=201)
def get_redirect_url(self, term=None): suggestions = UrlMediatorSuggestionService().pick_the_best_matched_for( term) if suggestions: session_query = Builder( FilterTags(FromSuggestions(suggestions=suggestions))).build() session = Session(query=session_query) session.save() return session.get_absolute_url() return reverse('homepage')
def get_redirect_url(self, term=None): suggestions = UrlMediatorSuggestionService().pick_the_best_matched_for(term) if suggestions: session_query = Builder( FilterTags( FromSuggestions(suggestions=suggestions) )).build() session = Session(query=session_query) session.save() return session.get_absolute_url() return reverse('homepage')
def get_redirect_url(self, crid=None, category_slug=None, cat_hash=None): allegation = get_object_or_404(Allegation, crid=crid) cat_id = MobileUrlHashUtil().decode(cat_hash) category = get_object_or_404(AllegationCategory, pk=cat_id) session_query = Builder( FilterTags( AllegationCrid(crids=[allegation.crid]), AllegationType(categories=[(category.id, category.category)]))).build() session = Session(query=session_query) session.save() return session.get_absolute_url()
def get_redirect_url(self, crid=None, category_slug=None, cat_hash=None): allegation = get_object_or_404(Allegation, crid=crid) cat_id = MobileUrlHashUtil().decode(cat_hash) category = get_object_or_404(AllegationCategory, pk=cat_id) session_query = Builder( FilterTags( AllegationCrid(crids=[allegation.crid]), AllegationType(categories=[(category.id, category.category)]) ) ).build() session = Session(query=session_query) session.save() return session.get_absolute_url()
def get(self, request): hash_id = request.GET.get('hash_id', '') if hash_id: ints = Session.id_from_hash(hash_id) if ints: session_id = ints[0] session = get_object_or_404(Session, pk=session_id) owned_sessions = request.session.get('owned_sessions', []) if session_id in owned_sessions: return HttpResponse(JSONSerializer().serialize( self.prepare_return_data(False, session))) else: new_session = session.clone() self.update_owned_session(request, new_session) return HttpResponse(JSONSerializer().serialize( self.prepare_return_data(False, new_session))) else: return HttpResponse(JSONSerializer().serialize( {'data': { 'msg': "Hash not found" }}), status=404) else: session = self.create_new_session(request) return HttpResponse(JSONSerializer().serialize( self.prepare_return_data(True, session)))
def get_search_results(self, request, queryset, search_term): queryset, use_distinct = super(ShareSessionAdmin, self).get_search_results(request, queryset, search_term) id = Session.id_from_hash(search_term) if id: queryset = Session.objects.filter(id=id[0]) return queryset, use_distinct
def get_site_title(self, hash_id): try: session_id = Session.id_from_hash(hash_id)[0] session = Session.objects.filter(pk=session_id)[0] return session.title except IndexError: default_site_title = Setting.objects.first().default_site_title return default_site_title
def get_search_results(self, request, queryset, search_term): queryset, use_distinct = super(ShareSessionAdmin, self).get_search_results( request, queryset, search_term) id = Session.id_from_hash(search_term) if id: queryset = Session.objects.filter(id=id[0]) return queryset, use_distinct
def get_queryset(self): queryset = super(AdminSessionsAliasViewSet, self).get_queryset() query = self.request.GET.get('q', '') session_id = Session.id_from_hash(query) if session_id: return queryset.filter(session=session_id[0]) if query: queryset = queryset.filter(session__title__icontains=query.lower()) return queryset
def save(self, user, commit=True): instance = super(SessionAliasForm, self).save(commit=False) session_hash = self.cleaned_data['target'].split("/")[4] session_id = Session.id_from_hash(session_hash)[0] instance.session_id = session_id instance.user = user if commit: instance.save() self.save_m2m() return instance
def get_queryset(self): queryset = super(AdminSessionsView, self).get_queryset() query = self.request.GET.get('q', '') session_id = Session.id_from_hash(query) if session_id: return queryset.filter(id=session_id[0]) if query: queryset = queryset.filter(title__icontains=query.lower()) return queryset.order_by('-created_at')
def get_queryset(self): queryset = super(AdminSessionsAliasViewSet, self).get_queryset() query = self.request.GET.get('q', '') session_id = Session.id_from_hash(query) if session_id: return queryset.filter(session=session_id[0]) if query: queryset = queryset.filter(session__title__icontains=query.lower()) return queryset
def save(self, user, commit=True): instance = super(SessionAliasForm, self).save(commit=False) session_hash = self.cleaned_data['target'].split("/")[4] session_id = Session.id_from_hash(session_hash)[0] instance.session_id = session_id instance.user = user if commit: instance.save() self.save_m2m() return instance
def test_change_filter_clear_active_officers(self): officer_allegation = OfficerAllegationFactory() self.visit_home() self.click_first_officer() self.click_active_tab('Categories') self.until(lambda: self.link(officer_allegation.cat.category).click()) self.until_ajax_complete() session_hash = self.browser.current_url.split("/")[4] session = Session.objects.get(id=Session.id_from_hash(session_hash)[0]) session.query['active_officers'].should.be.empty
def get_queryset(self): queryset = super(AdminSessionsView, self).get_queryset() query = self.request.GET.get('q', '') session_id = Session.id_from_hash(query) if session_id: return queryset.filter(id=session_id[0]) if query: queryset = queryset.filter(title__icontains=query.lower()) return queryset.order_by('-created_at')
def post(self, request): """Clone an existing session, marking the new session as "shared".""" try: session = get_object_or_404(Session, pk=Session.id_from_hash( request.POST['hash_id'])[0]) except (KeyError, IndexError): return self.error_response('Bad parameters.') new_session = session.clone() new_session.shared = True new_session.save() return HttpResponse(JSONSerializer().serialize( self.prepare_return_data(True, new_session)))
def test_init_in_area(self): lng, lat = self.area.polygon.centroid self.visit(reverse('init'), { 'lat': lat, 'lng': lng, }) self.response.status_code.should.equal(302) hash_id = self.response['location'].split('/')[-1] session_id = Session.id_from_hash(hash_id)[0] session = Session.objects.get(id=session_id) session.query['filters']['Area'][0]['value']\ .should.equal(self.area.id)
def create_new_session(self, request): session = Session() session.ip = get_client_ip(request) session.user_agent = request.user_agent session.save() request.session['current_session'] = session.hash_id self.update_owned_session(request, session) return session
def get_context_data(self, hash_id=None, **kwargs): context = super(SunburstView, self).get_context_data(**kwargs) session = get_object_or_404( Session, pk=Session.id_from_hash(hash_id)[0]) query_str = build_query_string_from_session(session) query_dict = QueryDict(query_str) query_set = OfficerAllegationQueryBuilder().build(query_dict) officer_allegations = OfficerAllegation.objects.filter(query_set) context['sunburst_data'] = { 'sunburst': SunburstSerializer(officer_allegations).data } context['sunburst_arc'] = session.sunburst_arc return context
def get_redirect_url(self, *args, **kwargs): hash_id = kwargs.get('hash_id', '') try: session_id = Session.id_from_hash(hash_id)[0] session = Session.objects.get(id=session_id) self.filters = session.query.get('filters', {}) except (IndexError, Session.DoesNotExist): self.filters = {} redirect_urls = DesktopToMobileRedirectorService(DEFAULT_REDIRECTORS).perform(self.filters) if len(redirect_urls) == 1: return redirect_urls[0] return DEFAULT_REDIRECT_URL
def test_home_page_generate_new_share_when_access_a_share(self): session = SessionFactory() session_count = Session.objects.all().count() title_slug = slugify(session.title) response = self.visit("/data/%s/%s" % (session.hash_id, title_slug)) Session.objects.all().count().should.equal(session_count + 1) # 1 new session created location = self.browser.current_url new_hash_id = location.split("/")[-2] new_session_id = Session.id_from_hash(new_hash_id)[0] new_session = Session.objects.get(pk=new_session_id) dict(self.compact(new_session.query)).should.equal(self.compact(dict(session.query))) response = self.client.get(location) response.status_code.should.equal(200) Session.objects.all().count().should.equal(session_count + 1) # 0 new session created
def get_redirect_url(self, *args, **kwargs): hash_id = kwargs.get('hash_id', '') try: session_id = Session.id_from_hash(hash_id)[0] session = Session.objects.get(id=session_id) self.filters = session.query.get('filters', {}) except (IndexError, Session.DoesNotExist): self.filters = {} redirect_urls = DesktopToMobileRedirectorService( DEFAULT_REDIRECTORS).perform(self.filters) if len(redirect_urls) == 1: return redirect_urls[0] return DEFAULT_REDIRECT_URL
def test_home_page_generate_new_share_when_access_a_share(self): session = SessionFactory() session_count = Session.objects.all().count() title_slug = slugify(session.title) response = self.visit("/data/%s/%s" % (session.hash_id, title_slug)) Session.objects.all().count().should.equal(session_count + 1) # 1 new session created location = self.browser.current_url new_hash_id = location.split("/")[-2] new_session_id = Session.id_from_hash(new_hash_id)[0] new_session = Session.objects.get(pk=new_session_id) dict(self.compact(new_session.query)).should.equal( self.compact(dict(session.query))) response = self.client.get(location) response.status_code.should.equal(200) Session.objects.all().count().should.equal(session_count + 1) # 0 new session created
def test_share_bar_facebook_share(self): title = 'Donald Duck' self.visit_home() self.find('.share-button button').click() self.until_ajax_complete() self.fill_in('.site-title-input', title) shared_hash_id = re.findall( r'data/([^/]+)', self.find('.share-bar-content-wrapper input').get_attribute('value'))[0] self.find('.share-bar-facebook-link').click() self.until_ajax_complete() with switch_to_popup(self.browser): ('https://www.facebook.com' in self.browser.current_url).should.be.true self.find('.share-button button').click() session_id = Session.id_from_hash(shared_hash_id)[0] session = Session.objects.get(id=session_id) session.title.should.be.equal(title)
def put(self, request): data = json.loads(request.body.decode("utf-8")) owned_sessions = request.session.get('owned_sessions', []) try: session_id = Session.id_from_hash(data['hash'])[0] except IndexError: return self.error_response('Hash not found') session = Session.objects.filter(pk=session_id).first() if not session: return self.error_response('Session is not found') if not session.shared and session_id not in owned_sessions: return self.error_response('Hash is not owned') session = self.update_session_data(session, data) return HttpResponse(JSONSerializer().serialize( self.prepare_return_data(False, session)), content_type='application/json')
def test_share_bar_facebook_share(self): title = 'Donald Duck' self.visit_home() self.find('.share-button button').click() self.until_ajax_complete() self.fill_in('.site-title-input', title) shared_hash_id = re.findall( r'data/([^/]+)', self.find('.share-bar-content-wrapper input').get_attribute( 'value'))[0] self.find('.share-bar-facebook-link').click() self.until_ajax_complete() with switch_to_popup(self.browser): ('https://www.facebook.com' in self.browser.current_url).should.be.true self.find('.share-button button').click() session_id = Session.id_from_hash(shared_hash_id)[0] session = Session.objects.get(id=session_id) session.title.should.be.equal(title)
def get_session_from_data(self, data): session_id = Session.id_from_hash(data['hash'])[0] return Session.objects.get(id=session_id)
def __init__(self, *args, **kwargs): super(RequestEmailForm, self).__init__(*args, **kwargs) if self.data.get('session'): session_id = Session.id_from_hash(self.data.get('session'))[0] self.instance.session = Session.objects.get(pk=session_id)
def get_object(self): if 'pk' in self.kwargs: self.kwargs['pk'] = Session.id_from_hash(self.kwargs['pk'])[0] return super(SessionViewSet, self).get_object()