Ejemplo n.º 1
0
def profile_action(request, id):

    context = {}
    id = int(id)

    if id != request.user.id:
        followee_profile = Profile.objects.filter(user_id=id).last()
        request_user_profile = Profile.objects.filter(
            user_id=request.user.id).last()

        context['followee_profile'] = followee_profile
        context['user_profile'] = request_user_profile
        try:
            context['followers'] = request_user_profile.followers.all()
        except:
            context['followers'] = None
        return render(request, "socialnetwork/follower.html", context)

    new_profile = Profile.objects.filter(user_id=request.user.id).last()
    form = ProfileForm(request.POST, request.FILES)
    print("----------")
    print(form)
    # request.POST, request.FILES, instance=new_profile)

    if not form.is_valid():
        context['form'] = form
        # temp = Profile.objects.all()
        try:

            context['profile'] = Profile.objects.filter(
                user=request.user).last()
            context['followers'] = new_profile.followers.all()

        except:
            context['followers'] = None

        return render(request, "socialnetwork/profile.html", context)

    else:
        # Must copy content_type into a new model field because the model
        # FileField will not store this in the database.  (The uploaded file
        # is actually a different object than what's return from a DB read.)
        context['form'] = form
        pic = form.cleaned_data['profile_picture']
        print('Uploaded picture: {} (type={})'.format(pic, type(pic)))
        new_profile.profile_picture = pic
        new_profile.content_type = form.cleaned_data[
            'profile_picture'].content_type
        new_profile.bio_text = request.POST['bio_text']
        new_profile.save()
        context['profile'] = new_profile
        context['followers'] = new_profile.followers.all()
    return render(request, "socialnetwork/profile.html", context)
Ejemplo n.º 2
0
def update_profile(request):
    context = {}
    newProfile = Profile.objects.get(user=request.user)
    newForm = ProfileForm(request.POST, request.FILES, instance=newProfile)
    if not newForm.is_valid():
        context['form'] = newForm
    else:
        # Must copy content_type into a new model field because the model
        # FileField will not store this in the database.  (The uploaded file
        # is actually a different object than what's return from a DB read.)
        newProfile.content_type = newForm.cleaned_data[
            'profile_picture'].content_type
        newForm.save()
    context['form'] = newForm
    context['profile'] = newProfile
    return render(request, 'socialnetwork/profile.html', context)
Ejemplo n.º 3
0
def profile(request):
    context = {}
    loginUser = Profile.objects.get(user=request.user)
    context['form'] = ProfileForm(instance=loginUser)
    context['profile'] = loginUser
    followingUser = []
    for i in range(loginUser.follows.count()):
        followingUser.append(loginUser.follows.all()[i].user)
    context['following'] = followingUser

    return render(request, 'socialnetwork/profile.html', context)
Ejemplo n.º 4
0
def profile_logged_in(request):

    item = Profile.objects.select_for_update().get(user=request.user)
    context = {}
    current_user = Profile.objects.get(user=request.user)
    followlist = current_user.follower.all().values()
    follower_ids = [x["id"] for x in followlist]
    all_user = User.objects.all()
    context['users'] = all_user
    context['followlist'] = follower_ids

    if request.method == 'GET':
        # if temp:
        # context = {'pic': temp, 'form': ProfileForm(initial={'bio_input_text': temp.bio_input_text}) }
        context['item'] = item
        context['form'] = ProfileForm(
            initial={'bio_input_text': item.bio_input_text})
        return render(request, 'profile.html', context)

    form = ProfileForm(request.POST, request.FILES)
    if not form.is_valid():
        context = {'item': item, 'form': form}
        return render(request, 'profile.html', context)

    pic = form.cleaned_data['Profile_Picture']
    print('Uploaded picture: {} (type={})'.format(pic, type(pic)))

    item.Profile_Picture = form.cleaned_data['Profile_Picture']
    item.content_type = form.cleaned_data['Profile_Picture'].content_type
    item.bio_input_text = form.cleaned_data['bio_input_text']
    item.save()
    context['item'] = item
    context['form'] = ProfileForm(
        initial={'bio_input_text': item.bio_input_text})

    return render(request, 'profile.html', context)
Ejemplo n.º 5
0
def profile_id(request, username = ""):
    context = dict()

    # attempt to get user object and profile for that username
    u = get_object_or_404(User, username=username)
    p = get_object_or_404(Profile, profile_user = u)
    curr_u_profile = get_object_or_404(Profile, profile_user = request.user)

    # format the page title
    context['page_name'] = "Profile Page for {}".format(u.get_full_name())
    context['other_user'] = p
    context['all_following'] = curr_u_profile.following.all()
    context['has_picture'] = p.profile_picture

    # GET request
    if (request.method == 'GET'):
        if request.user.get_username() == username:
            # profile page is for logged in user
            context['profile_form'] = ProfileForm(instance=p)
        else:
            # profile page is for some other user
            context['other_user'] = p
            if p in curr_u_profile.following.all(): 
                context['follow_label'] = "unfollow"
            else:
                context['follow_label'] = "follow"

        # return redirect(reverse('profile', args=(,)))
        return render(request, 'socialnetwork/profile.html', context)

    # POST method
    if request.user.get_username() == username:
        # user has changed own profile
        profile_form = ProfileForm(request.POST, request.FILES, instance=p)
        context['profile_form'] = profile_form
        if not profile_form.is_valid(): 
            return render(request, 'socialnetwork/profile.html', context)

        picture = profile_form.cleaned_data['profile_picture']
        if picture: p.image_type = picture.content_type
        profile_form.save()
        return render(request, 'socialnetwork/profile.html', context)

    else:
        # user has (un)followed other user
        if p in curr_u_profile.following.all(): 
            curr_u_profile.following.remove(p)
            context['follow_label'] = "follow"
        else:
            curr_u_profile.following.add(p)
            context['follow_label'] = "unfollow"


    return render(request, 'socialnetwork/profile.html', context)
