Ejemplo n.º 1
0
def register_user(request, role):
    if request.method == 'GET':
        context = {
            'form': RegisterForm(),
            'user_register_form': UserProfileForm(),
            'role': role
        }
        return render(request, 'auth/register.html', context)
    else:
        register_form = RegisterForm(request.POST)

        if register_form.is_valid():
            user = register_form.save()
            user_group = Group.objects.get(name=role)
            user_group.user_set.add(user)
            form = UserProfileForm(request.POST, request.FILES)
            if form.is_valid():
               up = form.save(commit=False)
               up.user_id = user.id
               up.save()
            else:
                context = {
                    'form': register_form,
                    'role': role
                }
                return render(request, 'auth/register.html', context)
            login(request, user)

            return redirect('home')

        context = {
            'form': register_form,
            'role': role
        }
        return render(request, 'auth/register.html', context)
Ejemplo n.º 2
0
def user_reg(request):
     # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            registered = True

        else:
            print user_form.errors, profile_form.errors

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render(request,
            'Gigstop/user_registration.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
Ejemplo n.º 3
0
def register(request):
	context = RequestContext(request)
	
	registered = False
	
	if request.method == 'POST':
		user_form = UserForm(data=request.POST)
		profile_form = UserProfileForm(request.POST, request.FILES)
		
		if user_form.is_valid() and profile_form.is_valid():
			user = user_form.save()
			
			user.set_password(user.password)
			user.save()
			
			profile = profile_form.save(commit=False)
			profile.user = user
			
			if profile_form.is_multipart():
				picture = save_files(request.FILES['picture'])
				
			profile.save()
			
			registered = True
			
		else:
			print user_form.errors, profile_form.errors
	
	else:
		user_form =UserForm()
		profile_form = UserProfileForm()
	
	return render_to_response('app/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
Ejemplo n.º 4
0
def register(request):
    context = RequestContext(request)
    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            profile.save()
            registered = True
            new_user = authenticate(username=request.POST['username'], password=request.POST['password'])
            login(request, new_user)
            return HttpResponseRedirect('/user/')
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return JsonResponse({'errors': {'profile': profile_form.errors, 'user': user_form.errors}}, status=422);
Ejemplo n.º 5
0
def register(request):
    """ View for register
    returns register.html template
    """
    if not request.user.is_authenticated():
        context_dict={}
        if request.method == 'POST':
            user_form = UserRegisterForm(data=request.POST)
            profile_form = UserProfileForm(data=request.POST)
            if user_form.is_valid() and profile_form.is_valid():
                user = user_form.save(commit=False)
                user.set_password(user.password)
                user.is_staff=True
                profile = profile_form.save(commit=False)
                user.save()
                profile.user = user
                profile.save()
                return HttpResponseRedirect(settings.REGISTER_REDIRECT_UTL)
            else:
                print user_form.errors
                print profile_form.errors
                context_dict["error1"] = user_form.errors
                context_dict["error2"] = user_form.errors
        else:
            user_form = UserRegisterForm()
            profile_form = UserProfileForm()
            context_dict["user_form"]=user_form
            context_dict["profile_form"]=profile_form
        return render(request,
            'app/register.html',context_dict
            )
    else :
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
Ejemplo n.º 6
0
def register(request):
    # Like before, get the request's context.
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance.
            profile.save()

            # Update our variable to tell the template registration was successful.
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print user_form.errors, profile_form.errors

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    # Render the template depending on the context.
    return render_to_response(
            'app/register.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
            context)
Ejemplo n.º 7
0
def profile_post(request):
    request.session['tab'] = 'tab1'
    profile = request.user.profile
    form = UserProfileForm(request.POST, instance=profile)
    if form.is_valid():
        user_profile = form.save()
        user_profile.save()
    return redirect(to='profile')
Ejemplo n.º 8
0
def userprofile(request):
    if request.method == "POST":
        form = UserProfileForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/image')
    form = UserProfileForm()
    return render(request, 'form_1.html', {'form': form})
Ejemplo n.º 9
0
def register(request):
    context = RequestContext(request)

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

            # Now we save the UserProfile model instance.
            profile.save()

            # Update our variable to tell the template registration was successful.
            registered = True

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            pass

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    # Render the template depending on the context.
    return render(
        'app/user/register_form.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        }, context)
Ejemplo n.º 10
0
def update_user_picture(request):
    if request.method != 'POST':
        return HttpResponseBadRequest({'error': 'Only POST is accepted'})

    user = get_object_or_404(UserWithProfile, user=request.user.id)
    profile_form = UserProfileForm(data=request.POST)

    if profile_form.is_valid():
        profile = profile_form.save(commit=False)
        profile.user = user.user
        if 'picture' in request.FILES:
            profile.picture = request.FILES['picture']
        profile.save()
        return JsonResponse({'message': 'Profile picture updated'})
    else:
        return HttpResponseBadRequest({'error': 'No picture received from POST files'})
Ejemplo n.º 11
0
def register(request):
    """ Handles user registration logic

    Arguments:
        request -- [standard Django request arg]

    Returns:
        Render - render registration page with appropriate info
    """
    registered = False
    if request.method == "POST":
        user_form = UserForm(request.POST)
        profile_form = UserProfileForm(request.POST, request.FILES)
        if (user_form.is_valid() and profile_form.is_valid()):
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            # check for picture
            if ("picture" in request.FILES):
                profile.picture = request.FILES["picture"]
            profile.save()
            # create wish list for new user
            wish_list = ProductList.objects.create(name=user.username,
                                                   user=profile)
            logger.info("User: %s is registered", user)
            registered = True
        else:
            print(user_form.errors, profile_form.errors)
            logger.info(
                "User: failed to register with user form errors: %s \n \
                        and profile form errors: %s", user_form.errors,
                profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
        logger.info("Rendered registration page")

    return render(request,
                  "app/register.html",
                  context={
                      "user_form": user_form,
                      "profile_form": profile_form,
                      "registered": registered
                  })
Ejemplo n.º 12
0
def profile(request):
    form_errors = None
    try:
        user_profile = request.user.userprofile
    except ObjectDoesNotExist:
        # While not strictly necessary, if there was a user created before
        # introducing a user profile, we will need to create the object.
        user_profile = UserProfile.objects.create(user=request.user)
        user_profile.save()

    """ Process the post request for form """
    if request.method == 'POST':
        form = UserProfileForm(request.POST,instance=user_profile)
        if form.is_valid(): # All validation rules pass
            form.save()
        else:
            # Transform field name to label name and put in tuple with errors
            form_errors = []
            for i in form.errors:
                try:
                    label = form.fields[i].label if form.fields[i].label is not None else i
                    form_errors.append((label, form.errors[i]))
                except:
                    form_errors.append(('Error', form.errors[i]))

    """Renders the profile page."""
    assert isinstance(request, HttpRequest)
    user_profile_form = UserProfileForm(instance=user_profile)
    return render(
        request,
        'registration/profile.html',
        context_instance = RequestContext(request,
        {
            'title':'Profile',
            'message':'User Profile Information',
            'year':datetime.now().year,
            'user_profile': user_profile,
            'form':user_profile_form,
            'form_errors':form_errors,
        })
    )
Ejemplo n.º 13
0
def profile(request):
    form_errors = None
    try:
        user_profile = request.user.userprofile
    except ObjectDoesNotExist:
        # While not strictly necessary, if there was a user created before
        # introducing a user profile, we will need to create the object.
        user_profile = UserProfile.objects.create(user=request.user)
        user_profile.save()

    """ Process the post request for form """
    if request.method == 'POST':
        form = UserProfileForm(request.POST,instance=user_profile)
        if form.is_valid(): # All validation rules pass
            form.save()
        else:
            # Transform field name to label name and put in tuple with errors
            form_errors = []
            for i in form.errors:
                try:
                    label = form.fields[i].label if form.fields[i].label is not None else i
                    form_errors.append((label, form.errors[i]))
                except:
                    form_errors.append(('Error', form.errors[i]))

    """Renders the profile page."""
    assert isinstance(request, HttpRequest)
    user_profile_form = UserProfileForm(instance=user_profile)
    return render(
        request,
        'registration/profile.html',
        context_instance = RequestContext(request,
        {
            'title':'Profile',
            'message':'User Profile Information',
            'year':datetime.now().year,
            'user_profile': user_profile,
            'form':user_profile_form,
            'form_errors':form_errors,
        })
    )
Ejemplo n.º 14
0
def add_user(request):
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            try:
                user.set_password(user.password)
                user.save()
                print(user)
                print(type(user.id))
                user_id = user.id
                profile = profile_form.save(commit=False)
                profile.user = user

                profile.save()
                registered = True

                try:
                    print('Sending')
                    request_user = AddUser(str(user_id))
                    client.send(request_user)
                    print('Sent')

                except:
                    print('Not sent')

                return redirect(reverse('login'))

            except:
                return redirect(reverse('login'))

        else:
            print(user_form.errors, profile_form.errors)
            return redirect(reverse('register'))
    else:
        return redirect(reverse('login'))
Ejemplo n.º 15
0
def register(request):
    registered=False
    user_form=UserForm(data=request.POST)
    profile_form=UserProfileForm(data=request.POST)
    
    if request.method=='POST':
        if user_form.is_valid() and profile_form.is_valid():
            user=user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save()
            profile.user = user
            profile.save()

            registered=True
    
        else:
            print (user_form.errors, profile_form.errors)

    return render(request,
            'app/register.html',
            context_instance=RequestContext(request,{'user_form': user_form, 'profile_form': profile_form, 'registered': registered} ))
Ejemplo n.º 16
0
def register(request):
    registered = False

    if request.method == 'POST':

        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            profile.save()

            registered = True

        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    context = {
        'user_form': user_form,
        'profile_form': profile_form,
        'registered': registered
    }
    return render(request, 'app/register.html', context)
Ejemplo n.º 17
0
def register(request):
	registered = False
	if request.method == 'POST':
		user_form = UserForm(request.POST)
		profile_form = UserProfileForm(request.POST)
		if user_form.is_valid() and profile_form.is_valid():
			user = user_form.save()
			user.set_password(user.password)
			user.save()
			profile = profile_form.save(commit = False)
			profile.user = user

			if 'photo' in request.FILES:
				profile.photo = request.FILES['photo']

			profile.save()
			registered = True
		else:
			print user_form.errors, profile_form.errors
	else:
		user_form = UserForm()
		profile_form = UserProfileForm()
	dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}
	return render(request, 'blog/register.htm', dict)
Ejemplo n.º 18
0
def index(request):
    ''' If logged in, direct to logged in user's profile page. Otherwise this is a login and registration form.
    '''
    
    # Redirect to user page if already logged in
    if request.user.is_authenticated():
        user = UserProfile.objects.get(user_id=request.user.id)
        return HttpResponseRedirect('/user/%d' % user.id)
    
    # Otherwise this is a registration page    
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            login(request, user)
            user = UserProfile.objects.get(user_id=user.id)
            return HttpResponseRedirect('/user/%d' % user.id)
        else:
            print user_form.errors, profile_form.errors  
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    
    context = {'user_form': user_form, 'profile_form': profile_form}
    return render(request, 'app/index.html', context)    
Ejemplo n.º 19
0
def profile(request):
    """User profile page"""
    context = get_global_context(request)

    if request.method == 'POST':
        user = request.user
        if user.has_perm('app.employee_role'):
            employee = Employee.objects.get(user_id=user.id)
            employee_form = EmployeeProfileForm(request.POST,
                                                request.FILES,
                                                instance=employee)
            user_form = UserProfileForm(request.POST, instance=request.user)

            if user_form.is_valid() and employee_form.is_valid():
                if user_form.has_changed():
                    user.username = user_form.cleaned_data['username']
                    user.email = user_form.cleaned_data['email']
                    user.first_name = user_form.cleaned_data['first_name']
                    user.last_name = user_form.cleaned_data['last_name']
                    user.save()

                if employee_form.has_changed():
                    # saving data
                    if employee_form.cleaned_data['avatar'] and\
                            employee.avatar !=\
                                    employee_form.cleaned_data['avatar']:
                        cur_avatar = employee.avatar
                        employee.avatar = employee_form.cleaned_data['avatar']
                        delete_current_avatar_file(cur_avatar.path)
                    employee.save()
            else:
                user_data = dict((key, user_form.data[key]) for key in list(
                    set(user_form.data) & set(user_form.fields)))
                current_data_user = User(**user_data)

                employee_data = dict(
                    (key, employee_form.data[key]) for key in list(
                        set(employee_form.data) & set(employee_form.fields))
                    if key != 'avatar')
                current_data_employee = Employee(**employee_data)
                current_data_employee.avatar = employee.avatar

                context.update({
                    'title': employee.get_short_instance_name(),
                    'cur_user': current_data_user,
                    'employee': current_data_employee,
                    'employee_form': employee_form,
                    'user_form': user_form,
                })

            return render_profile(request, context)

        elif user.has_perm('app.client_role'):
            client = Client.objects.get(user_id=request.user.id)
            client_form = ClientProfileForm(request.POST,
                                            request.FILES,
                                            instance=client)
            user_form = UserProfileForm(request.POST, instance=request.user)

            if user_form.is_valid() and client_form.is_valid():
                if user_form.has_changed():
                    user.username = user_form.cleaned_data['username']
                    user.email = user_form.cleaned_data['email']
                    user.first_name = user_form.cleaned_data['first_name']
                    user.last_name = user_form.cleaned_data['last_name']
                    user.save()

                if client_form.has_changed():
                    # saving data
                    client = Client.objects.get(user_id=user.id)
                    client.name = client_form.cleaned_data['name']
                    client.full_name = client_form.cleaned_data['full_name']
                    client.code_ipn = client_form.cleaned_data['code_ipn']
                    client.address = client_form.cleaned_data['address']
                    client.phone = client_form.cleaned_data['phone']
                    client.overall_mark_id =\
                        client_form.data['overall_mark']
                    if client_form.cleaned_data['avatar'] and\
                            client.avatar != client_form.cleaned_data['avatar']:
                        cur_avatar = client.avatar
                        client.avatar = client_form.cleaned_data['avatar']
                        delete_current_avatar_file(cur_avatar.path)
                    client.save()
            else:
                user_data = dict((key, user_form.data[key]) for key in list(
                    set(user_form.data) & set(user_form.fields)))
                current_data_user = User(**user_data)

                client_data = dict(
                    (key, client_form.data[key]) for key in list(
                        set(client_form.data) & set(client_form.fields))
                    if key != 'avatar')
                client_data.update({
                    'overall_mark':
                    Mark.objects.get(id=client_form.data['overall_mark']),
                })
                current_data_client = Client(**client_data)
                current_data_client.balance = client.balance
                current_data_client.avatar = client.avatar

                context.update({
                    'title': client.get_title_name(),
                    'cur_user': current_data_user,
                    'client': current_data_client,
                    'client_form': client_form,
                    'user_form': user_form,
                })

            return render_profile(request, context)

    else:
        return render_profile(request, context)
Ejemplo n.º 20
0
def register(request):
    # A boolean value for telling the template
    # whether the registration was successful.
    # Set to False initially. Code changes value to
    # True when registration succeeds.
    registered = False
    # If it's a HTTP POST, we're interested in processing form data.
    print("registration request received")
    if request.method == 'POST':

        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        # If the two forms are valid...
        print(user_form)
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()
            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()
            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves,
            # we set commit=False. This delays saving the model
            # until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.name = user.username
            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and
            #put it in the UserProfile model.
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
            # Now we save the UserProfile model instance.
            profile.save()
            password = request.POST.get('password', None)
            authenticated = authenticate(username=user.username,
                                         password=password)
            if authenticated:
                login(request, authenticated)
            # Update our variable to indicate that the template
            # registration was successful.
            registered = True
        else:
            # Invalid form or forms - mistakes or something else?
            # Print problems to the terminal.
            print(user_form.errors, profile_form.errors)
    else:
        # Not a HTTP POST, so we render our form using two ModelForm instances.
        # These forms will be blank, ready for user input.
        user_form = UserForm()
        profile_form = UserProfileForm()
        # Render the template depending on the context.
    return render(
        request, 'ClassMateZ/register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })