Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. class CreatePostForm(forms.ModelForm):
  2.     def __init__(self, *args, **kwargs):
  3.         self.user = kwargs.pop('user')  # get user from kwargs, but don't pass that kwarg to super
  4.         super().__init__(*args, **kwargs)
  5.  
  6.     class Meta:
  7.         model = Post
  8.         fields = ['title', 'url', 'postpic', 'postvideo', 'day', 'alt_text', 'is_private', 'anything_else']
  9.         exclude = ('day',)
  10.  
  11.     def clean(self):
  12.         current_user = self.user  # from init
  13.         # if Post.objects.filter(author=current_user, date_posted=timezone.now().today()).exists():
  14.         print('timezone.now().date()='.format(timezone.now().date()))
  15.         print('current_user={}'.format(current_user))
  16.         # print(Post.objects.filter(author=current_user, date_posted=timezone.now().date()))
  17.  
  18.         # prevent posting if user has already posted today
  19.         if Post.objects.filter(author=current_user, day__date_posted__date=timezone.now().date()).exists():
  20.             # messages.error(current_user, 'You already submitted something today!')
  21.             print('User {} was forbidden from posting again today'.format(self.user))
  22.             raise forms.ValidationError("You already submitted something today")
  23.         else:
  24.             print('This is the users first submission ofthe day 1!!!!')
  25.  
  26.         # prevent posting if the user is blocked
  27.         current_user_profile = UserProfile.objects.get(user=current_user)
  28.         if current_user_profile.blocked:
  29.             print('User us blocked!!!!!!!!!!!!!!!!!!!')
  30.             raise forms.ValidationError("Sorry, you are not allowed to submit anymore.")
  31.         else:
  32.             print('USer is not blocked')
  33.  
  34.         # if the submission is a soundcloud/vimeo/youtube URL:
  35.         if self.data.get('postvideo'):
  36.             print('This is a video post!!!!!')
  37.  
  38.             # remember old state
  39.             _mutable = self.data._mutable
  40.             self.data._mutable = True
  41.  
  42.             # pull out url value
  43.             url = self.data.get('postvideo')
  44.  
  45.             # strip mobile "m" part if necessary
  46.             cleaned_url = mobile_to_regular_url(url)
  47.  
  48.             # сhange the value
  49.             self.data['postvideo'] = cleaned_url
  50.             print('Cleaned mobile URL to {}'.format(cleaned_url))
  51.             print('Now the data value is: {}'.format(self.data.get('postvideo')))
  52.  
  53.             # set mutable flag back
  54.             self.data._mutable = _mutable
  55.  
  56.         super().clean()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement