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

237 lines
6.1 KiB
Python
Raw Normal View History

2018-11-08 19:45:21 +00:00
"""
This holds JSON views for various data sets,
Mostly these are needed for filling form-selects for editable
mode
"""
import datetime
2018-11-08 19:45:21 +00:00
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
import django_countries
from . import models
2018-11-08 19:45:21 +00:00
import django_peeringdb.const as const
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
2019-12-05 16:57:52 +00:00
from peeringdb_server.models import Organization, Network, Sponsorship
2019-12-05 16:57:52 +00:00
# def _(x):
2018-11-08 19:45:21 +00:00
# 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:]
Support 202104 (#980) * Add migration for service level and terms * Add service level and terms to UI and serializer, as well as data/enum * Wire up data/enum endpoint and loader * remove proto_ from ix UI * derive fields for proto_unicast and proto_ipv6 * update tests for readonly fields * Fix query for protocols * Fix api bug with protocol * add readonly fields to django admin * rename readonly fields * Add translation to names * Add pdb api test for suggested facility re-add * Add printing debuggin test * add printing debugging serializer * Update _undelete with _reapprove to handle pending cases * Update tests (one is still failing) * adjust suggest test * Add ix_count to fac (834) * Add test for ix_count on fac (834) * Add fac_count to IX (836) * add ix_count and fac_count to Network * Refactor ix net_count filtering * Add filtering for 834, 835, 836 * Remove duplicates from the Network's ix_count * Setup Network for ix_count and fac_count (835) * initial obj_counts for Facilities and Exchanges * Add signals for updates to all counts * add migration * Add print statements to test * introduce reversion to tests * rename network count to net count across codebase * fix network_count typo * add migration to set default vals * fix filter tests for obj_counts * speed up migration * fix failing tests * fix final test * sort out migration tree and add fac offered fields * update frontend for facility dropdown offered_resilience * First pass at advanced api search for user story 1 * melissa geo lookup first steps * fix migration hierarchy * working melissa integration * begin ending filters for api endpoints * add more org_present endpoints * add search for IXs that match multiple networks * extend logic to facility * Add service level and terms to advanced search * use address2 field for lookup * melissa tests * cleanup and docs * uncomment offered_power * developed offered_power component * fix geo normalize existing cmd normalize state * change migration to match django-peeringdb * add offered_space field * Fill out remaining api filter fields * Add org_not_present endpoint filter * fix unit input ux * more ux fixes * remove merge cruft * google for geocoding various melissa improvements (consider result quality) * fix tests * refactor org_preset and org_not_present queries * ix capacity api filters * ix capacity filters for #802 advanced search ux for #802 * finalize advanced search UX for #802 * css fixes * remove cruft * fix net_count fac_count queries * add new fields to create facility (#800) tests for #802 and #800 * fix tests * remove #800 changes * fix capacity search * more #800 changes to remove * django-peeringdb 2.7.0 and pipenv relock * black format * pin black version Co-authored-by: Elliot Frank <elliot@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-05-19 09:11:30 -04:00
const.SERVICE_LEVEL_TYPES_TRUNC = const.SERVICE_LEVEL_TYPES[1:]
const.TERMS_TYPES_TRUNC = const.TERMS_TYPES[1:]
2018-11-08 19:45:21 +00:00
# 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:])
2019-12-05 16:57:52 +00:00
const.RATIOS_ADVS[0] = (",%s" % const.RATIOS_ADVS[0][0], const.RATIOS_ADVS[0][1])
2018-11-08 19:45:21 +00:00
const.SCOPES_ADVS = list(const.SCOPES[1:])
2019-12-05 16:57:52 +00:00
const.SCOPES_ADVS[0] = (",%s" % const.SCOPES_ADVS[0][0], const.SCOPES_ADVS[0][1])
2018-11-08 19:45:21 +00:00
const.NET_TYPES_ADVS = list(const.NET_TYPES[1:])
2019-12-05 16:57:52 +00:00
const.NET_TYPES_ADVS[0] = (
",%s" % const.NET_TYPES_ADVS[0][0],
const.NET_TYPES_ADVS[0][1],
)
Support 202104 (#980) * Add migration for service level and terms * Add service level and terms to UI and serializer, as well as data/enum * Wire up data/enum endpoint and loader * remove proto_ from ix UI * derive fields for proto_unicast and proto_ipv6 * update tests for readonly fields * Fix query for protocols * Fix api bug with protocol * add readonly fields to django admin * rename readonly fields * Add translation to names * Add pdb api test for suggested facility re-add * Add printing debuggin test * add printing debugging serializer * Update _undelete with _reapprove to handle pending cases * Update tests (one is still failing) * adjust suggest test * Add ix_count to fac (834) * Add test for ix_count on fac (834) * Add fac_count to IX (836) * add ix_count and fac_count to Network * Refactor ix net_count filtering * Add filtering for 834, 835, 836 * Remove duplicates from the Network's ix_count * Setup Network for ix_count and fac_count (835) * initial obj_counts for Facilities and Exchanges * Add signals for updates to all counts * add migration * Add print statements to test * introduce reversion to tests * rename network count to net count across codebase * fix network_count typo * add migration to set default vals * fix filter tests for obj_counts * speed up migration * fix failing tests * fix final test * sort out migration tree and add fac offered fields * update frontend for facility dropdown offered_resilience * First pass at advanced api search for user story 1 * melissa geo lookup first steps * fix migration hierarchy * working melissa integration * begin ending filters for api endpoints * add more org_present endpoints * add search for IXs that match multiple networks * extend logic to facility * Add service level and terms to advanced search * use address2 field for lookup * melissa tests * cleanup and docs * uncomment offered_power * developed offered_power component * fix geo normalize existing cmd normalize state * change migration to match django-peeringdb * add offered_space field * Fill out remaining api filter fields * Add org_not_present endpoint filter * fix unit input ux * more ux fixes * remove merge cruft * google for geocoding various melissa improvements (consider result quality) * fix tests * refactor org_preset and org_not_present queries * ix capacity api filters * ix capacity filters for #802 advanced search ux for #802 * finalize advanced search UX for #802 * css fixes * remove cruft * fix net_count fac_count queries * add new fields to create facility (#800) tests for #802 and #800 * fix tests * remove #800 changes * fix capacity search * more #800 changes to remove * django-peeringdb 2.7.0 and pipenv relock * black format * pin black version Co-authored-by: Elliot Frank <elliot@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-05-19 09:11:30 -04:00
const.SERVICE_LEVEL_TYPES_ADVS = list(const.SERVICE_LEVEL_TYPES[1:])
const.SERVICE_LEVEL_TYPES_ADVS[0] = (
",{}".format(const.SERVICE_LEVEL_TYPES_ADVS[0][0]),
const.SERVICE_LEVEL_TYPES_ADVS[0][1],
)
const.TERMS_TYPES_ADVS = list(const.TERMS_TYPES[1:])
const.TERMS_TYPES_ADVS[0] = (
",{}".format(const.TERMS_TYPES_ADVS[0][0]),
const.TERMS_TYPES_ADVS[0][1],
)
2018-11-08 19:45:21 +00:00
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
"""
2019-12-05 16:57:52 +00:00
return JsonResponse(
{
"countries_b": [{"id": "", "name": ""}]
+ [
{"id": str(code), "name": str(name)}
2019-12-05 16:57:52 +00:00
for code, name in list(django_countries.countries)
]
}
)
2018-11-08 19:45:21 +00:00
def countries(request):
"""
Returns all valid countries and their country codes
"""
2019-12-05 16:57:52 +00:00
return JsonResponse(
{
"countries": [
{"id": str(code), "name": str(name)}
2019-12-05 16:57:52 +00:00
for code, name in list(django_countries.countries)
]
}
)
2018-11-08 19:45:21 +00:00
def sponsorships(request):
"""
Returns all sponsorships
"""
sponsors = {}
for org, sponsorship in Sponsorship.active_by_org():
2019-12-05 16:57:52 +00:00
sponsors[org.id] = {"id": org.id, "name": sponsorship.label.lower()}
return JsonResponse(
{
"sponsors": sponsors,
}
)
2018-11-08 19:45:21 +00:00
@login_required
def facilities(request):
"""
Returns all valid facilities with id and name
"""
2019-12-05 16:57:52 +00:00
return JsonResponse(
{
"facilities": [
{"id": fac.id, "name": str(fac.name)}
2019-12-05 16:57:52 +00:00
for fac in models.Facility.handleref.all().undeleted().order_by("name")
]
}
)
2018-11-08 19:45:21 +00:00
def enum(request, name):
if name.upper() not in [
2019-12-05 16:57:52 +00:00
"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",
Support 202104 (#980) * Add migration for service level and terms * Add service level and terms to UI and serializer, as well as data/enum * Wire up data/enum endpoint and loader * remove proto_ from ix UI * derive fields for proto_unicast and proto_ipv6 * update tests for readonly fields * Fix query for protocols * Fix api bug with protocol * add readonly fields to django admin * rename readonly fields * Add translation to names * Add pdb api test for suggested facility re-add * Add printing debuggin test * add printing debugging serializer * Update _undelete with _reapprove to handle pending cases * Update tests (one is still failing) * adjust suggest test * Add ix_count to fac (834) * Add test for ix_count on fac (834) * Add fac_count to IX (836) * add ix_count and fac_count to Network * Refactor ix net_count filtering * Add filtering for 834, 835, 836 * Remove duplicates from the Network's ix_count * Setup Network for ix_count and fac_count (835) * initial obj_counts for Facilities and Exchanges * Add signals for updates to all counts * add migration * Add print statements to test * introduce reversion to tests * rename network count to net count across codebase * fix network_count typo * add migration to set default vals * fix filter tests for obj_counts * speed up migration * fix failing tests * fix final test * sort out migration tree and add fac offered fields * update frontend for facility dropdown offered_resilience * First pass at advanced api search for user story 1 * melissa geo lookup first steps * fix migration hierarchy * working melissa integration * begin ending filters for api endpoints * add more org_present endpoints * add search for IXs that match multiple networks * extend logic to facility * Add service level and terms to advanced search * use address2 field for lookup * melissa tests * cleanup and docs * uncomment offered_power * developed offered_power component * fix geo normalize existing cmd normalize state * change migration to match django-peeringdb * add offered_space field * Fill out remaining api filter fields * Add org_not_present endpoint filter * fix unit input ux * more ux fixes * remove merge cruft * google for geocoding various melissa improvements (consider result quality) * fix tests * refactor org_preset and org_not_present queries * ix capacity api filters * ix capacity filters for #802 advanced search ux for #802 * finalize advanced search UX for #802 * css fixes * remove cruft * fix net_count fac_count queries * add new fields to create facility (#800) tests for #802 and #800 * fix tests * remove #800 changes * fix capacity search * more #800 changes to remove * django-peeringdb 2.7.0 and pipenv relock * black format * pin black version Co-authored-by: Elliot Frank <elliot@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-05-19 09:11:30 -04:00
"SERVICE_LEVEL_TYPES_TRUNC",
"TERMS_TYPES_TRUNC",
"SERVICE_LEVEL_TYPES_ADVS",
"TERMS_TYPES_ADVS",
2018-11-08 19:45:21 +00:00
]:
raise Exception("Unknown enum")
2019-12-05 16:57:52 +00:00
return JsonResponse(
{
"enum/%s"
% name: [
{
"id": id,
# as of django-peeringdb 1.0.0 already comes in
# translated
"name": n,
2019-12-05 16:57:52 +00:00
}
for id, n in getattr(const, name.upper())
]
}
)
2018-11-08 19:45:21 +00:00
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})
Support 202104 (#980) * Add migration for service level and terms * Add service level and terms to UI and serializer, as well as data/enum * Wire up data/enum endpoint and loader * remove proto_ from ix UI * derive fields for proto_unicast and proto_ipv6 * update tests for readonly fields * Fix query for protocols * Fix api bug with protocol * add readonly fields to django admin * rename readonly fields * Add translation to names * Add pdb api test for suggested facility re-add * Add printing debuggin test * add printing debugging serializer * Update _undelete with _reapprove to handle pending cases * Update tests (one is still failing) * adjust suggest test * Add ix_count to fac (834) * Add test for ix_count on fac (834) * Add fac_count to IX (836) * add ix_count and fac_count to Network * Refactor ix net_count filtering * Add filtering for 834, 835, 836 * Remove duplicates from the Network's ix_count * Setup Network for ix_count and fac_count (835) * initial obj_counts for Facilities and Exchanges * Add signals for updates to all counts * add migration * Add print statements to test * introduce reversion to tests * rename network count to net count across codebase * fix network_count typo * add migration to set default vals * fix filter tests for obj_counts * speed up migration * fix failing tests * fix final test * sort out migration tree and add fac offered fields * update frontend for facility dropdown offered_resilience * First pass at advanced api search for user story 1 * melissa geo lookup first steps * fix migration hierarchy * working melissa integration * begin ending filters for api endpoints * add more org_present endpoints * add search for IXs that match multiple networks * extend logic to facility * Add service level and terms to advanced search * use address2 field for lookup * melissa tests * cleanup and docs * uncomment offered_power * developed offered_power component * fix geo normalize existing cmd normalize state * change migration to match django-peeringdb * add offered_space field * Fill out remaining api filter fields * Add org_not_present endpoint filter * fix unit input ux * more ux fixes * remove merge cruft * google for geocoding various melissa improvements (consider result quality) * fix tests * refactor org_preset and org_not_present queries * ix capacity api filters * ix capacity filters for #802 advanced search ux for #802 * finalize advanced search UX for #802 * css fixes * remove cruft * fix net_count fac_count queries * add new fields to create facility (#800) tests for #802 and #800 * fix tests * remove #800 changes * fix capacity search * more #800 changes to remove * django-peeringdb 2.7.0 and pipenv relock * black format * pin black version Co-authored-by: Elliot Frank <elliot@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-05-19 09:11:30 -04:00
def my_organizations(request):
"""
Returns a JSON response with a list of organization names and ids
that the requesting user is a member of
"""
if not request.user.is_authenticated:
return JsonResponse({"my_organizations": []})
Support 202104 (#980) * Add migration for service level and terms * Add service level and terms to UI and serializer, as well as data/enum * Wire up data/enum endpoint and loader * remove proto_ from ix UI * derive fields for proto_unicast and proto_ipv6 * update tests for readonly fields * Fix query for protocols * Fix api bug with protocol * add readonly fields to django admin * rename readonly fields * Add translation to names * Add pdb api test for suggested facility re-add * Add printing debuggin test * add printing debugging serializer * Update _undelete with _reapprove to handle pending cases * Update tests (one is still failing) * adjust suggest test * Add ix_count to fac (834) * Add test for ix_count on fac (834) * Add fac_count to IX (836) * add ix_count and fac_count to Network * Refactor ix net_count filtering * Add filtering for 834, 835, 836 * Remove duplicates from the Network's ix_count * Setup Network for ix_count and fac_count (835) * initial obj_counts for Facilities and Exchanges * Add signals for updates to all counts * add migration * Add print statements to test * introduce reversion to tests * rename network count to net count across codebase * fix network_count typo * add migration to set default vals * fix filter tests for obj_counts * speed up migration * fix failing tests * fix final test * sort out migration tree and add fac offered fields * update frontend for facility dropdown offered_resilience * First pass at advanced api search for user story 1 * melissa geo lookup first steps * fix migration hierarchy * working melissa integration * begin ending filters for api endpoints * add more org_present endpoints * add search for IXs that match multiple networks * extend logic to facility * Add service level and terms to advanced search * use address2 field for lookup * melissa tests * cleanup and docs * uncomment offered_power * developed offered_power component * fix geo normalize existing cmd normalize state * change migration to match django-peeringdb * add offered_space field * Fill out remaining api filter fields * Add org_not_present endpoint filter * fix unit input ux * more ux fixes * remove merge cruft * google for geocoding various melissa improvements (consider result quality) * fix tests * refactor org_preset and org_not_present queries * ix capacity api filters * ix capacity filters for #802 advanced search ux for #802 * finalize advanced search UX for #802 * css fixes * remove cruft * fix net_count fac_count queries * add new fields to create facility (#800) tests for #802 and #800 * fix tests * remove #800 changes * fix capacity search * more #800 changes to remove * django-peeringdb 2.7.0 and pipenv relock * black format * pin black version Co-authored-by: Elliot Frank <elliot@20c.com> Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-05-19 09:11:30 -04:00
return JsonResponse(
{
"my_organizations": [
{"id": o.id, "name": o.name} for o in request.user.organizations
]
}
)
2018-11-08 19:45:21 +00:00
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)
2019-12-05 16:57:52 +00:00
return JsonResponse(
{
"organizations": [
{"id": o.id, "name": o.name}
for o in Organization.objects.filter(status="ok").order_by("name")
]
}
)
2018-11-08 19:45:21 +00:00
def languages(request):
from django.conf import settings
2019-12-05 16:57:52 +00:00
2018-11-08 19:45:21 +00:00
cur_language = translation.get_language()
2019-12-05 16:57:52 +00:00
return JsonResponse(
{"locales": [{"id": id, "name": _(name)} for (id, name) in settings.LANGUAGES]}
)