Thursday, October 20, 2016

Integrating django-registration-redux along with custom_user: Prevent user registration if UserProfile is not valid

I'm trying to use django-registration-redux along with custom_user. I have added both apps to settings.py


My main urls.py is as given below

from userprofile.forms import UserProfileForm  from registration.backends.default.views import RegistrationView  urlpatterns = [  url(r'user/register/$',           RegistrationView.as_view(form_class = UserProfileForm),           name = 'registration_register'),        url(r'^user/', include('registration.backends.default.urls')),  ]  

I have a UserProfile model as given below. (userprofile/models.py)

def validate_dob(value):      now=timezone.now()      start_date=date(now.year-settings.AGE_UPPER, 1, 1)      end_date=date(now.year-settings.AGE_LOWER, 12, 31)      print("start: %s end: %s supplied :%s" % (start_date, end_date, value))      if start_date >value or value > end_date:              raise ValidationError("Please choose a valid date from the given set")   #----------------------------------------  class UserProfile(models.Model):      user=models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,          primary_key=True,)      fullname=models.CharField(max_length=70, blank=False)      dob=models.DateField(blank=False,validators=[validate_dob] )      photo=models.ImageField(upload_to='profile_images', blank=False)      def __str__(self):          return self.user.email      def get_profile(self):          return self.user.id  #---adding signal to save UserProfile  def user_registered_callback(sender, user, request, **kwargs):      profile = UserProfile(user = user)      print(request.POST)      profile.fullname =request.POST["fullname"]      profile.dob ="%s-%s-%s" % (request.POST["dob_year"],request.POST["dob_month"],request.POST["dob_day"])      #profile.dob=request.POST["dob"]      if 'photo' in request.FILES:          profile.photo = request.FILES['photo']        profile.save()    user_registered.connect(user_registered_callback)  

and here's my userprofile/forms.py

class UserProfileForm(RegistrationForm):      fullname=forms.CharField(required=True,label="Full name",  min_length=3, max_length=75)      dob=forms.DateField(required=True,label="Date of birth",           widget=forms.SelectDateWidget(years=range(now.year-settings.AGE_UPPER,now.year-settings.AGE_LOWER)))        photo=forms.ImageField(required=True)`  

How do I prevent inserting User values into the table if the filed, say dob is not in the specified range (AGE_UPPER=70 ,AGE_LOWER=18)


Please tell me if I'm making this over complicated.


Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5df878dc-1bd3-4170-a462-9861d5ae0455%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment