@@ -22,6 +22,7 @@ class MediaMetadataForm(forms.ModelForm):
2222 class Meta :
2323 model = Media
2424 fields = (
25+ "friendly_token" ,
2526 "title" ,
2627 "new_tags" ,
2728 "add_date" ,
@@ -38,18 +39,22 @@ class Meta:
3839 "thumbnail_time" : forms .NumberInput (attrs = {'min' : 0 , 'step' : 0.1 }),
3940 }
4041 labels = {
42+ "friendly_token" : "Slug" ,
4143 "uploaded_poster" : "Poster Image" ,
4244 "thumbnail_time" : "Thumbnail Time (seconds)" ,
4345 }
4446 help_texts = {
4547 "title" : "" ,
48+ "friendly_token" : "Media URL slug" ,
4649 "thumbnail_time" : "Select the time in seconds for the video thumbnail" ,
4750 "uploaded_poster" : "Maximum file size: 5MB" ,
4851 }
4952
5053 def __init__ (self , user , * args , ** kwargs ):
5154 self .user = user
5255 super (MediaMetadataForm , self ).__init__ (* args , ** kwargs )
56+ if not getattr (settings , 'ALLOW_CUSTOM_MEDIA_URLS' , False ):
57+ self .fields .pop ("friendly_token" )
5358 if self .instance .media_type != "video" :
5459 self .fields .pop ("thumbnail_time" )
5560 if self .instance .media_type == "image" :
@@ -74,9 +79,22 @@ def __init__(self, user, *args, **kwargs):
7479
7580 if self .instance .media_type == "video" :
7681 self .helper .layout .append (CustomField ('thumbnail_time' ))
82+ if getattr (settings , 'ALLOW_CUSTOM_MEDIA_URLS' , False ):
83+ self .helper .layout .insert (0 , CustomField ('friendly_token' ))
7784
7885 self .helper .layout .append (FormActions (Submit ('submit' , 'Update Media' , css_class = 'primaryAction' )))
7986
87+ def clean_friendly_token (self ):
88+ token = self .cleaned_data .get ("friendly_token" , "" ).strip ()
89+
90+ if token :
91+ if not all (c .isalnum () or c in "-_" for c in token ):
92+ raise forms .ValidationError ("Slug can only contain alphanumeric characters, underscores, or hyphens." )
93+
94+ if Media .objects .filter (friendly_token = token ).exclude (pk = self .instance .pk ).exists ():
95+ raise forms .ValidationError ("This slug is already in use. Please choose a different one." )
96+ return token
97+
8098 def clean_uploaded_poster (self ):
8199 image = self .cleaned_data .get ("uploaded_poster" , False )
82100 if image :
0 commit comments