Ejemplo n.º 1
0
def set_user_profile(request):
    try:
        user_profile = UserProfile.objects.get(user_id=request.user.id)
    except MultipleObjectsReturned:
        return JsonResponse(CODE_MSG['multiple_objects_returned'])
    except ObjectDoesNotExist:
        return JsonResponse(CODE_MSG['object_does_not_exist'])
    except Error:
        return JsonResponse(CODE_MSG['database_error'])
    #user = User.objects.get(username=request.user.username)
    """
    if user.has_perm('change_user_profile', user_profile):
        print("[+]user has permission")
    """
    if request.method == 'POST':
        # 表单匹配POST中相应的数据,多余的不匹配
        print(request.POST)
        user_profile_form = UserProfileForm(data=request.POST)
        #print(user_profile_form)
        if user_profile_form.is_valid():
            user_profile_data = user_profile_form.cleaned_data
            user_profile.nickname = user_profile_data['nickname']
            user_profile.phone_1 = user_profile_data['phone_1']
            user_profile.phone_2 = user_profile_data['phone_2']
            user_profile.qq = user_profile_data['qq']
            user_profile.wechat = user_profile_data['wechat']
            try:
                user_profile.save()
            except Error:
                return JsonResponse(CODE_MSG['database_error'])
            return JsonResponse(CODE_MSG['success'])
        else:
            return JsonResponse(CODE_MSG['profile_set_failed'])
    else:
        return JsonResponse(CODE_MSG['profile_set_failed'])
Ejemplo n.º 2
0
def edit_general_profile(request):
    if request.method == 'POST':
        user = request.user
        user_details = UserDetails.objects.get(user=user)
        form = UserProfileForm(request.POST, instance=user_details)

        if form.is_valid():
            form.save()
            return get_general_profile_panel(request)
        else:
            return render_to_response('user_profile/general_profile_form.html',
                                      {'form': form},
                                      context_instance=RequestContext(request))
Ejemplo n.º 3
0
def profile(request, username):
    extra_context = {}
    profile = get_object_or_404(UserProfile, user__username__iexact=username)
    extra_context.update({'profile': profile})
    if request.user == profile.user:
        form = UserProfileForm(instance=profile)
        extra_context.update({'form': form})
        if request.method == 'POST':
            form = UserProfileForm(request.POST, request.FILES, instance=profile)
            if form.is_valid():
                form.save()
                return redirect('user_profile', profile)

    return render_to_response('user_profile/profile.html', context_instance=RequestContext(request, extra_context))
Ejemplo n.º 4
0
def create_or_update_user(request: HttpRequest, user_form: UserForm,
                          user_profile_form: UserProfileForm,
                          creating: bool) -> HttpResponse:
    if not user_form.is_valid() or not user_profile_form.is_valid():
        messages.add_message(request, messages.ERROR,
                             user_form.non_field_errors())
    else:
        try:
            if creating:
                user = User.objects.create_user(
                    username=user_form.cleaned_data['username'],
                    email=user_form.cleaned_data['email'],
                    password=user_form.cleaned_data['password'],
                    is_active=True)
            else:
                user = user_form.instance
                user.username = user_form.cleaned_data['username']
                new_pass = user_form.cleaned_data.get('password')
                if new_pass:
                    user.set_password(new_pass)
                else:
                    user.password = user_form.initial['password']
                user.email = user_form.cleaned_data['email']
                user.save()
            user_profile = user_profile_form.save(commit=False)
            user_profile.user_info = user
            user_profile.save()
            if creating:
                sessionlist = Session.objects.all()
                for item in sessionlist:
                    persessionuserlikes = PerSessionUserLikes(
                        user=user_profile, session=item)
                    persessionuserlikes.save()
                messages.add_message(
                    request, messages.SUCCESS,
                    'Congratulations ! Your user is created.')
                t = authenticate(username=user.username,
                                 password=user_form.cleaned_data['password'])
                if t is not None:
                    login(request, t)
            else:
                messages.add_message(request, messages.SUCCESS,
                                     'Your user has been updated.')
            return redirect(reverse('user:profile'))
        except Exception:
            #logger.exception('Error while creating/updating a user. creating=' + str(creating) + ', req=' + "\n".join(request.readlines()))
            messages.add_message(request, messages.ERROR,
                                 'Error, Contact Admin')
    return None
