Esempio n. 1
0
def update_profile(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)
        # image_form=ImageUploadForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid() and image_form.is_valid():
            user_form.save()
            profile_form.save()
            # m = ExampleModel.objects.get(pk=course_id)
            # m.model_pic = image_form.cleaned_data['image']
            # m.save()
#            messages.success(request, _('Your profile was successfully updated!'))
            return redirect('home')
#        else:
#             messages.error(request, _('Please correct the error below.'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
        # image_form=ImageUploadForm()
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form,
        # 'image_form': image_form
    })

# def upload_pic(request):
#     if request.method == 'POST':
#         form = ImageUploadForm(request.POST, request.FILES)
#         if form.is_valid():
#             m = ExampleModel.objects.get(pk=course_id)
#             m.model_pic = form.cleaned_data['image']
#             m.save()
#             return HttpResponse('image upload success')
#     return HttpResponseForbidden('allowed only via POST')
def index(request):
    if Profile.objects.first():
        budget = Profile.objects.first().budget
        expenses = Expenses.objects.all()
        total_expenses = 0
        prices = []
        for entry in expenses:
            total_expenses += entry.price
            prices.append(f'{entry.price:.2f}')
        context = {
            'profiles': Profile.objects.all(),
            'expenses': expenses,
            'budget': f'{budget:.2f}',
            'left': f'{(budget - total_expenses):.2f}',
            'prices': prices,
        }
        return render(request, 'home-with-profile.html', context)

    if request.method == "GET":
        form = ProfileForm()
        return render(request, 'home-no-profile.html', {'form': form})
    else:
        form = ProfileForm(request.POST)
        if form.is_valid():
            # new_profile = form.save()
            # new_profile.save()
            form.save()
            return redirect('index')
        return render(request, 'home-no-profile.html', {'form': form})
Esempio n. 3
0
def request_post_method_processing(request, args):
    post = copy.deepcopy(request.POST)
    if 'username' in post:
        post['username'] = post['username'].lower()
    user_form = UserCreationForm(post)
    profile_form = ProfileForm(post)
    email_form = UserChangeEmailForm(post)
    if user_form.is_valid() and profile_form.is_valid(
    ) and email_form.is_valid():
        user = user_form.save(commit=False)
        profile = profile_form.save(commit=False)
        profile.user = user
        user.email = request.POST.get('email', '')
        user.save()
        profile.save()

        birth_date = datetime.datetime.strptime(post['birthday'],
                                                '%d.%m.%Y').date()
        birthday = BirthDate()
        birthday.birthday = birth_date
        birthday.profile = profile
        birthday.save()

        profile_photo = ProfilePhoto()
        profile_photo.profile = profile
        profile_photo.photo = 'images/photo.svg'
        profile_photo.save()

        # Убрать, если не нужна автоматическая авторизация после регистрации пользователя
        auth.login(request, user)
        return redirect('/')
    else:
        args['user_form'] = user_form
        args['profile_form'] = profile_form
        args['email_form'] = email_form
Esempio n. 4
0
def manage_profile(request):
    profile, created = Profile.objects.get_or_create(user=request.user)
    form = ProfileForm(request.POST or None, instance=profile)
    if form.is_valid():
        form.instance.save()
        return redirect(reverse('dashboard'))
    return render(request, 'main/profile/manage_profile.html', {'form': form})
Esempio n. 5
0
def signup(request, template="main/register.html"):
    """
    Signup form.
    """
    """
    Login form.
    """
    login_form = LoginForm(prefix="login")
    signup_form = ProfileForm()
    if request.method == "POST":
        login_form = LoginForm(request.POST, prefix="login")
        signup_form = ProfileForm(request.POST)
        if not login_form.has_changed() and not request.POST.get(
                "from_popup", False):
            login_form = LoginForm(prefix="login")
        if not signup_form.has_changed(): signup_form = ProfileForm()

        if login_form.is_valid():
            authenticated_user = login_form.save()
            messages.info(request, "Successfully logged in")
            auth_login(request, authenticated_user)

            return redirect('/')

        if signup_form.has_changed() and signup_form.is_valid():
            #import pdb;pdb.set_trace()
            new_user = signup_form.save()
            messages.info(request, "Successfully signed up")
            auth_login(request, new_user)
            return redirect("/")

    context = {"login_form": login_form, "signup_form": signup_form}
    return render(request, template, context)
