You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by gc...@apache.org on 2023/01/11 18:18:51 UTC

[allura] branch gc/8484 updated: fixup! [#8484] improvements to validation and fixed project features duplication bug

This is an automated email from the ASF dual-hosted git repository.

gcruz pushed a commit to branch gc/8484
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/gc/8484 by this push:
     new 3e5c35514 fixup! [#8484] improvements to validation and fixed project features duplication bug
3e5c35514 is described below

commit 3e5c3551417b8c1b3535bd166cb26dc38c9c087a
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Wed Jan 11 12:18:36 2023 -0600

    fixup! [#8484] improvements to validation and fixed project features duplication bug
---
 Allura/allura/ext/admin/widgets.py | 19 ++++++++------
 Allura/allura/lib/validators.py    | 51 +++++++++++++++++++++++---------------
 2 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/Allura/allura/ext/admin/widgets.py b/Allura/allura/ext/admin/widgets.py
index 0ce7e4b15..de4fbf933 100644
--- a/Allura/allura/ext/admin/widgets.py
+++ b/Allura/allura/ext/admin/widgets.py
@@ -167,7 +167,7 @@ class MetadataAdmin(ff.AdminForm):
 
     class fields(ew_core.NameList):
         allowed_social_domains = aslist(tg.config.get('allowed_social_domains',
-                                                      ['facebook', 'instagram', 'linkedin', 'twitter']),
+                                                      ['facebook.com', 'instagram.com', 'linkedin.com', 'twitter.com']),
                                         ',')
         name = ew.InputField(field_type='text',
                              label='Name',
@@ -225,19 +225,22 @@ class MetadataAdmin(ff.AdminForm):
             field_type="text", label="Google Analytics ID",
             attrs=(dict(placeholder='UA-123456-0', pattern='UA-[0-9]+-[0-9]+')))
         twitter_handle = ew.InputField(
-            field_type="text", label='Twitter Handle', validator=formencode.All(fev.URL(add_http=True, if_empty=''),
-                                                                                V.SocialDomainValidator('twitter.com')))
+            field_type="text", label='Twitter Handle',
+            validator=formencode.All(fev.URL(add_http=True, if_empty=''),
+                                     V.SocialDomainValidator(domains=allowed_social_domains),
+                                     V.TwitterValidator))
+
         facebook_page = ew.InputField(field_type="text", label='Facebook page',
                                       validator=formencode.All(fev.URL(add_http=True),
-                                                               V.SocialDomainValidator('facebook.com')) )
+                                                               V.SocialDomainValidator(domains=allowed_social_domains),
+                                                               V.FacebookValidator) )
         instagram_page = ew.InputField(
             field_type="text", label='Instagram page',
-            validator=formencode.All(fev.URL(add_http=True), V.SocialDomainValidator('instagram.com')))
+            validator=formencode.All(fev.URL(add_http=True), V.SocialDomainValidator(domains=allowed_social_domains),
+                                     V.InstagramValidator))
 
         fediverse_address = ew.InputField(field_type="text", label="Mastodon address",
-                                          validator=V.SocialDomainValidator(domains=allowed_social_domains))
-
-
+                                          validator=V.FediverseValidator)
 
 
 class AuditLog(ew_core.Widget):
diff --git a/Allura/allura/lib/validators.py b/Allura/allura/lib/validators.py
index 231f6312f..b7a8d5828 100644
--- a/Allura/allura/lib/validators.py
+++ b/Allura/allura/lib/validators.py
@@ -503,30 +503,41 @@ class FediverseAddressValidator(fev.FancyValidator):
 
 
 class SocialDomainValidator(fev.FancyValidator):
-    def __init__(self, domain='', **kw):
-        self.domain = domain
+    def __init__(self,**kw):
         self.domains = kw.get('domains')
 
     def _to_python(self, value, state):
         url = urlsplit(value)
-        state_values = state.full_dict
-        if value.startswith('@') and not re.match(FEDIVERSE_REGEX , value):
-            if state_values.get('socialnetwork','') == 'Twitter' or state_values.get('twitter_handle',''):
-                value = f'https://twitter.com/{value.replace("@","")}'
-            elif state_values.get('socialnetwork','') == 'Instagram' or state_values.get('instagram_page',''):
-                value = f'https://instagram.com/{value.replace("@", "")}'
-            url = urlsplit(value)
-        if (state_values.get('socialnetwork','') == 'Mastodon' and state_values.get('accounturl','').startswith('http'))\
-                or state_values.get('fediverse_address','').startswith('http'):
-            fediverse_url = f'{url.path.replace("/", "")}@{url.netloc}'
-            if re.match(FEDIVERSE_REGEX, fediverse_url):
-                return fediverse_url
-        if value.startswith('@') and re.match(FEDIVERSE_REGEX, value) and state_values.get('socialnetwork','') != 'Mastodon':
+        if self.domains and not any(domain == url.netloc.replace('www.','') for domain in self.domains):
             raise fe.Invalid('Invalid domain for this field', value, state)
-        if not re.match(FEDIVERSE_REGEX , value):
-            if self.domain and not self.domain == url.netloc.replace('www.',''):
-                raise fe.Invalid('Invalid domain for this field', value, state)
-            if self.domains and not any(domain == url.netloc.replace('www.','') for domain in self.domains):
-                raise fe.Invalid('Invalid domain for this field', value, state)
+        return value
+
+class TwitterValidator(fev.FancyValidator):
+    def _to_python(self, value, state):
+        if value.startswith('@') and not re.match(FEDIVERSE_REGEX, value):
+            value = f'https://twitter.com/{value.replace("@", "")}'
+        return value
+
+class InstagramValidator(fev.FancyValidator):
+    def _to_python(self, value, state):
+        if value.startswith('@') and not re.match(FEDIVERSE_REGEX, value):
+            value = f'https://instagram.com/{value.replace("@", "")}'
+        return value
+
+class FacebookValidator(fev.FancyValidator):
+    def _to_python(self, value, state):
+        if value.startswith('@') and not re.match(FEDIVERSE_REGEX, value):
+            value = f'https://facebook.com/{value.replace("@", "")}'
+        return value
+
+class FediverseValidator(fev.FancyValidator):
+    def _to_python(self, value, state):
+        if value.startswith('http'):
+            url = urlsplit(value)
+            value = f'{url.path.replace("/", "")}@{url.netloc}'
+            if not re.match(FEDIVERSE_REGEX, value):
+                raise fe.Invalid('Invalid Mastodon address', value, state)
+        elif not re.match(FEDIVERSE_REGEX , value):
+            raise fe.Invalid('Invalid Mastodon address', value, state)
         return value