Ejemplo n.º 5
0
def send_update_profile(request):
    profile = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=profile)
        if form.is_valid():
            if 'picture' in request.FILES:
                picture = request.FILES['picture']
                profile.picture = picture
            form.save()

            return redirect('/user/profile/')
    else:
        form = UserProfileForm(instance=profile)

    return redirect('/user/update_profile/')
Ejemplo n.º 6
0
def register(request: HttpRequest) -> HttpResponse:
    if settings.REGISTER_ENABLED:
        if request.method == "POST":
            user_profile_form = UserProfileForm(request.POST, request.FILES)
            user_form = UserForm(request.POST)
            response = create_or_update_user(request, user_form,
                                             user_profile_form, True)
            if response is not None:
                return response
        else:
            user_profile_form = UserProfileForm()
            user_form = UserForm()
    else:
        messages.add_message(
            request, messages.ERROR,
            "You cannot register anymore, please contact an admin.")
    return render(request, 'user_profile/register.html', locals())
Ejemplo n.º 7
0
def get_general_profile_form(request):
    user_details = UserDetails.objects.get(user=request.user)
    form = UserProfileForm(instance=user_details)

    context = {
        'form': form,
        'user': request.user,
        'user_details': user_details
    }
    return render(request, 'user_profile/general_profile_form.html', context)
Ejemplo n.º 8
0
def checkout_success(request, order_number):
    """ successful checkouts """
    save_info = request.session.get('save_info')
    order = get_object_or_404(Order, order_number=order_number)

    if request.user.is_authenticated:
        profile = UserProfile.objects.get(user=request.user)
        # attach profile to order
        order.user_profile = profile
        order.save()

        # save user info
        if save_info:
            profile_data = {
                'default_phone_no': order.phone_no,
                'default_street1': order.street1,
                'default_street2': order.street2,
                'default_town_city': order.town_city,
                'default_county': order.county,
                'default_post_code': order.post_code,
                'default_country': order.country
            }
            user_profile_form = UserProfileForm(profile_data, instance=profile)
            if user_profile_form.is_valid():
                user_profile_form.save()

    messages.success(request, f'Order successfully processed! \
        Your order number is {order_number}. A conformation \
        email will be sent to {order.email}.')

    if 'bag' in request.session:
        del request.session['bag']

    template = 'checkout/checkout_success.html'
    context = {
        'order': order
    }

    return render(request, template, context)
Ejemplo n.º 9
0
def profile(request: HttpRequest) -> HttpResponse:
    user = request.user
    if not hasattr(user, 'userprofile'):
        messages.add_message(
            request, messages.ERROR,
            'We tried really hard to find it but this user does not exists.')
        return redirect(reverse('user:login'))
    user_profile = user.userprofile

    if request.method == "POST":
        user_form = UserForm(request.POST, instance=user)
        user_profile_form = UserProfileForm(request.POST,
                                            request.FILES,
                                            instance=user_profile)
        response = create_or_update_user(request, user_form, user_profile_form,
                                         False)
        if response is not None:
            return response
    else:
        user_form = UserForm(instance=user)
        user_profile_form = UserProfileForm(instance=user_profile)

    return render(request, 'user_profile/profile.html', locals())
