Пример #1
0
    def register(self, request, **cleaned_data):
        """Register a new user, saving the User and UserProfile data."""
        user = User()
        for field in user._meta.fields:
            if field.name in cleaned_data:
                setattr(user, field.name, cleaned_data[field.name])
                # the password has been validated by the form

        user.set_password(cleaned_data["password2"])
        user.save()

        profile = UserProfile(user=user)
        for field in profile._meta.fields:
            if field.name in cleaned_data:
                setattr(profile, field.name, cleaned_data[field.name])

        print "request data: image field"
        img_data = request.POST.get("image")
        # If none or len 0, means illegal image data
        if img_data == None or len(img_data) == 0:
            pass

            # Decode the image data
        img_data = base64.b64decode(img_data)
        filename = "%s.png" % uuid.uuid4()

        # XXX make the upload path a fixed setting in models, since it's
        # reference in two places
        upload_path = "data/avatars/%s/" % user.username
        upload_abs_path = os.path.join(settings.MEDIA_ROOT, upload_path)
        if not os.path.exists(upload_abs_path):
            os.makedirs(upload_abs_path)
        full_file_name = os.path.join(upload_abs_path, filename)

        with open(full_file_name, "wb") as f:
            f.write(img_data)
            f.close()

        profile.image = full_file_name
        profile.save()

        new_user = authenticate(username=user.username, password=cleaned_data["password2"])
        login(request, new_user)
        signals.user_activated.send(sender=self.__class__, user=new_user, request=request)
        return new_user
Пример #2
0
	def register(self, request, **cleaned_data):
		'''Register a new user, saving the User and UserProfile data.'''
		user = User()
		for field in user._meta.fields:
			if field.name in cleaned_data:
				setattr(user, field.name, cleaned_data[field.name])
		# the password has been validated by the form

		user.set_password(cleaned_data['password2'])
		user.save()

		profile = UserProfile(user=user)
		for field in profile._meta.fields:
			if field.name in cleaned_data:
				setattr(profile, field.name, cleaned_data[field.name])

		print "request data: image field"
		img_data = request.POST.get("image")
		# If none or len 0, means illegal image data
		if img_data == None or len(img_data) == 0:
			pass

		# Decode the image data
		img_data = base64.b64decode(img_data)
		filename = "%s.png" % uuid.uuid4()

		# XXX make the upload path a fixed setting in models, since it's
		# reference in three places
		upload_path = "data/avatars/%s/" % user.username
		upload_abs_path = os.path.join(settings.MEDIA_ROOT, upload_path)
		if not os.path.exists(upload_abs_path):
			os.makedirs(upload_abs_path)
		full_file_name = os.path.join(upload_abs_path, filename)

		with open(full_file_name, 'wb') as f:
			f.write(img_data)
			f.close()

		profile.image = full_file_name
		profile.save()

		new_user = authenticate(username=user.username, password=cleaned_data['password2'])
		login(request, new_user)
		signals.user_activated.send(sender=self.__class__, user=new_user, request=request)
		return new_user