def trust_control(request): #Blacklist, Whitelist prefix_list = \ map(str, Prefix.objects.values_list("prefix", flat=True).all().order_by('prefix')) prefix_list = (','.join('"' + item + '"' for item in prefix_list)) prefix_list = "[" + str(prefix_list) + "]" blacklist = Blacklist.objects.filter(user=request.user).order_by('id') whitelist = Whitelist.objects.filter(user=request.user).order_by('id') # blacklist form bl_country_form = BWCountryForm() bl_prefix_form = BWPrefixForm() # whitelist form wl_country_form = BWCountryForm() wl_prefix_form = BWPrefixForm() template = 'frontend/cdr_alert/common_black_white_list.html' data = { 'module': current_view(request), 'prefix_list': prefix_list, 'bl_country_form': bl_country_form, 'bl_prefix_form': bl_prefix_form, 'wl_country_form': wl_country_form, 'wl_prefix_form': wl_prefix_form, 'blacklist': blacklist, 'whitelist': whitelist, 'notice_count': notice_count(request), } return render_to_response(template, data, context_instance=RequestContext(request))
def blacklist_by_country(self, request): """Add custom method in django admin view to import CSV file of Contacts **Attributes**: * ``form`` - BWCountryForm() * ``template`` - admin/cdr_alert/blacklist/blacklist_by_country.html **Logic Description**: **Important variable**: """ opts = Blacklist._meta form = BWCountryForm() prefix_list = [] if request.method == 'POST': form = BWCountryForm(request.POST) if form.is_valid(): country = int(request.POST['country']) prefix_list = \ Prefix.objects.values('prefix').filter(country_id=country) msg = _("Successfully added prefix into blacklist") if request.POST.getlist('blacklist_country'): # blacklist whole country Blacklist.objects.create( phonenumber_prefix=int(prefix_list[0]['prefix']), country=Country.objects.get(pk=country)) messages.info(request, msg) return HttpResponseRedirect( reverse("admin:cdr_alert_blacklist_changelist")) else: values = request.POST.getlist('select') if values: #blacklist_prefix = ", ".join(["%s" % el for el in values]) for i in values: Blacklist.objects.create( phonenumber_prefix=int(i), country=Country.objects.get(pk=country)) messages.info(request, msg) return HttpResponseRedirect( reverse("admin:cdr_alert_blacklist_changelist")) ctx = RequestContext(request, { 'title': _('Blacklist by country'), 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('cdr_alert'), 'prefix_list': prefix_list, }) template_name = 'admin/cdr_alert/blacklist/blacklist_by_country.html' return render_to_response(template_name, context_instance=ctx)
def blacklist_by_country(self, request): """Add custom method in django admin view to import CSV file of Contacts **Attributes**: * ``form`` - BWCountryForm() * ``template`` - admin/cdr_alert/blacklist/blacklist_by_country.html **Logic Description**: **Important variable**: """ opts = Blacklist._meta app_label = opts.app_label form = BWCountryForm() prefix_list = [] if request.method == 'POST': form = BWCountryForm(request.POST) if form.is_valid(): country = int(request.POST['country']) prefix_list = Prefix.objects.values('prefix').filter(country_id=country) msg = _("Successfully added prefix into blacklist") if request.POST.getlist('blacklist_country'): # blacklist whole country Blacklist.objects.create(phonenumber_prefix=int(prefix_list[0]['prefix']), country=Country.objects.get(pk=country)) messages.info(request, msg) return HttpResponseRedirect(reverse("admin:cdr_alert_blacklist_changelist")) else: values = request.POST.getlist('select') if values: #blacklist_prefix = ", ".join(["%s" % el for el in values]) for i in values: Blacklist.objects.create(phonenumber_prefix=int(i), country=Country.objects.get(pk=country)) messages.info(request, msg) return HttpResponseRedirect(reverse("admin:cdr_alert_blacklist_changelist")) ctx = RequestContext(request, { 'title': _('Blacklist by country'), 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'app_label': _('cdr_alert'), 'prefix_list': prefix_list, }) template_name = 'admin/cdr_alert/blacklist/blacklist_by_country.html' return render_to_response(template_name, context_instance=ctx)
def whitelist_by_country(self, request): """Add custom method in django admin view to import CSV file of Contacts **Attributes**: * ``form`` - BWCountryForm() * ``template`` - admin/cdr_alert/whitelist/whitelist_by_country.html **Logic Description**: **Important variable**: """ opts = Whitelist._meta form = BWCountryForm('whitelist', request.POST or None) prefix_list = [] if form.is_valid(): country = int(request.POST['country']) prefix_list = Prefix.objects.values('prefix').filter(country_id=country) msg = _("successfully added prefix into whitelist") if request.POST.getlist('whitelist_country'): # whitelist whole country Whitelist.objects.create( phonenumber_prefix=int(prefix_list[0]['prefix']), country=Country.objects.get(pk=country), user=request.user) messages.info(request, msg) return HttpResponseRedirect(reverse("admin:cdr_alert_whitelist_changelist")) else: values = request.POST.getlist('select') if values: for i in values: Whitelist.objects.create( phonenumber_prefix=int(i), country=Country.objects.get(pk=country), user=request.user) messages.info(request, msg) return HttpResponseRedirect(reverse("admin:cdr_alert_whitelist_changelist")) ctx = RequestContext(request, { 'title': _('whitelist by country'), 'form': form, 'opts': opts, 'model_name': opts.object_name.lower(), 'prefix_list': prefix_list, }) return render_to_response('admin/cdr_alert/whitelist/whitelist_by_country.html', context_instance=ctx)
def test_admin_alert_whitelist_by_country(self): """Test Function to check alert whitelist by country""" response = self.client.get( '/admin/cdr_alert/whitelist/whitelist_by_country/') self.assertTrue(response.context['form'], BWCountryForm()) self.assertTemplateUsed( response, 'admin/cdr_alert/whitelist/whitelist_by_country.html') self.failUnlessEqual(response.status_code, 200) response = self.client.post( '/admin/cdr_alert/whitelist/whitelist_by_country/', { 'country': 198, 'whitelist_country': [], 'select': [34] }, follow=True) self.failUnlessEqual(response.status_code, 200) response = self.client.post( '/admin/cdr_alert/whitelist/whitelist_by_country/', { 'country': 198, 'whitelist_country': [198], 'select': [34] }, follow=True) self.failUnlessEqual(response.status_code, 200)
def trust_control(request): #Blacklist, Whitelist blacklist = Blacklist.objects.filter(user=request.user).order_by('id') whitelist = Whitelist.objects.filter(user=request.user).order_by('id') # blacklist form bl_country_form = BWCountryForm(form_type='blacklist') bl_prefix_form = BWPrefixForm(form_type='blacklist') # whitelist form wl_country_form = BWCountryForm(form_type='whitelist') wl_prefix_form = BWPrefixForm(form_type='whitelist') data = { 'bl_country_form': bl_country_form, 'bl_prefix_form': bl_prefix_form, 'wl_country_form': wl_country_form, 'wl_prefix_form': wl_prefix_form, 'blacklist': blacklist, 'whitelist': whitelist, } return render_to_response('cdr_alert/common_black_white_list.html', data, context_instance=RequestContext(request))