Ejemplo n.º 10
0
def update_profile(request):
    userProfile = UserProfile.objects.get(user=request.user)
    form = UserProfileForm(initial={
                                    'user': userProfile.user,
                                    'bio': userProfile.bio,
                                    'studentID': userProfile.studentID,
                                    'picture': userProfile.picture,
                                    'career': userProfile.career,
                                    'university': userProfile.university,
                                    'subjects': userProfile.subjects.all,
                                    'video': userProfile.video,
                                    'begin_time': userProfile.begin_time,
                                    'end_time': userProfile.end_time})
    return render(request, 'user_profile/update_profile.html', {'form': form, 'userProfile': userProfile})
Ejemplo n.º 11
0
def send_update_profile(request):
    profile = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=profile)
        if form.is_valid():
            if 'picture' in request.FILES:
                picture = request.FILES['picture']
                profile.picture = picture
            form.save()

            return redirect('/user/profile/')
    else:
        form = UserProfileForm(instance=profile)

    return redirect('/user/update_profile/')
Ejemplo n.º 12
0
def user_profile_dashboard(request):

    current_user = request.user
    current_user_instance = UserProfile.objects.get(user=current_user)
    if request.method == "POST":
        form = UserProfileForm(request.POST,
                               instance=current_user_instance,
                               user=current_user)
        if form.is_valid():
            form.save()
    else:
        form = UserProfileForm(user=current_user)
    return render(request, 'user_auth/login_signup.html', {
        'form': form,
        'user': current_user,
    })
Ejemplo n.º 13
0
def edit_user_profile(request):

    view_title = 'Edit Profile'
    form_title = 'Submit'

    current_user = request.user
    current_user_instance = UserProfile.objects.get(user=current_user)
    if request.method == "POST":
        form = UserProfileForm(request.POST,
                               instance=current_user_instance,
                               user=current_user)
        if form.is_valid():
            form.save()
            return redirect('../get_started')
    else:
        form = UserProfileForm(user=current_user)
    return render(
        request, 'user_profile/edit_user_profile.html', {
            'form': form,
            'view_title': view_title,
            'current_user': current_user,
            'form_title': form_title,
            'user': current_user,
        })
Ejemplo n.º 14
0
def checkout_success(request, order_number):
    """
    Handle successful checkouts
    """
    save_user_info = request.session.get('save_user_info')
    order = get_object_or_404(ProductOrder, order_number=order_number)

    if request.user.is_authenticated:
        profile = UserProfile.objects.get(user=request.user)
        # Attach the user's profile to the order
        order.user_profile = profile
        order.save()

        current_bag = bag_products(request)
        bag_items = current_bag['bag_items']
        user_has_plan = False
        for item in bag_items:
            cat = item['category']
            if str(cat) == 'nutrition_plan':
                user_has_plan = True
                profile = UserProfile.objects.get(user=request.user)
                form_database_sub = {
                    'has_plan': user_has_plan,
                }

                order_form_ = UserProfileForm(form_database_sub,
                                              instance=profile)
                if order_form_.is_valid():
                    if order_form_.is_valid():
                        order_form_.save()

        # Save the user's info
        if save_user_info:
            user_profile_data = {
                'default_phone_number': order.phone_number,
                'default_country': order.country,
                'default_postcode': order.postcode,
                'default_town_or_city': order.town_or_city,
                'default_street_address1': order.street_address1,
                'default_street_address2': order.street_address2,
                'default_county': order.county,
            }
            user_profile_form = UserProfileForm(user_profile_data,
                                                instance=profile)
            if user_profile_form.is_valid():
                user_profile_form.save()

    messages.success(request, f'Your order was successfully processed! \
        The order number is {order_number}. A confirmation \
        email will be sent to {order.email} shortly.')

    if 'shop_bag' in request.session:
        del request.session['shop_bag']

    template = 'checkout/checkout_success.html'
    context = {
        'order': order,
        'from_user_profile': True,
    }

    return render(request, template, context)
Ejemplo n.º 15
0
def update_profile(request):
    userProfile = userProfile.object.get(user=request.user)
    form = UserProfileForm()
    return render_to_response('user_profile/update_profile.html',
                              {'form': form}, RequestContext(request))