views:

15

answers:

0

I have the following problem to solve. In my project users (defined by first name, last name and personal id number ) have the possibility to register accounts. Each registered user (let's call him 'user1') can also add data of up to 4 friends. Those friends then later can join the community themselves, and their profile should disappear from user1 friends list (don't ask me why this must work in this way). Okay so first thing's first : what is the simplest method of getting rid of the username from my registration form ? Will treating e-mail as the username and saving user with username=email_from_form do the job here ?
Now the complicated part with extending registration forms.
This is my forms file:

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        exclude = ('user',)
        fields = (
                    'first_name',
                    'last_name',
                    'pin',  
                    'street',
                    'number',
                    'city',
                    'code', 
               )

class FriendForm(ModelForm):

        class Meta:
            model = Friend
            exclude = ('friend_of',)
            fields = (
                        'first_name',
                        'last_name',
                        'pin',  
                        'street',
                        'number',
                        'city',
                        'code', 
                   )

    class ExtendedRegistrationForm(RegistrationForm):
        first_name = forms.CharField(label="First name", error_messages={'required': 'Please fill the first name field'})
        last_name = forms.CharField(label="Last name", error_messages={'required': 'Please fill the last name field'})
        pin = forms.CharField(label="Pid",)
        street = forms.CharField(label="Street",)
        number = forms.CharField(label="House/flat number",)
        code = forms.CharField("Zip code",)
        city = forms.CharField("City",)
class AddFriendForm(RegistrationForm):
    first_name = forms.CharField(label="First name", error_messages={'required': 'Please fill the first name field'})
    last_name = forms.CharField(label="Last name", error_messages={'required': 'Please fill the last name field'})
    pin = forms.CharField(label="Personal ID number",)
    street = forms.CharField(label="Street",)
    number = forms.CharField(label="House/flat number",)
    code = forms.CharField("Zip code",)
    city = forms.CharField("City",)

So currently ExtendedRegistrationForm and AddFriendForm simply keep fields for my input data. Then there is models file :

class InheritedProfile(models.Model):
    first_name = models.CharField("Name", max_length=50, blank=True, null=True)
    last_name = models.CharField("Last name", max_length=50, blank=True, null=True)
    pin = models.CharField("Personal id number", max_length=15, blank=True, null=True)
    street = models.CharField("Street", max_length=50, blank=True, null=True)
    number = models.CharField("Flat/house number", max_length=10, blank=True, null=True)
    code = models.CharField("Zip ", max_length=6, blank=True, null=True)
    city = models.CharField("City", max_length=50, blank=True, null=True)    

class Friend(InheritedProfile):
    friend_of = models.ForeignKey('UserProfile')

class UserProfile(InheritedProfile):
    def upload_path(self, field_attname):
        filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
        return "uploads/users/%s" % (filename,)

    user = models.OneToOneField(User, blank=True, null=True, unique=True)
    image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)

    class Meta:
        ordering = ['-id']        
        verbose_name = "User profile"
        verbose_name_plural = "Users profiles"
    def __unicode__(self):
        return u"%s %s " % (self.user.first_name, self.user.last_name)

def user_post_save(sender, instance, **kwargs):
    UserProfile.objects.get_or_create(user=instance)

models.signals.post_save.connect(user_post_save, sender=User)

Is this the proper way of inheritance (creating InheritedProfile that does not produce instances but just keeps fields for models and inherit it from UserProfile and Friend models)?
Finally the views - the custom_register view overrides url of default registration view. Then the magic sohuld happen. This is waht I'm doing here :
- load ExtendedRegistrationForm(that inherits from RegistrationForm) as 'form' and the default RegistrationForm's clean_data as form_data.
- in function _create_profile(that will be sent as profile_callback) I first create base_user (auth.User) by sending to 'register' function form_data of basic form.
Then I create extended_user(which is an instance of UserProfile) and from my 'form' load data to its fields.
- finally I save my extended_user

Is this the right approach for the problem ? I haven't yet tested this code since I don't have the templates etc and I'm pretty sure that I made some errors here.

This is my views.py :

def custom_register(request, success_url=None,
           form_class=ExtendedRegistrationForm, profile_callback=None,
           template_name='registration/registration_form.html',
           extra_context=None):

    form = form_class(data=request.POST, files=request.FILES)
    form_data = super(ExtendedRegistrationForm, self).cleaned_data

    def _create_profile(user):
        base_user = backend.register(request, **form_data)
        extended_user = UserProfile(user=user)
        extended_user.is_active = False
        extended_user.first_name = form.cleaned_data['first_name']
        extended_user.last_name = form.cleaned_data['last_name']
        extended_user.pin = form.cleaned_data['pin']
        extended_user.street = form.cleaned_data['street']
        extended_user.number = form.cleaned_data['number']
        extended_user.code = form.cleaned_data['code']
        extended_user.city = form.cleaned_data['city']
        extended_user.save()               

    return register(request, success_url="/user/register/complete",
              form_class=UserRegistrationForm, profile_callback=_create_profile,
              template_name='registration/registration_form.html',
              extra_context=extra_context)

def add_friend(request, success_url=None):
    pass