user created but profile did not create django-registration tastypie using ModelResource

74 views Asked by At

Good day! I'm new here and a beginner in django and I want to create registration of user with instant profile upon successful registration.

resources.py

class AccountResource(ModelResource):
    class Meta:
        proxy=True
        authorization= DjangoAuthorization()
        # authentication = AUTHENTICATION
        queryset = User.objects.all()
        list_allowed_methods = ['get', 'post']
        resource_name = 'account'
       fields = ['username','password','email']

    def prepend_urls(self):
        return [
                url(r"^(?P<resource_name>%s)/register%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('register'), name="api_register"),
            url(r"^(?P<resource_name>%s)/login%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('login'), name="api_login"),
            url(r"^(?P<resource_name>%s)/logout%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('logout'), name="api_logout"),
            url(r"^(?P<resource_name>%s)/activate%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('activate'), name="api_activate"),
        ]

    def activate(self, request, **kwargs):

        data = self.deserialize(request, request.body,format=request.META.get('CONTENT_TYPE', 'application/json'))

        activation_key = data.get('activation_key', '')

        activated_user = RegistrationProfile.objects.activate_user(activation_key)
        if activated_user:
            return self.create_response(request, {
               'success': True,
               'username':  activated_user
            })
        else:
             return self.create_response(request, {
                'success': False,
                'reason': 'already activated or user activation not exist',
            }, HttpUnauthorized )

     def register(self, request, **kwargs):

        data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))

        username = data.get('username', '')
        password = data.get('password', '')
        email = data.get('email', '')

        RegistrationProfile.objects.create_inactive_user(username, email, password,'',send_email=True)
        new_user = authenticate(username=username, password=password)
        if new_user:
            return self.create_response(request, {
                'success': True
            })
        else:
            return self.create_response(request, {
                'success': False,
                'reason': 'incorrect',
                }, HttpUnauthorized )

    def default_group(sender, instance, created, **kwargs):
        if created:
            instance.groups.add(Group.objects.get(name='Member'))
    post_save.connect(default_group, sender=User)

Above code use to register, activate etc. using url POST with raw data. What I what to know if there's a way to create a "Profile" after user have been created using register or after the user have been activated. Any response or concern are mostly appreciated. Thanks in advance.

1

There are 1 answers

0
Jemo Veldad On

OMG I also answer my question. I think it's already answered(duplicate) but I still show for other with same problem as mine.

something/Models.py

from django.db.models.signals import post_save

class Profile(models.Model):
    (blah blah blah) ...

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

my problem earlier is I didn't DROP my DATABASE(because I'm afraid that it cause bigger problem). I don't know but once i do that it's work like a charm(now my problem is I need to re-encode my data XD ).