From 056d937f15f52706079a2f693c57b174df5e5904 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Sat, 2 Oct 2021 05:56:16 +0200 Subject: [PATCH] #50 Added logic to set link_type based on url and LINK_DOMAINS dict. --- django_lostplaces/lostplaces/views/views.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/views/views.py b/django_lostplaces/lostplaces/views/views.py index f11651b..491d615 100644 --- a/django_lostplaces/lostplaces/views/views.py +++ b/django_lostplaces/lostplaces/views/views.py @@ -12,7 +12,7 @@ from django.http import HttpResponseForbidden from django.utils.translation import ugettext_lazy as _ from lostplaces.forms import SignupVoucherForm, TagSubmitForm -from lostplaces.models import Place, ExternalLink +from lostplaces.models import Place, ExternalLink, external_links from lostplaces.views.base_views import IsAuthenticatedMixin from lostplaces.common import redirect_referer_or @@ -54,6 +54,18 @@ class ExternalLinkCreateView(PlaceAssetCreateView): template_name = 'external_link/external_link_create.html' success_message = _('External link submitted') + def post(self, request, place_id, *args, **kwargs): + response = super().post(request, place_id, *args, **kwargs) + if not self.object.linktype: + for domain, link_type in external_links.LINK_DOMAINS.items(): + if domain in self.object.url: + self.object.linktype = link_type + self.object.save() + break + else: + self.object.linktype = None + return response + class ExternalLinkDeleteView(PlaceAssetDeleteView): model = ExternalLink pk_url_kwarg = 'pk'