def alerts(request): select_poll = SelectPoll() poll_form = NewPollForm() range_form = rangeForm() poll_form.updateTypes() assign_polls=Poll.objects.exclude(start_date=None).order_by('-pk')[0:5] district_form=DistrictForm(request.POST or None) if request.GET.get('reset_districts',None): request.session['districts']=None if district_form.is_valid(): request.session['districts']=[c.pk for c in district_form.cleaned_data['districts']] template = 'ureport/polls/alerts.html' if request.session.get('districts'): message_list = \ Message.objects.filter(details__attribute__name='alert' ).filter(connection__contact__reporting_location__in=request.session.get('districts')).order_by('-date') else: message_list =Message.objects.filter(details__attribute__name='alert').order_by('-date') (capture_status, _) = \ Settings.objects.get_or_create(attribute='alerts') (rate, _) = MessageAttribute.objects.get_or_create(name='rating') # message_list=[Message.objects.latest('date')] # use more efficient count if request.GET.get('download', None): #import pdb;pdb.set_trace() range_form = rangeForm(request.POST) if range_form.is_valid(): start = range_form.cleaned_data['startdate'] end = range_form.cleaned_data['enddate'] data = list(AlertsExport.objects.filter(date__range=(start, end)).values()) #save some memory from django import db db.reset_queries() res=ExcelResponse(data=data) res['Cache-Control'] = 'no-cache' return res if request.GET.get('search',None): search=request.GET.get('search') if search[0] == '"' and search[-1] == '"': search = search[1:-1] message_list = message_list.filter(Q(text__iregex=".*\m(%s)\y.*" % search) | Q(connection__contact__reporting_location__name__iregex=".*\m(%s)\y.*" % search) | Q(connection__identity__iregex=".*\m(%s)\y.*" % search)) elif search[0] == "'" and search[-1] == "'": search = search[1:-1] message_list = message_list.filter(Q(text__iexact=search) | Q(connection__contact__reporting_location__name__iexact=search) | Q(connection__identity__iexact=search)) elif search == "=numerical value()": message_list = message_list.filter(text__iregex="(-?\d+(\.\d+)?)") else: message_list = message_list.filter(Q(text__icontains=search) | Q(connection__contact__reporting_location__name__icontains=search) | Q(connection__identity__icontains=search)) if request.GET.get('capture', None): (s, _) = Settings.objects.get_or_create(attribute='alerts') if s.value == 'true': s.value = 'false' s.save() reply = 'Start Capture' else: s.value = 'true' s.save() reply = 'Stop Capture' return HttpResponse(reply) if request.GET.get('ajax', None): date = datetime.datetime.now() - datetime.timedelta(seconds=30) prev = request.session.get('prev', []) msgs = Message.objects.filter(details__attribute__name='alert', direction='I' ).filter(date__gte=date).exclude(pk__in=prev) request.session['prev'] = list(msgs.values_list('pk', flat=True)) msgs_list = [] if msgs: for msg in msgs: from django.template.loader import render_to_string can_view_number=request.user.has_perm('view_numbers') can_foward=request.user.has_perm('forward') row_rendered = \ render_to_string('ureport/partials/row.html', {'msg': msg,'can_foward':can_foward,'can_view_number':can_view_number,'assign_polls':assign_polls}) m = {} m['text'] = msg.text m['date'] = str(msg.date.date()) if msg.connection.contact: m['name'] = msg.connection.contact.name else: m['name'] = 'Anonymous User' m['number'] = msg.connection.identity if msg.connection.contact \ and msg.connection.contact.reporting_location: m['district'] = \ msg.connection.contact.reporting_location.name else: m['district'] = 'N/A' rating = msg.details.filter(attribute__name='alerts') if rating: r = rating[0].value else: r = 0 m['row'] = row_rendered m['connection'] = msg.connection.pk m['pk'] = msg.pk msgs_list.append(m) return HttpResponse(mark_safe(simplejson.dumps(msgs_list))) else: return HttpResponse('success') if request.GET.get('rating', None): rating = request.GET.get('rating') descs = { '1': 'Requires Attention', '2': 'Moderate', '3': 'Important', '4': 'Urgent', '5': 'Very Urgent', } msg = Message.objects.get(pk=int(request.GET.get('msg'))) (rate, _) = MessageAttribute.objects.get_or_create(name='rating' ) det = MessageDetail.objects.create(message=msg, attribute=rate, value=rating, description=descs.get(rating, '')) response = \ """<li><a href='javascript:void(0)' class="rate%s" title="%s">%s</a></li>""" \ % (rating, descs.get(rating, ''), descs.get(rating, '')) return HttpResponse(mark_safe(response)) paginator = UreportPaginator(message_list, 10, body=12, padding=2) page = request.GET.get('page', 1) try: messages = paginator.page(page) except (PageNotAnInteger, EmptyPage): # If page is not an integer, deliver first page. messages = paginator.page(1) return render_to_response(template, { 'messages': messages, 'assign_polls':assign_polls, 'paginator': paginator, 'capture_status': capture_status, 'rate': rate, 'district_form':district_form, 'range_form': range_form, }, context_instance=RequestContext(request))
def alerts(request, pk): access = get_access(request) poll_form = NewPollForm() range_form = rangeForm() poll_form.updateTypes() assign_polls = Poll.objects.exclude(start_date=None).order_by('-pk')[0:5] district_form = DistrictForm(request.POST or None) if request.GET.get('reset_districts', None): request.session['districts'] = None request.session['groups'] = None if district_form.is_valid(): request.session['districts'] = [c.pk for c in district_form.cleaned_data['districts']] groupform = AssignResponseGroupForm(request=request, access=access) if request.method == 'POST' and request.POST.get('groups', None): g_form = AssignResponseGroupForm(request.POST, request=request) if g_form.is_valid(): request.session['groups'] = g_form.cleaned_data['groups'] template = 'ureport/polls/alerts.html' if request.session.get('districts'): message_list = \ Message.objects.filter(details__attribute__name='alert', direction='I', ).filter( connection__contact__reporting_location__in=request.session.get('districts')) else: message_list = Message.objects.filter(details__attribute__name='alert', direction='I') if request.session.get('groups', None): message_list = message_list.filter(connection__contact__groups__in=request.session.get('groups' )) if access: if access.assigned_messages.exists(): message_list = access.assigned_messages.all() else: message_list = message_list.filter(connection__contact__groups__in=access.groups.all()) (capture_status, _) = \ Settings.objects.get_or_create(attribute='alerts') (rate, _) = MessageAttribute.objects.get_or_create(name='rating') # message_list=[Message.objects.latest('date')] # use more efficient count if request.GET.get('download', None): range_form = rangeForm(request.POST) if range_form.is_valid(): tasks.export_alerts_task.delay(range_form, access, request.get_host(), request.user.username) return HttpResponseRedirect(reverse('alerts', args=[request.user.pk])) if request.GET.get('search', None): search = request.GET.get('search') if search[0] == '"' and search[-1] == '"': search = search[1:-1] message_list = message_list.filter(Q(text__iregex=".*\m(%s)\y.*" % search) | Q(connection__contact__reporting_location__name__iregex=".*\m(%s)\y.*" % search) | Q(connection__pk__iregex=".*\m(%s)\y.*" % search)) elif search[0] == "'" and search[-1] == "'": search = search[1:-1] message_list = message_list.filter(Q(text__iexact=search) | Q(connection__contact__reporting_location__name__iexact=search) | Q(connection__pk__iexact=search)) elif search == "=numerical value()": message_list = message_list.filter(text__iregex="(-?\d+(\.\d+)?)") else: message_list = message_list.filter(Q(text__icontains=search) | Q(connection__contact__reporting_location__name__icontains=search) | Q(connection__pk__icontains=search)) if request.GET.get('capture', None): (s, _) = Settings.objects.get_or_create(attribute='alerts') if s.value == 'true': s.value = 'false' s.save() reply = gettext('Start Capture') else: s.value = 'true' s.save() reply = gettext('Stop Capture') return HttpResponse(reply) if request.GET.get('ajax', None): if request.GET.get('ajax') == 'get_replied': date = datetime.datetime.now() - datetime.timedelta(seconds=4 * 60 * 60 * 30) msgs = Message.objects.filter(details__attribute__name='replied', direction='I', date__gte=date).values( 'pk') return HttpResponse(simplejson.dumps(list(msgs)), content_type="application/json") date = datetime.datetime.now() - datetime.timedelta(seconds=30) prev = request.session.get('prev', []) msgs = Message.objects.filter(details__attribute__name='alert', direction='I' ).filter(date__gte=date).exclude(pk__in=prev) if access: msgs = msgs.filter(connection__contact__groups__in=access.groups.all()) request.session['prev'] = list(msgs.values_list('pk', flat=True)) msgs_list = [] if msgs: for msg in msgs: from django.template.loader import render_to_string can_view_number = request.user.has_perm('view_numbers') can_foward = request.user.has_perm('forward') row_rendered = \ render_to_string('ureport/partials/row.html', {'msg': msg, 'can_foward': can_foward, 'can_view_number': can_view_number, 'assign_polls': assign_polls}) m = {} m['text'] = msg.text m['date'] = str(msg.date.date()) if msg.connection.contact: m['name'] = msg.connection.contact.name else: m['name'] = 'Anonymous User' m['number'] = msg.connection.identity if msg.connection.contact \ and msg.connection.contact.reporting_location: m['district'] = \ msg.connection.contact.reporting_location.name else: m['district'] = 'N/A' rating = msg.details.filter(attribute__name='alerts') if rating: r = rating[0].value else: r = 0 m['row'] = row_rendered m['connection'] = msg.connection.pk m['pk'] = msg.pk msgs_list.append(m) return HttpResponse(mark_safe(simplejson.dumps(msgs_list))) else: return HttpResponse('success') if request.GET.get('rating', None): rating = request.GET.get('rating') descs = { '1': 'Requires Attention', '2': 'Moderate', '3': 'Important', '4': 'Urgent', '5': 'Very Urgent', } msg = Message.objects.get(pk=int(request.GET.get('msg'))) (rate, _) = MessageAttribute.objects.get_or_create(name='rating') MessageDetail.objects.create(message=msg, attribute=rate, value=rating, description=descs.get(rating, '')) response = \ """<li><a href='javascript:void(0)' class="rate%s" title="%s">%s</a></li>""" \ % (rating, descs.get(rating, ''), descs.get(rating, '')) return HttpResponse(mark_safe(response)) paginator = UreportPaginator(message_list.order_by('-date'), 10, body=12, padding=2) page = request.GET.get('page', 1) try: messages = paginator.page(page) except (PageNotAnInteger, EmptyPage): # If page is not an integer, deliver first page. messages = paginator.page(1) return render_to_response(template, { 'messages': messages, 'assign_polls': assign_polls, 'paginator': paginator, 'capture_status': capture_status, 'rate': rate, 'district_form': district_form, 'range_form': range_form, 'groupform': groupform, 'access': access, 'accesses': Access.objects.filter(user__username__startswith='GAPP'), }, context_instance=RequestContext(request))
def alerts(request): access = get_access(request) poll_form = NewPollForm() range_form = rangeForm() poll_form.updateTypes() assign_polls = Poll.objects.exclude(start_date=None).order_by('-pk')[0:5] district_form = DistrictForm(request.POST or None) if request.GET.get('reset_districts', None): request.session['districts'] = None request.session['groups'] = None if district_form.is_valid(): request.session['districts'] = [ c.pk for c in district_form.cleaned_data['districts'] ] groupform = AssignResponseGroupForm(request=request, access=access) if request.method == 'POST' and request.POST.get('groups', None): g_form = AssignResponseGroupForm(request.POST, request=request) if g_form.is_valid(): request.session['groups'] = g_form.cleaned_data['groups'] template = 'ureport/polls/alerts.html' if request.session.get('districts'): message_list = \ Message.objects.filter(details__attribute__name='alert' ).filter(connection__contact__reporting_location__in=request.session.get('districts')) else: message_list = Message.objects.filter(details__attribute__name='alert') if request.session.get('groups', None): message_list = message_list.filter( connection__contact__groups__in=request.session.get('groups')) if access: message_list = message_list.filter( connection__contact__groups__in=access.groups.all()) (capture_status, _) = \ Settings.objects.get_or_create(attribute='alerts') (rate, _) = MessageAttribute.objects.get_or_create(name='rating') # message_list=[Message.objects.latest('date')] # use more efficient count if request.GET.get('download', None) and access is None: range_form = rangeForm(request.POST) if range_form.is_valid(): start = range_form.cleaned_data['startdate'] end = range_form.cleaned_data['enddate'] from django.core.servers.basehttp import FileWrapper cols = [ "replied", "rating", "direction", "district", "date", "message", "id", "forwarded" ] data = AlertsExport.objects.filter( date__range=(start, end)).values_list(*cols).iterator() excel_file_path = \ os.path.join(os.path.join(os.path.join(UREPORT_ROOT, 'static'), 'spreadsheets'), 'alerts.xlsx') ExcelResponse(data, output_name=excel_file_path, write_to_file=True, headers=cols) response = HttpResponse( FileWrapper(open(excel_file_path)), content_type= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) response[ 'Content-Disposition'] = 'attachment; filename=alerts.xlsx' from django import db db.reset_queries() response['Cache-Control'] = 'no-cache' return response if request.GET.get('search', None): search = request.GET.get('search') if search[0] == '"' and search[-1] == '"': search = search[1:-1] message_list = message_list.filter( Q(text__iregex=".*\m(%s)\y.*" % search) | Q(connection__contact__reporting_location__name__iregex= ".*\m(%s)\y.*" % search) | Q(connection__pk__iregex=".*\m(%s)\y.*" % search)) elif search[0] == "'" and search[-1] == "'": search = search[1:-1] message_list = message_list.filter( Q(text__iexact=search) | Q(connection__contact__reporting_location__name__iexact=search) | Q(connection__pk__iexact=search)) elif search == "=numerical value()": message_list = message_list.filter(text__iregex="(-?\d+(\.\d+)?)") else: message_list = message_list.filter( Q(text__icontains=search) | Q(connection__contact__reporting_location__name__icontains= search) | Q(connection__pk__icontains=search)) if request.GET.get('capture', None): (s, _) = Settings.objects.get_or_create(attribute='alerts') if s.value == 'true': s.value = 'false' s.save() reply = 'Start Capture' else: s.value = 'true' s.save() reply = 'Stop Capture' return HttpResponse(reply) if request.GET.get('ajax', None): date = datetime.datetime.now() - datetime.timedelta(seconds=30) prev = request.session.get('prev', []) msgs = Message.objects.filter( details__attribute__name='alert', direction='I').filter(date__gte=date).exclude(pk__in=prev) if access: msgs = msgs.filter( connection__contact__groups__in=access.groups.all()) request.session['prev'] = list(msgs.values_list('pk', flat=True)) msgs_list = [] if msgs: for msg in msgs: from django.template.loader import render_to_string can_view_number = request.user.has_perm('view_numbers') can_foward = request.user.has_perm('forward') row_rendered = \ render_to_string('ureport/partials/row.html', {'msg': msg, 'can_foward': can_foward, 'can_view_number': can_view_number, 'assign_polls': assign_polls}) m = {} m['text'] = msg.text m['date'] = str(msg.date.date()) if msg.connection.contact: m['name'] = msg.connection.contact.name else: m['name'] = 'Anonymous User' m['number'] = msg.connection.identity if msg.connection.contact \ and msg.connection.contact.reporting_location: m['district'] = \ msg.connection.contact.reporting_location.name else: m['district'] = 'N/A' rating = msg.details.filter(attribute__name='alerts') if rating: r = rating[0].value else: r = 0 m['row'] = row_rendered m['connection'] = msg.connection.pk m['pk'] = msg.pk msgs_list.append(m) return HttpResponse(mark_safe(simplejson.dumps(msgs_list))) else: return HttpResponse('success') if request.GET.get('rating', None): rating = request.GET.get('rating') descs = { '1': 'Requires Attention', '2': 'Moderate', '3': 'Important', '4': 'Urgent', '5': 'Very Urgent', } msg = Message.objects.get(pk=int(request.GET.get('msg'))) (rate, _) = MessageAttribute.objects.get_or_create(name='rating') det = MessageDetail.objects.create(message=msg, attribute=rate, value=rating, description=descs.get(rating, '')) response = \ """<li><a href='javascript:void(0)' class="rate%s" title="%s">%s</a></li>""" \ % (rating, descs.get(rating, ''), descs.get(rating, '')) return HttpResponse(mark_safe(response)) paginator = UreportPaginator(message_list.order_by('-date'), 10, body=12, padding=2) page = request.GET.get('page', 1) try: messages = paginator.page(page) except (PageNotAnInteger, EmptyPage): # If page is not an integer, deliver first page. messages = paginator.page(1) return render_to_response(template, { 'messages': messages, 'assign_polls': assign_polls, 'paginator': paginator, 'capture_status': capture_status, 'rate': rate, 'district_form': district_form, 'range_form': range_form, 'groupform': groupform, }, context_instance=RequestContext(request))
def alerts(request): access = get_access(request) poll_form = NewPollForm() range_form = rangeForm() poll_form.updateTypes() assign_polls = Poll.objects.exclude(start_date=None).order_by('-pk')[0:5] district_form = DistrictForm(request.POST or None) if request.GET.get('reset_districts', None): request.session['districts'] = None request.session['groups'] = None if district_form.is_valid(): request.session['districts'] = [c.pk for c in district_form.cleaned_data['districts']] groupform = AssignResponseGroupForm(request=request, access=access) if request.method == 'POST' and request.POST.get('groups', None): g_form = AssignResponseGroupForm(request.POST, request=request) if g_form.is_valid(): request.session['groups'] = g_form.cleaned_data['groups'] template = 'ureport/polls/alerts.html' if request.session.get('districts'): message_list = \ Message.objects.filter(details__attribute__name='alert' ).filter(connection__contact__reporting_location__in=request.session.get('districts')) else: message_list = Message.objects.filter(details__attribute__name='alert') if request.session.get('groups', None): message_list = message_list.filter(connection__contact__groups__in=request.session.get('groups' )) if access: message_list = message_list.filter(connection__contact__groups__in=access.groups.all()) (capture_status, _) = \ Settings.objects.get_or_create(attribute='alerts') (rate, _) = MessageAttribute.objects.get_or_create(name='rating') # message_list=[Message.objects.latest('date')] # use more efficient count if request.GET.get('download', None) and access is None: range_form = rangeForm(request.POST) if range_form.is_valid(): start = range_form.cleaned_data['startdate'] end = range_form.cleaned_data['enddate'] from django.core.servers.basehttp import FileWrapper cols = ["replied", "rating", "direction", "district", "date", "message", "id", "forwarded"] data = AlertsExport.objects.filter(date__range=(start, end)).values_list(*cols).iterator() excel_file_path = \ os.path.join(os.path.join(os.path.join(UREPORT_ROOT, 'static'), 'spreadsheets'), 'alerts.xlsx') ExcelResponse(data, output_name=excel_file_path, write_to_file=True, headers=cols) response = HttpResponse(FileWrapper(open(excel_file_path)), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename=alerts.xlsx' from django import db db.reset_queries() response['Cache-Control'] = 'no-cache' return response if request.GET.get('search', None): search = request.GET.get('search') if search[0] == '"' and search[-1] == '"': search = search[1:-1] message_list = message_list.filter(Q(text__iregex=".*\m(%s)\y.*" % search) | Q(connection__contact__reporting_location__name__iregex=".*\m(%s)\y.*" % search) | Q(connection__pk__iregex=".*\m(%s)\y.*" % search)) elif search[0] == "'" and search[-1] == "'": search = search[1:-1] message_list = message_list.filter(Q(text__iexact=search) | Q(connection__contact__reporting_location__name__iexact=search) | Q(connection__pk__iexact=search)) elif search == "=numerical value()": message_list = message_list.filter(text__iregex="(-?\d+(\.\d+)?)") else: message_list = message_list.filter(Q(text__icontains=search) | Q(connection__contact__reporting_location__name__icontains=search) | Q(connection__pk__icontains=search)) if request.GET.get('capture', None): (s, _) = Settings.objects.get_or_create(attribute='alerts') if s.value == 'true': s.value = 'false' s.save() reply = 'Start Capture' else: s.value = 'true' s.save() reply = 'Stop Capture' return HttpResponse(reply) if request.GET.get('ajax', None): date = datetime.datetime.now() - datetime.timedelta(seconds=30) prev = request.session.get('prev', []) msgs = Message.objects.filter(details__attribute__name='alert', direction='I' ).filter(date__gte=date).exclude(pk__in=prev) if access: msgs = msgs.filter(connection__contact__groups__in=access.groups.all()) request.session['prev'] = list(msgs.values_list('pk', flat=True)) msgs_list = [] if msgs: for msg in msgs: from django.template.loader import render_to_string can_view_number = request.user.has_perm('view_numbers') can_foward = request.user.has_perm('forward') row_rendered = \ render_to_string('ureport/partials/row.html', {'msg': msg, 'can_foward': can_foward, 'can_view_number': can_view_number, 'assign_polls': assign_polls}) m = {} m['text'] = msg.text m['date'] = str(msg.date.date()) if msg.connection.contact: m['name'] = msg.connection.contact.name else: m['name'] = 'Anonymous User' m['number'] = msg.connection.identity if msg.connection.contact \ and msg.connection.contact.reporting_location: m['district'] = \ msg.connection.contact.reporting_location.name else: m['district'] = 'N/A' rating = msg.details.filter(attribute__name='alerts') if rating: r = rating[0].value else: r = 0 m['row'] = row_rendered m['connection'] = msg.connection.pk m['pk'] = msg.pk msgs_list.append(m) return HttpResponse(mark_safe(simplejson.dumps(msgs_list))) else: return HttpResponse('success') if request.GET.get('rating', None): rating = request.GET.get('rating') descs = { '1': 'Requires Attention', '2': 'Moderate', '3': 'Important', '4': 'Urgent', '5': 'Very Urgent', } msg = Message.objects.get(pk=int(request.GET.get('msg'))) (rate, _) = MessageAttribute.objects.get_or_create(name='rating' ) det = MessageDetail.objects.create(message=msg, attribute=rate, value=rating, description=descs.get(rating, '')) response = \ """<li><a href='javascript:void(0)' class="rate%s" title="%s">%s</a></li>""" \ % (rating, descs.get(rating, ''), descs.get(rating, '')) return HttpResponse(mark_safe(response)) paginator = UreportPaginator(message_list.order_by('-date'), 10, body=12, padding=2) page = request.GET.get('page', 1) try: messages = paginator.page(page) except (PageNotAnInteger, EmptyPage): # If page is not an integer, deliver first page. messages = paginator.page(1) return render_to_response(template, { 'messages': messages, 'assign_polls': assign_polls, 'paginator': paginator, 'capture_status': capture_status, 'rate': rate, 'district_form': district_form, 'range_form': range_form, 'groupform': groupform, }, context_instance=RequestContext(request))
def alerts(request, pk): access = get_access(request) poll_form = NewPollForm() range_form = rangeForm() poll_form.updateTypes() assign_polls = Poll.objects.exclude(start_date=None).order_by('-pk')[0:5] district_form = DistrictForm(request.POST or None) if request.GET.get('reset_districts', None): request.session['districts'] = None request.session['groups'] = None if district_form.is_valid(): request.session['districts'] = [c.pk for c in district_form.cleaned_data['districts']] groupform = AssignResponseGroupForm(request=request, access=access) if request.method == 'POST' and request.POST.get('groups', None): g_form = AssignResponseGroupForm(request.POST, request=request) if g_form.is_valid(): request.session['groups'] = g_form.cleaned_data['groups'] template = 'ureport/polls/alerts.html' if request.session.get('districts'): message_list = \ Message.objects.filter(details__attribute__name='alert', direction='I', ).filter(connection__contact__reporting_location__in=request.session.get('districts')) else: message_list = Message.objects.filter(details__attribute__name='alert', direction='I') if request.session.get('groups', None): message_list = message_list.filter(connection__contact__groups__in=request.session.get('groups' )) if access: if access.assigned_messages.exists(): message_list = access.assigned_messages.all() else: message_list = message_list.filter(connection__contact__groups__in=access.groups.all()) (capture_status, _) = \ Settings.objects.get_or_create(attribute='alerts') (rate, _) = MessageAttribute.objects.get_or_create(name='rating') # message_list=[Message.objects.latest('date')] # use more efficient count if request.GET.get('download', None): range_form = rangeForm(request.POST) if range_form.is_valid(): tasks.export_alerts_task.delay(range_form, access, request.get_host(), request.user.username) return HttpResponseRedirect(reverse('alerts', args=[request.user.pk])) if request.GET.get('search', None): search = request.GET.get('search') if search[0] == '"' and search[-1] == '"': search = search[1:-1] message_list = message_list.filter(Q(text__iregex=".*\m(%s)\y.*" % search) | Q(connection__contact__reporting_location__name__iregex=".*\m(%s)\y.*" % search) | Q(connection__pk__iregex=".*\m(%s)\y.*" % search)) elif search[0] == "'" and search[-1] == "'": search = search[1:-1] message_list = message_list.filter(Q(text__iexact=search) | Q(connection__contact__reporting_location__name__iexact=search) | Q(connection__pk__iexact=search)) elif search == "=numerical value()": message_list = message_list.filter(text__iregex="(-?\d+(\.\d+)?)") else: message_list = message_list.filter(Q(text__icontains=search) | Q(connection__contact__reporting_location__name__icontains=search) | Q(connection__pk__icontains=search)) if request.GET.get('capture', None): (s, _) = Settings.objects.get_or_create(attribute='alerts') if s.value == 'true': s.value = 'false' s.save() reply = gettext('Start Capture') else: s.value = 'true' s.save() reply = gettext('Stop Capture') return HttpResponse(reply) if request.GET.get('ajax', None): if request.GET.get('ajax') == 'get_replied': date = datetime.datetime.now() - datetime.timedelta(seconds=4 * 60 * 60 * 30) msgs = Message.objects.filter(details__attribute__name='replied', direction='I', date__gte=date).values( 'pk') return HttpResponse(simplejson.dumps(list(msgs)), content_type="application/json") date = datetime.datetime.now() - datetime.timedelta(seconds=30) prev = request.session.get('prev', []) msgs = Message.objects.filter(details__attribute__name='alert', direction='I' ).filter(date__gte=date).exclude(pk__in=prev) if access: msgs = msgs.filter(connection__contact__groups__in=access.groups.all()) request.session['prev'] = list(msgs.values_list('pk', flat=True)) msgs_list = [] if msgs: for msg in msgs: from django.template.loader import render_to_string can_view_number = request.user.has_perm('view_numbers') can_foward = request.user.has_perm('forward') row_rendered = \ render_to_string('ureport/partials/row.html', {'msg': msg, 'can_foward': can_foward, 'can_view_number': can_view_number, 'assign_polls': assign_polls}) m = {} m['text'] = msg.text m['date'] = str(msg.date.date()) if msg.connection.contact: m['name'] = msg.connection.contact.name else: m['name'] = 'Anonymous User' m['number'] = msg.connection.identity if msg.connection.contact \ and msg.connection.contact.reporting_location: m['district'] = \ msg.connection.contact.reporting_location.name else: m['district'] = 'N/A' rating = msg.details.filter(attribute__name='alerts') if rating: r = rating[0].value else: r = 0 m['row'] = row_rendered m['connection'] = msg.connection.pk m['pk'] = msg.pk msgs_list.append(m) return HttpResponse(mark_safe(simplejson.dumps(msgs_list))) else: return HttpResponse('success') if request.GET.get('rating', None): rating = request.GET.get('rating') descs = { '1': 'Requires Attention', '2': 'Moderate', '3': 'Important', '4': 'Urgent', '5': 'Very Urgent', } msg = Message.objects.get(pk=int(request.GET.get('msg'))) (rate, _) = MessageAttribute.objects.get_or_create(name='rating') MessageDetail.objects.create(message=msg, attribute=rate, value=rating, description=descs.get(rating, '')) response = \ """<li><a href='javascript:void(0)' class="rate%s" title="%s">%s</a></li>""" \ % (rating, descs.get(rating, ''), descs.get(rating, '')) return HttpResponse(mark_safe(response)) paginator = UreportPaginator(message_list.order_by('-date'), 10, body=12, padding=2) page = request.GET.get('page', 1) try: messages = paginator.page(page) except (PageNotAnInteger, EmptyPage): # If page is not an integer, deliver first page. messages = paginator.page(1) return render_to_response(template, { 'messages': messages, 'assign_polls': assign_polls, 'paginator': paginator, 'capture_status': capture_status, 'rate': rate, 'district_form': district_form, 'range_form': range_form, 'groupform': groupform, 'access': access, 'accesses': Access.objects.filter(user__username__startswith='GAPP'), }, context_instance=RequestContext(request))