Esempio n. 6
0
File: views.py Progetto: armenav/a2b
def signup(request, template="main/register.html"):
    """
    Signup form.
    """
    """
    Login form.
    """
    login_form = LoginForm(prefix="login")
    #DriverImageFormSet = formset_factory(CarImageForm, max_num=6)
    signup_form = ProfileForm()
    #formset = DriverImageFormSet
    if request.method == "POST":
        #import pdb;pdb.set_trace()

        login_form = LoginForm(request.POST, prefix="login")
        signup_form = ProfileForm(request.POST, request.FILES)
        #import pdb;pdb.set_trace()
        

        if not login_form.has_changed() and not request.POST.get("from_popup",False): login_form = LoginForm(prefix="login")
        if not signup_form.has_changed(): signup_form = ProfileForm()
        
        if login_form.is_valid():
            authenticated_user = login_form.save()
            messages.info(request, _("Successfully logged in"))
            auth_login(request, authenticated_user)
            
            return redirect('/')

        if signup_form.has_changed() and signup_form.is_valid():
            #formset = DriverImageFormSet(request.POST, request.FILES)
            new_user = signup_form.save()
            Driver(user=new_user).save()
            
            #import pdb;pdb.set_trace()
            #
            #data = signup_form.cleaned_data
            #import pdb;pdb.set_trace()
#            f_img = Image.objects.create(image=request.FILES.get('featured_image'))
#            driver = Driver.objects.create(user=new_user, mobile=data.get('mobile',None), featured_image=f_img, sex=data.get('gender'))#set mobile and featured image
            
            
#            if formset.is_valid():
#                for form in formset: 
#                    cd = form.cleaned_data
#                    if cd:
#                        dci = DriverImage(driver=driver, image=cd.get('image'))
#                        dci.save()

            
            messages.info(request, _("Successfully signed up!"))
            auth_login(request, new_user)
            return redirect("/")
    #import pdb;pdb.set_trace()
    context = {"login_form": login_form, "signup_form": signup_form}
    return render(request, template, context)
Esempio n. 7
0
 def post(self, request):
     form = ProfileForm(request.POST, request.FILES, instance=request.user)
     if 'save' in request.POST:
         if form.is_valid():
             form.save()
             username = form.cleaned_data.get('username')
             request.user.username = username
             request.user.bio = form.cleaned_data.get('bio')
             request.user.profile_img = form.cleaned_data.get('profile_img')
             request.user.save()
         return redirect('account')
Esempio n. 8
0
def profile_settings(request):
    if request.method == "POST":
        profileform = ProfileForm(request.POST, instance=request.user.profile)
        if profileform.is_valid():
            profileform.save()
            messages.success(request, _("Profile settings successfully updated"))
            return redirect("profile_settings")
        messages.error(request, _("Please fix these errors and resubmit the form"))
    else:
        profileform = ProfileForm(instance=request.user.profile)
    return render_to_response("user/profile_settings.jinja", locals())
def edit_profile(request):
    user = Profile.objects.first()
    if request.method == "GET":
        form = ProfileForm(instance=user)
        return render(request, 'profile-edit.html', {'form': form})
    else:
        form = ProfileForm(request.POST, instance=user)
        if form.is_valid():
            # user = form.save()
            # user.save()
            form.save()
            return redirect('index')
        return render(request, 'profile-edit.html', {'form': form})
