コード例 #1
0
ファイル: admin.py プロジェクト: BillTheBest/cdr-stats
    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)
コード例 #2
0
ファイル: admin.py プロジェクト: ZhiephieCook/cdr-stats
    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)