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(self, request): officer_id = request.GET.get('pk') officer = Officer.objects.get(id=officer_id) officer_allegations = OfficerAllegation.objects.filter(officer=officer) has_map = officer_allegations.exclude(allegation__point__isnull=True).count() > \ settings.MAP_POINT_THRESHOLD related_query = OfficerAllegation.objects.filter( allegation__pk__in=officer_allegations.values_list( 'allegation__pk', flat=True)) related_query = related_query.exclude(officer_id=officer_id).exclude( officer_id__isnull=True) related_officers_ordered = related_query.values('officer')\ .annotate(total=Count('allegation__pk')).order_by('-total') related_witness = PoliceWitness.objects.filter( allegation__pk__in=officer_allegations.values_list( 'allegation__pk', flat=True)) related_witness = related_witness.exclude( officer_id=officer_id).exclude(officer_id__isnull=True) related_witness_ordered = related_witness.values('officer')\ .annotate(total=Count('crid')).order_by('-total') related_officers = [] for related_officer in related_officers_ordered: related_officers.append({ 'num_allegations': related_officer['total'], 'witness': False, 'officer': Officer.objects.get(pk=related_officer['officer']) }) for witness in related_witness_ordered: related_officers.append({ 'num_allegations': witness['total'], 'witness': True, 'officer': Officer.objects.get(pk=witness['officer']) }) related_officers = sorted(related_officers, key=lambda n: n['num_allegations'], reverse=True) return HttpResponse(JSONSerializer().serialize({ 'officer': OfficerDetailSerializer(officer, context={ 'request': request }).data, 'officer_allegations': officer_allegations, 'relatedOfficers': related_officers, 'has_map': has_map }))
def __init__(self, form=None, content='', *args, **kwargs): if form: content = {'errors': form.errors} content = JSONSerializer().serialize(content) super(HttpResponseBadRequest, self).__init__(content, content_type="application/json", *args, **kwargs)
def __init__(self, data=None, safe=True, **kwargs): data = data or {} if safe and not isinstance(data, dict): raise TypeError( 'In order to allow non-dict objects to be serialized set the safe parameter to False' ) kwargs.setdefault('content_type', 'application/json') data = JSONSerializer().serialize(data) super(JsonResponse, self).__init__(content=data, **kwargs)
def get(self, request): officer_allegations = self.get_officer_allegations() officer_allegations = officer_allegations.order_by( '-allegation__incident_date', '-start_date', 'allegation__crid') officer_allegations = officer_allegations.select_related('cat') start, end = self.get_fetch_range(request, officer_allegations) content = JSONSerializer().serialize({ 'officer_allegations': self.serialize_officer_allegations( officer_allegations[start:end]), 'analytics': OutcomeAnalytics.get_analytics(officer_allegations) }) return HttpResponse(content, content_type='application/json')
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 get(self, request): officer_id = request.GET.get('officer') officer = get_object_or_404(Officer, pk=officer_id) stories = officer.story_set.order_by('-custom_order', '-created_date') content = { 'stories': stories, 'success': True, } content = JSONSerializer().serialize(content) return HttpResponse(content, content_type='application/json')
def get(self, request): try: allegation = Allegation.objects.get(crid=request.GET['crid']) except (KeyError, Allegation.DoesNotExist): return HttpResponse('{}', content_type="application/json") related_officers = Officer.objects.filter( officerallegation__allegation__pk=allegation.pk) witnesses = PoliceWitness.objects.filter(allegation__pk=allegation.pk) ret = {'police_witness': []} for witness in witnesses: witness.officer_name = "%s %s" % ( witness.officer.officer_first, witness.officer.officer_last) officers = [] for officer in related_officers: allegation_pks = OfficerAllegation.objects\ .filter(officer=officer)\ .values_list('allegation__pk', flat=True) total_witnesses = PoliceWitness.objects.filter( officer__pk=witness.officer_id, allegation_id=allegation_pks) related_officer_allegations = OfficerAllegation.objects\ .filter(allegation__pk__in=allegation_pks)\ .filter( Q(officer__pk=witness.officer_id) | Q(allegation__pk__in=total_witnesses .values_list('allegation__pk', flat=True), officer__pk=officer.pk)) num_complaints = related_officer_allegations.count() no_action_taken_count = related_officer_allegations.filter( Q(final_outcome__in=NO_DISCIPLINE_CODES) | Q(final_outcome__isnull=True)).count() officers.append({ 'num_complaints': num_complaints, 'no_action_taken': no_action_taken_count, 'officer': officer, }) ret['police_witness'].append({ 'witness': witness, 'witness_officer': witness.officer, 'officers': officers, }) content = JSONSerializer().serialize(ret) return HttpResponse(content, content_type="application/json")
def get(self, request): officer_allegations = self.get_officer_allegations() officers = Officer.objects.filter(officerallegation__in=officer_allegations)\ .annotate(filtered_allegations_count=Count('officerallegation__allegation__pk'))\ .order_by('-allegations_count') overview = [] for r in OFFICER_COMPLAINT_COUNT_RANGE: range_officers = officers.filter(allegations_count__gte=r[0]) if r[1]: range_officers = range_officers.filter( allegations_count__lt=r[1]) overview.append(range_officers.count()) content = JSONSerializer().serialize({ 'officers': officers, 'overview': overview, }) return HttpResponse(content, content_type="application/json")
def get(self, request): officer_id = request.GET.get('officer') officer = Officer.objects.get(pk=officer_id) officer_allegations = OfficerAllegation.objects.filter(officer=officer) allegations_date = officer_allegations\ .values_list('allegation__incident_date_only', 'start_date')\ .order_by('allegation__incident_date') items = [] for date in allegations_date: if not date[0] and date[1]: items.append(date[1]) elif date[0]: items.append(date[0]) items = [officer.appt_date] + sorted(items) result = JSONSerializer().serialize({ 'items': items, }) return HttpResponse(result, content_type='application/json')
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 get(self, request): officer_allegations = self.get_officer_allegations( ignore_filters=['cat', 'cat__category']) count_query = officer_allegations.values_list('cat')\ .annotate(dcount=Count('id')) count_by_category = dict(count_query) discipline_allegations = officer_allegations\ .exclude(final_outcome__in=NO_DISCIPLINE_CODES) discipline_allegations = discipline_allegations\ .exclude(final_outcome__isnull=True) discipline_count_query = discipline_allegations\ .values_list('cat').annotate(dcount=Count('id')) discipline_count_by_category = dict(discipline_count_query) categories = AllegationCategory.objects.all().order_by('category') summary = [] summary_map_by_name = {} for category in categories: if category.category in summary_map_by_name: summary_value = summary_map_by_name[category.category] else: summary_value = summary_map_by_name[category.category] = { 'name': category.category, 'total': 0, 'count': 0, 'subcategories': [], 'id': len(summary) + 1, } summary.append(summary_value) count = count_by_category.get(category.id, 0) if not count: continue summary_value['total'] += count summary_value['count'] += discipline_count_by_category.get( category.id, 0) summary_value['subcategories'].append({ 'name': category.allegation_name, 'cat_id': category.cat_id, 'id': category.id, 'count': count, 'id': category.id }) summary = sorted(summary, key=lambda x: -x['total']) summary = [x for x in summary if x['total']] for summary_row in summary: summary_row['subcategories'] = sorted(summary_row['subcategories'], key=lambda x: -x['count']) if summary and summary[0]['total']: maximum = summary[0]['total'] else: maximum = 1 for value in summary: value['percentToMax'] = value['total'] * 100.0 / maximum content = JSONSerializer().serialize({ 'summary': summary }) return HttpResponse(content, content_type="application/json")
def error_response(self, error_message): return HttpResponse(JSONSerializer().serialize( {'data': { 'msg': error_message }}), status=400)