Esempio n. 10
0
File: views.py Progetto: armenav/a2b
def profile(request, template='main/account/profile.html'):
    
    instance = request.user.driver.car if hasattr(request.user.driver,'car') else None
    carform = CarForm(prefix='car', user=request.user, instance=instance)
    if request.method == "POST":
        
        userform   = ProfileForm(request.POST, prefix='user', instance=request.user)
        if userform.is_valid():
            user = userform.save()

        messages.info(request, _("Your personal page has been updated!"))
    else:
        userform   = ProfileForm(prefix='user', instance=request.user)
    context = {"userform": userform, 'carform': carform}
    return render(request, template, context)
Esempio n. 11
0
def settings(request):
    user = request.user
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            user.first_name = form.cleaned_data.get('first_name')
            user.last_name = form.cleaned_data.get('last_name')
            user.email = form.cleaned_data.get('email')
            user.save()
            messages.add_message(request, messages.SUCCESS,
                                 'Your profile was successfully edited.')

    else:
        form = ProfileForm(instance=user)
    return render(request, 'main/settings.html', {'form': form})
Esempio n. 12
0
def create_profile(request):
    if Profile.objects.filter(user=request.user).exists():
        messages.add_message(request, messages.INFO, 'A Profile already exists for user %s' % request.user.username)
        return HttpResponseRedirect(reverse('admin:index'))

    if request.method == "POST":
        profile_form = ProfileForm(request.POST, request.FILES)
        if profile_form.is_valid():
            profile_form.save(user_id=request.user.pk)
            messages.add_message(request, messages.INFO,
                                 '%s, your Profile has been successfully created.' % request.user.username)
            return HttpResponseRedirect(reverse('admin:index'))
    else:
        profile_form = ProfileForm
        args = {'profile_form': profile_form}
        return render(request, 'profile/createprofile.html', args)
Esempio n. 13
0
def update_profile(request):
    if request.method == 'POST':
        form1 = ProfileForm(request.POST,
                            request.FILES,
                            instance=request.user.profile)
        form2 = UpdateProfile(request.POST, instance=request.user)
        if form1.is_valid() and form2.is_valid():
            form1.save()
            form2.save()
            messages.info(request, 'บันทึกข้อมูลแล้ว')
            return redirect('index')
    else:
        form1 = ProfileForm(instance=request.user.profile)
        form2 = UpdateProfile(instance=request.user)
    context = {'form1': form1, 'form2': form2}
    return render(request, 'profile.html', context=context)
