Changeset View
Changeset View
Standalone View
Standalone View
swh/web/add_forge_now/models.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from __future__ import annotations | from __future__ import annotations | ||||
import enum | import enum | ||||
from typing import List | from typing import List | ||||
from urllib.parse import urlparse | |||||
from django.db import models | from django.db import models | ||||
from ..config import get_config | from ..config import get_config | ||||
from ..inbound_email.utils import get_address_for_pk | from ..inbound_email.utils import get_address_for_pk | ||||
from .apps import APP_LABEL | from .apps import APP_LABEL | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | class Meta: | ||||
app_label = APP_LABEL | app_label = APP_LABEL | ||||
db_table = "add_forge_request" | db_table = "add_forge_request" | ||||
@property | @property | ||||
def inbound_email_address(self) -> str: | def inbound_email_address(self) -> str: | ||||
"""Generate an email address for correspondence related to this request.""" | """Generate an email address for correspondence related to this request.""" | ||||
base_address = get_config()["add_forge_now"]["email_address"] | base_address = get_config()["add_forge_now"]["email_address"] | ||||
return get_address_for_pk(salt=APP_LABEL, base_address=base_address, pk=self.pk) | return get_address_for_pk(salt=APP_LABEL, base_address=base_address, pk=self.pk) | ||||
@property | |||||
def forge_domain(self) -> str: | |||||
"""Get the domain/netloc out of the forge_url. | |||||
Fallback to using the first part of the url path, if the netloc can't be found | |||||
(for instance, if the url scheme hasn't been set). | |||||
""" | |||||
parsed_url = urlparse(self.forge_url) | |||||
domain = parsed_url.netloc | |||||
if not domain: | |||||
domain = parsed_url.path.split("/", 1)[0] | |||||
return domain |