1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/data_views.py
Matt Griswold ba6f9b6432 Qu1003 (#621)
* use new peeringdb client (1.0.0) for pdb_load_data sync (#599)

* drop django-mobi for lack of py3/dj2 support (#492)
remove django-forms-bootstrap for lack of py3/dj2 support (#492)

* black formatted

* django2.2 and py3 upgrade (#492)

* drop ixlans (#21) ui and api changes

* drop local_asn (#168)

* org search (#193)

* phone number validation (#50)

* implement help text tooltips (#228)

* Mark own ASN as transit-free (#394)

* py3 fix for `pdb_migrate_ixlans` command when writing migration report

* pdb_migrate_ixlans: properly handle py3 Runtime error if ixlan dict changes during iteration

* set rest DEFAULT_SCHEMA_CLASS to coreapi to fix swagger apidocs
fix migration 0027 missing from facsimile manifest

* fix swagger doc strings

* fix tests that were broken from api doc fixes

* fix UniqueFieldValidator for netixlan ipaddress validation that broke during django/drf upgrade

* fix org merge tool layout issues

* travis config

* update pipfile and lock

* black formatting

* update travis dist

* beta mode banner (#411)

* add beta banner template (#411)

* automatically scheduled sync may not always be on, add a flag that lets us reflect that state in the beta banner message
clean up beta banner implementation (#411)

* add tests for beta banner (#411)
2020-01-08 13:29:58 -06:00

199 lines
5.0 KiB
Python

"""
This holds JSON views for various data sets,
Mostly these are needed for filling form-selects for editable
mode
"""
import datetime
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
import django_countries
from . import models
import django_peeringdb.const as const
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from peeringdb_server.models import Organization, Network, Sponsorship
# def _(x):
# return x
# until django-peeringdb is updated we want to remove
# the 100+ Gbps choice since it's redundant
const.TRAFFIC = [(k, i) for k, i in const.TRAFFIC if k != "100+ Gbps"]
# create enums without duplicate "Not Disclosed" choices
const.RATIOS_TRUNC = const.RATIOS[1:]
const.SCOPES_TRUNC = const.SCOPES[1:]
const.NET_TYPES_TRUNC = const.NET_TYPES[1:]
# create enums without duplicate "Not Disclosed" choices
# but with the one Not Disclosed choice combining both
# values in a comma separated fashion - user for
# advanced search
const.RATIOS_ADVS = list(const.RATIOS[1:])
const.RATIOS_ADVS[0] = (",%s" % const.RATIOS_ADVS[0][0], const.RATIOS_ADVS[0][1])
const.SCOPES_ADVS = list(const.SCOPES[1:])
const.SCOPES_ADVS[0] = (",%s" % const.SCOPES_ADVS[0][0], const.SCOPES_ADVS[0][1])
const.NET_TYPES_ADVS = list(const.NET_TYPES[1:])
const.NET_TYPES_ADVS[0] = (
",%s" % const.NET_TYPES_ADVS[0][0],
const.NET_TYPES_ADVS[0][1],
)
const.ORG_GROUPS = (("member", "member"), ("admin", "admin"))
const.POC_ROLES = sorted(const.POC_ROLES, key=lambda x: x[1])
BOOL_CHOICE = ((False, _("No")), (True, _("Yes")))
const.BOOL_CHOICE_STR = (("False", _("No")), ("True", _("Yes")))
def countries_w_blank(request):
"""
Returns all valid countries and their country codes with a blank field
"""
return JsonResponse(
{
"countries_b": [{"id": "", "name": ""}]
+ [
{"id": str(code), "name": str(name)}
for code, name in list(django_countries.countries)
]
}
)
def countries(request):
"""
Returns all valid countries and their country codes
"""
return JsonResponse(
{
"countries": [
{"id": str(code), "name": str(name)}
for code, name in list(django_countries.countries)
]
}
)
def sponsorships(request):
"""
Returns all sponsorships
"""
sponsors = {}
for org, sponsorship in Sponsorship.active_by_org():
sponsors[org.id] = {"id": org.id, "name": sponsorship.label.lower()}
return JsonResponse({"sponsors": sponsors,})
@login_required
def facilities(request):
"""
Returns all valid facilities with id and name
"""
return JsonResponse(
{
"facilities": [
{"id": fac.id, "name": str(fac.name)}
for fac in models.Facility.handleref.all().undeleted().order_by("name")
]
}
)
def enum(request, name):
if name.upper() not in [
"RATIOS",
"RATIOS_TRUNC",
"RATIOS_ADVS",
"TRAFFIC",
"SCOPES",
"SCOPES_TRUNC",
"SCOPES_ADVS",
"NET_TYPES",
"NET_TYPES_TRUNC",
"NET_TYPES_ADVS",
"POLICY_GENERAL",
"POLICY_LOCATIONS",
"POLICY_CONTRACTS",
"REGIONS",
"POC_ROLES",
"MEDIA",
"PROTOCOLS",
"ORG_GROUPS",
"BOOL_CHOICE_STR",
"VISIBILITY",
]:
raise Exception("Unknown enum")
return JsonResponse(
{
"enum/%s"
% name: [
{
"id": id,
# as of django-peeringdb 1.0.0 already comes in
# translated
"name": n,
}
for id, n in getattr(const, name.upper())
]
}
)
def asns(request):
"""
Returns a JSON response with a list of asns that the user's
organizations own, to use for selecting asn in netixlan
creation
"""
rv = []
try:
net = Network.objects.get(id=request.GET.get("id"))
org = net.org
except Network.DoesNotExist:
return JsonResponse({"asns": []})
for net in org.net_set_active.order_by("asn"):
rv.append({"id": net.asn, "name": net.asn})
return JsonResponse({"asns": rv})
def organizations(request):
"""
Returns a JSON response with a list of organization names and ids
This is currently only used by the org-merge-tool which is only
available to site administrators.
"""
if not request.user.is_superuser:
return JsonResponse({}, status=403)
return JsonResponse(
{
"organizations": [
{"id": o.id, "name": o.name}
for o in Organization.objects.filter(status="ok").order_by("name")
]
}
)
def languages(request):
from django.conf import settings
cur_language = translation.get_language()
return JsonResponse(
{"locales": [{"id": id, "name": _(name)} for (id, name) in settings.LANGUAGES]}
)