mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* Support 202403 prepare * refactor peeringdb_server/management/commands/pdb_delete_outdated_pending_affil_request.py for improved code structure fix tests/test_settings.py::TestAutoApproveAffiliation::test_setting by using rdap mocking * db schema docs and api docs regen * rir should be RIR --------- Co-authored-by: 20C <code@20c.com>
124 lines
3.3 KiB
Python
124 lines
3.3 KiB
Python
"""
|
|
Assorted utility functions for peeringdb site templates.
|
|
"""
|
|
import ipaddress
|
|
from decimal import Decimal
|
|
|
|
import django_peeringdb.const as const
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django_grainy.util import Permissions, check_permissions, get_permissions # noqa
|
|
|
|
from peeringdb_server.models import (
|
|
CarrierFacility,
|
|
InternetExchangeFacility,
|
|
NetworkFacility,
|
|
)
|
|
from peeringdb_server.permissions import APIPermissionsApplicator # noqa
|
|
|
|
|
|
def disable_auto_now_and_save(entity):
|
|
updated_field = entity._meta.get_field("updated")
|
|
updated_field.auto_now = False
|
|
entity.save()
|
|
updated_field.auto_now = True
|
|
|
|
|
|
def round_decimal(value, places):
|
|
if value is not None:
|
|
return value.quantize(Decimal(10) ** -places)
|
|
return value
|
|
|
|
|
|
def coerce_ipaddr(value):
|
|
"""
|
|
ipaddresses can have multiple formats that are equivalent.
|
|
This function will standardize a ipaddress string.
|
|
|
|
Note: this function is not a validator. If it errors
|
|
It will return the original string.
|
|
"""
|
|
try:
|
|
value = str(ipaddress.ip_address(value))
|
|
except ValueError:
|
|
pass
|
|
return value
|
|
|
|
|
|
def v2_social_media_services():
|
|
"""
|
|
Until v3 website is still set through the main `website` property
|
|
of the object, we need to skip it here so it is not rendered to
|
|
the UX as a pickable choice in the social media dropdown
|
|
"""
|
|
return [x for x in const.SOCIAL_MEDIA_SERVICES if x[0] != "website"]
|
|
|
|
|
|
def generate_social_media_render_data(data, social_media, insert_index, dismiss):
|
|
"""
|
|
Generate the data for rendering the social media in view.html.
|
|
This function will insert the generated social media data to `data`.
|
|
"""
|
|
idx = insert_index
|
|
social_media = social_media
|
|
for i, soc in enumerate(social_media):
|
|
# until v3 website is still set through the main `website`
|
|
# field of the object, we need to skip it here so it
|
|
# is not rendered to the UX twice
|
|
|
|
if soc["service"] == "website":
|
|
continue
|
|
|
|
service = soc.get("service", dismiss)
|
|
|
|
idx = idx + 1
|
|
soc_data = {
|
|
"name": f"sc_value_{i}",
|
|
"value": soc.get("identifier", dismiss),
|
|
"label": _(service.capitalize()),
|
|
"editable_label": True,
|
|
"type": "soc",
|
|
"label_type": "list",
|
|
"label_name": f"sc_field_{i}",
|
|
"label_data": "enum/social_media_services",
|
|
"label_value": service,
|
|
}
|
|
# if i == len(social_media) - 1:
|
|
data.get("fields").insert(idx, soc_data)
|
|
|
|
soc_data = {"last_soc_field": True}
|
|
data.get("fields").insert(idx + 1, soc_data)
|
|
return data
|
|
|
|
|
|
def objfac_tupple(objfac_qset, obj):
|
|
data = {}
|
|
for objfac in objfac_qset:
|
|
if not data.get(getattr(objfac, obj)):
|
|
data[getattr(objfac, obj)] = [objfac.facility]
|
|
else:
|
|
data[getattr(objfac, obj)].append(objfac.facility)
|
|
return data
|
|
|
|
|
|
def generate_balloonstyle_text(keys):
|
|
table_data = ""
|
|
for key in keys:
|
|
table_data += f"""
|
|
<tr>
|
|
<td>$[{key}/displayName]</td>
|
|
<td>$[{key}]</td>
|
|
</tr>
|
|
"""
|
|
ballon_text = f"""
|
|
<h3>$[name]</h3>
|
|
$[description]
|
|
</br>
|
|
</br>
|
|
<table border="1">
|
|
<tbody>
|
|
{table_data}
|
|
</tbody>
|
|
</table>
|
|
"""
|
|
return ballon_text
|