Ejemplo n.º 6
0
def otherprofile(request, username):
    try:

        if request.user.is_authenticated():
            thisuser = User.objects.get(username=username)
            profile = Profile.objects.get(user=thisuser)

            cnt = Follow.objects.filter(iuser=thisuser.username, ouser=request.user.username).count()
            if cnt == 0:
                isfollow = False
            else:
                isfollow = True

            followlist = list(Follow.objects.filter(ouser=thisuser.username))

            if thisuser == request.user:
                profileform = ProfileForm(instance=profile)
                context = {'profile': profile, 'form': profileform, 'userself': True, 'followlist': followlist}
                return render(request, 'socialnetwork/profile.html', context)

            if request.method == 'GET':
                context = {'profile': profile, 'thisuser': thisuser, 'followlist': followlist, 'isfollow': isfollow}
                return render(request, 'socialnetwork/otherprofile.html', context)

            if request.method == 'POST':
                if isfollow:
                    Follow.objects.filter(iuser=thisuser.username, ouser=request.user.username).delete()
                else:

                    newfollow = Follow(iuser=thisuser.username, ouser=request.user.username)
                    newfollow.save()

                isfollow = not isfollow
                followlist = Follow.objects.filter(ouser=thisuser.username).all()
                context = {'profile': profile, 'thisuser': thisuser, 'followlist': followlist,
                           'isfollow': isfollow}

                return render(request, 'socialnetwork/otherprofile.html', context)



    except Profile.DoesNotExist:
        context = {'message': 'Record with id={0} does not exist'.format(id)}
        return render(request, 'socialnetwork/otherprofile.html', context)
Ejemplo n.º 7
0
def update_profile(request, id):
    user_profile = Profile.objects.filter(user_id=request.user.id).last()
    context = {}
    context['profile'] = user_profile
    if id != request.user.id:
        try:
            other = Post.objects.filter(poster_id=id).last().poster
        except:  # This should be a link from comment.
            other = Comment.objects.filter(user_id=id).last().comment_profile.user

        other_profile = Profile.objects.filter(user_id=other.id).last()
        context['others'] = other_profile
        context['current_user'] = user_profile
        return render(request, 'socialnetwork/otherProfile.html', context)

    if request.method == 'GET':
        form = ProfileForm(request.POST, request.FILES)
        context['form'] = form
        return render(request, 'socialnetwork/myProfile.html', context)

    # Must copy content_type into a new model field because the model
    # FileField will not store this in the database.  (The uploaded file
    # is actually a different object than what's return from a DB read.)
    form = ProfileForm(request.POST, request.FILES, instance=user_profile)
    if not form.is_valid():
        context['form'] = form
        return render(request, 'socialnetwork/myProfile.html', context)
    pic = form.cleaned_data['profile_picture']
    # print('Uploaded picture: {} (type={})'.format(pic, type(pic)))
    form.save()
    if user_profile.content_type is None:
        user_profile.content_type = pic.content_type
    user_profile.profile_picture = pic
    user_profile.userBio = request.POST['bio_text']
    # print(user_profile)
    user_profile.save()
    print('Profile #{0} saved.'.format(user_profile.id))
    # context['message'] = 'Item #{0} saved.'.format(new_profile.id)
    context['form'] = ProfileForm()

    return render(request, 'socialnetwork/myProfile.html', context)
Ejemplo n.º 8
0
def profile(request, username):

    try:
        thisuser = User.objects.get(username=username)
        profile = Profile.objects.get(user=thisuser)

        profileform = ProfileForm(instance=profile)
        followlist = list(Follow.objects.filter(ouser=thisuser.username))

        if request.user.is_authenticated() and thisuser.id == request.user.id:
            userself = True

            if request.method == 'GET':
                context = {
                    'profile': profile,
                    'form': profileform,
                    'userself': userself,
                    'followlist': followlist
                }
                return render(request, 'socialnetwork/profile.html', context)

            entry = Profile.objects.select_for_update().get(username=username)
            form = ProfileForm(request.POST, request.FILES, instance=entry)
            if not form.is_valid():
                context = {
                    'profile': entry,
                    'form': form,
                    'userself': userself,
                    'followlist': followlist
                }
                return render(request, 'socialnetwork/profile.html', context)

            entry.content_type = form.cleaned_data['img']
            form.save()

            context = {
                'message': 'Profile updated.',
                'profile': entry,
                'form': form,
                'userself': userself,
                'followlist': followlist,
            }
            return render(request, 'socialnetwork/profile.html', context)

    except Profile.DoesNotExist:
        context = {'message': 'Record with id={0} does not exist'.format(id)}
        return render(request, 'socialnetwork/profile.html', context)