Esempio n. 14
0
def update_user(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, ('Your profile was sucessfull updated!'))
        else:
            messages.error(request, ('Please correct the error(s) below:'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'main/update_user.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })
Esempio n. 15
0
def profile(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST,
                                   request.FILES,
                                   instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return redirect('home')
        else:
            messages.error(request, 'invalid sign up')
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })
Esempio n. 16
0
def update_profile(request):
    """ Update the current profile with a POST request. """

    room_index = request.POST.get('room_index')
    profile_index = request.POST.get('profile_index')

    form = ProfileForm(request.POST, 
        instance=get_object_or_404(Profile, 
            room_index=room_index, profile_index=profile_index))

    if form.is_valid():
        form.save()

    config = Configuration.objects.get()
    profile = Profile.objects.get(room_index=room_index, profile_index=profile_index)
    config.active_profile = profile
    config.save()

    return HttpResponse(json.dumps({}), content_type="application/json")
Esempio n. 17
0
def profile(request):
    courses = Course.objects.filter(is_active=True)
    vcourses = Vcourse.objects.filter(owner=request.user)
    profile = UserProfile.objects.get(pk=request.user.pk)
    if request.method == 'POST':
        form = ProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            form.save()
        return redirect('my_profile')
    else:
        form = ProfileForm(instance=profile)
    lessons = Subscriber2Lesson.objects.filter(user=request.user)
    context = {
        'form': form,
        'profile': profile,
        'lessons': lessons,
        'courses': courses,
        'vcourses': vcourses
    }
    return render_to_response('profile.html', context, RequestContext(request))
Esempio n. 18
0
def signup(request, template="main/register.html"):
    """
    Signup form.
    """
    loginform = LoginForm(prefix="login")
    signup_form = ProfileForm(prefix="signup")
    #import pdb; pdb.set_trace()
    if request.method == "POST":
        signup_form = ProfileForm(request.POST, prefix="signup")
        #
        if signup_form.is_valid():
            new_user = signup_form.save()
            d = Driver(user=new_user)
            d.save()
            
            messages.info(request, _("Successfully signed up!"))
            auth_login(request, new_user)
            return redirect("home")

    context = {"loginform": loginform, "signup_form": signup_form}
    return render(request, template, context)
Esempio n. 19
0
def edit_profile(request, id=None):
    """Edit profile page rendering"""
    form = ProfileForm(request.POST or None,
                       instance=id and Profile.objects.get(id=id))
    # Save edited profile
    if request.method == 'POST':
        valid = form.is_valid()
        if valid:
            form.save()
        if (request.is_ajax()):
            rdict = {'bad': 'false'}
            if not valid:
                rdict.update({'bad': 'true'})
                d = {}
                for e in form.errors.iteritems():
                    d.update({e[0]: unicode(e[1])})
                rdict.update({'errs': d})
            json = simplejson.dumps(rdict, ensure_ascii=False)
            return HttpResponse(json, mimetype='application/javascript')
        else:
            return HttpResponseRedirect(reverse('main.views.index'))
    return render_to_response('edit_profile.html', {'form': form},
                              context_instance=RequestContext(request))
Esempio n. 20
0
def profile(request):
    '''
    Shows profile page
    '''
    edit_profile = False
    edit_user = False
    edit_user_form = EditUserForm(instance=request.user)
    profile_form = ProfileForm(instance=request.user.profile)
    fb_form = FbUsernameForm(instance=request.user.profile)
    # The users has facebook profile?
    try:
        fb_profile = FacebookProfile.objects.get(user=request.user)
        if fb_profile.facebook_id is not None:
            connected_via_facebook = True
        else:
            connected_via_facebook = False
    except FacebookProfile.DoesNotExist:
        connected_via_facebook = False

    if request.method == 'POST':
        # Pull from facebook if user was logged via facebook
        if request.POST.get('use_fb_photo'):
            profile = request.user.profile
            profile.use_fb_photo = True
            profile.use_bethunting_photo = False
            profile.save()
            messages.add_message(request, messages.INFO, 'Your account has been succesfully updated')
        # Pull from facebook if user was logged via bethunting
        elif request.POST.get('fb_username') == '':
            return render(request, "profile.html", {
                'profile_form': profile_form,
                'edit_user_form': edit_user_form,
                'fb_form': fb_form,
                'connected_via_facebook': connected_via_facebook,
            })
        elif request.POST.get('fb_username'):
            profile = request.user.profile
            profile.use_fb_photo = True
            profile.fb_photo = "http://graph.facebook.com/%s/picture?type=large" % request.POST.get('fb_username')
            profile.use_bethunting_photo = False
            profile.save()
            messages.add_message(request, messages.INFO, 'Your account has been succesfully updated')
        else:
            edit_user_form = EditUserForm(request.POST, instance=request.user)
            profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
            if edit_user_form.is_valid():
                user = edit_user_form.save(commit=False)
                user.username = edit_user_form.cleaned_data.get('email')
                user.save()
                edit_user = True

            if profile_form.is_valid():
                profile = profile_form.save()
                edit_profile = True

            if edit_profile or edit_user:
                messages.add_message(request, messages.INFO, 'Your account has been succesfully updated')

    return render(request, "profile.html", {
        'profile_form': profile_form,
        'edit_user_form': edit_user_form,
        'fb_form': fb_form,
        'connected_via_facebook': connected_via_facebook,
    })