Exemplo n.º 1
0
def edit_profile(request):
    profile_to_edit = get_object_or_404(profile, user=request.user)

    if request.method == 'GET':
        form = profileform(instance=profile_to_edit)
        if profile.objects.filter(user=request.user).exists():
            pic = profile.objects.filter(user=request.user)
            context = {'form': form, 'pic': pic}
        else:
            context = {'form': form}
        return render(request, 'editprofile.html', context)

    form = profileform(request.POST, request.FILES, instance=profile_to_edit)
    if not form.is_valid():
        pic = profile.objects.filter(user=request.user)
        if profile.objects.filter(user=request.user).exists():
            pic = profile.objects.filter(user=request.user)
            context = {'form': form, 'pic': pic}
        else:
            context = {'form': form}
        return render(request, 'editprofile.html', context)

    if form.cleaned_data['pic']:
        url = s3_upload(form.cleaned_data['pic'], profile_to_edit.id)
        profile_to_edit.picture = url

    form.save()
    return redirect(reverse('home'))
Exemplo n.º 2
0
def add_post(request, name):

    errors = []
    form = PostForm(request.POST, request.FILES)
    if (form.is_valid()):
        post = Post(text=form.cleaned_data['text'],
                    title=form.cleaned_data['title'])
        student = Student.objects.get(user=request.user)
        classroom = Classroom.objects.get(name=name)
        post.classroom = classroom
        post.student = student
        post.upvotes = 0
        post.save()
        if form.cleaned_data['attachment']:
            url = s3_upload(form.cleaned_data['attachment'], post.id)
            post.attachment_url = url
            if form.cleaned_data['attachment_name']:
                post.attachment_name = form.cleaned_data['attachment_name']
            else:
                post.attachment_name = post.title
            post.save()
        return show_post(request, post.id)
    else:
        print 'FORM NOT VALID'
    return change_class(request, name)
Exemplo n.º 3
0
def add_comment(request, id):

    errors = []
    form = CommentForm(request.POST, request.FILES)
    post = Post.objects.get(id=id)
    student = Student.objects.get(user=request.user)
    #form.cleaned_data["text"]
    form.is_valid()
    new_comment = Comment(text=form.cleaned_data["text"],
                          student=student,
                          upvotes=0)
    new_comment.save()
    if form.cleaned_data['attachment']:
        print "jhdjsgfhjewgfweglfgewqlf"
        url = s3_upload(form.cleaned_data['attachment'], new_comment.id)
        new_comment.attachment_url = url
        if form.cleaned_data['attachment_name']:
            new_comment.attachment_name = form.cleaned_data['attachment_name']
        else:
            new_comment.attachment_name = post.title
        new_comment.save()
    post.comments.add(new_comment)
    post.save()
    class_name = post.classroom

    # Notification function
    notif_text = request.user.get_full_name() + " commented on your post"
    notif_link = '/socialnetwork/show_post/' + str(post.id)
    notify(request, post.student.id, notif_text, notif_link)

    return show_post(request, post.id)
Exemplo n.º 4
0
def add_comment(request, id):

    errors = []
    form = CommentForm(request.POST, request.FILES)
    post = Post.objects.get(id=id)
    student = Student.objects.get(user=request.user)
    #form.cleaned_data["text"]
    form.is_valid()
    new_comment = Comment(text=form.cleaned_data["text"], student=student, upvotes=0)
    new_comment.save()
    if form.cleaned_data['attachment']:
        print "jhdjsgfhjewgfweglfgewqlf"
        url = s3_upload(form.cleaned_data['attachment'], new_comment.id)
        new_comment.attachment_url = url
        if form.cleaned_data['attachment_name']:
            new_comment.attachment_name = form.cleaned_data['attachment_name']
        else:
            new_comment.attachment_name = post.title
        new_comment.save()
    post.comments.add(new_comment)
    post.save()
    class_name = post.classroom

    # Notification function
    notif_text = request.user.get_full_name() + " commented on your post"
    notif_link = '/socialnetwork/show_post/' + str(post.id)
    notify(request, post.student.id, notif_text, notif_link)

    return show_post(request, post.id)
Exemplo n.º 5
0
def edit_profile(request):
    profile_to_edit=get_object_or_404(profile,user=request.user)

    if request.method == 'GET':
        form=profileform(instance=profile_to_edit)
        if profile.objects.filter(user=request.user).exists():
            pic=profile.objects.filter(user=request.user)
            context={'form':form,'pic':pic}
        else:
            context={'form':form}
        return render(request,'editprofile.html',context);
   
    form=profileform(request.POST,request.FILES,instance=profile_to_edit)
    if not form.is_valid():
        pic=profile.objects.filter(user=request.user)
        if profile.objects.filter(user=request.user).exists():
            pic=profile.objects.filter(user=request.user)
            context={'form':form,'pic':pic}
        else:
            context={'form':form}
        return render(request,'editprofile.html',context);

    if form.cleaned_data['pic']:
            url = s3_upload(form.cleaned_data['pic'], profile_to_edit.id)
            profile_to_edit.picture = url
    
    form.save()
    return redirect(reverse('home'))
Exemplo n.º 6
0
def add_comment(request, id):
	form = CommentForm(request.POST, request.FILES)
	post = Post.objects.get(id=id)
	student = Student.objects.get(user=request.user)
	#form.cleaned_data["text"]
	form.is_valid()
	new_comment = Comment(text=form.cleaned_data["text"], student=student, upvotes=0)
	new_comment.save()
	if form.cleaned_data['attachment']:
	    url = s3_upload(form.cleaned_data['attachment'], new_comment.id)
	    new_comment.attachment_url = url
	    if form.cleaned_data['attachment_name']:
	        new_comment.attachment_name = form.cleaned_data['attachment_name']
	    else:
	        new_comment.attachment_name = post.title
	    new_comment.save()
	post.comments.add(new_comment)
	post.save()
	class_name = post.classroom

	# Notification function
	notif_text = request.user.get_full_name() + " commented on your post"
	notif_link = '/studyroom/show_post_studygroup/' + str(post.id)
	notify(request, post.student.id, notif_text, notif_link, persistent=False)

	return show_post_studygroup(request, post.id)
Exemplo n.º 7
0
def edit(request):
    context = get_default_context(request)
    context['user_id'] = request.user.id
    context['picture_url'] = Student.objects.get(user=request.user).picture_url
    profile = Student.objects.get(user=request.user)
    form = EditForm()
    try:
        if request.method == 'GET':
            context['profile'] = profile
            context['form'] = EditForm()
            print "in GET"
            return render(request, 'socialnetwork/edit.html', context)

        context['profile'] = Student.objects.get(user=request.user)
        form = EditForm(request.POST, request.FILES)
        context['form'] = form
        #print form
        if not form.is_valid():
            return render(request, 'socialnetwork/edit.html', context)
        #profile = form.save()

        # Update first and last name of the User
        user = request.user
        if form.cleaned_data['first_name']:
            user.first_name = form.cleaned_data['first_name']
        if form.cleaned_data['last_name']:
            user.last_name = form.cleaned_data['last_name']
        student = Student.objects.get(user=user)
        if form.cleaned_data['school']:
            student.school = form.cleaned_data['school']
        if form.cleaned_data['major']:
            student.major = form.cleaned_data['major']

        if form.cleaned_data['picture']:
            url = s3_upload(form.cleaned_data['picture'], student.id)
            student.picture_url = url
            student.save()
        student.save()
        user.save()

        # form = EditForm(instance=entry)
        context['message'] = 'Profile updated.'
        context['profile'] = profile
        context['form'] = form
        context['first_name'] = user.first_name
        context['last_name'] = user.last_name
        context['user_id'] = request.user.id
        context['classes'] = Student.objects.get(
            user=request.user).classes.all()
        context['picture_url'] = student.picture_url
        #return render(request, 'socialnetwork/profile.html', context)

        return redirect('/socialnetwork/profile/' + str(request.user.id))
    except Student.DoesNotExist:
        print 'F**K'
        context = {'message': 'Record with id={0} does not exist'.format(id)}
        return render(request, 'socialnetwork/edit.html', context)
Exemplo n.º 8
0
def edit(request):
    context = get_default_context(request)
    context['user_id'] = request.user.id
    context['picture_url'] = Student.objects.get(user=request.user).picture_url
    profile = Student.objects.get(user=request.user)
    form = EditForm()
    try:
        if request.method == 'GET':
            context['profile'] = profile
            context['form'] = EditForm()
            print "in GET"
            return render(request, 'socialnetwork/edit.html', context)
            
        context['profile'] = Student.objects.get(user=request.user)
        form = EditForm(request.POST, request.FILES)
        context['form'] = form
        #print form
        if not form.is_valid():
            return render(request, 'socialnetwork/edit.html', context)
        #profile = form.save()

        # Update first and last name of the User
        user = request.user
        if form.cleaned_data['first_name']:
            user.first_name = form.cleaned_data['first_name']
        if form.cleaned_data['last_name']:
            user.last_name = form.cleaned_data['last_name']
        student = Student.objects.get(user=user)
        if form.cleaned_data['school']:
            student.school = form.cleaned_data['school']
        if form.cleaned_data['major']:
            student.major = form.cleaned_data['major']
        
        if form.cleaned_data['picture']:
            url = s3_upload(form.cleaned_data['picture'], student.id)
            student.picture_url = url
            student.save()
        student.save()
        user.save()
        

        # form = EditForm(instance=entry)
        context['message'] = 'Profile updated.'
        context['profile'] = profile
        context['form'] = form
        context['first_name'] = user.first_name
        context['last_name'] = user.last_name
        context['user_id'] = request.user.id
        context['classes'] = Student.objects.get(user=request.user).classes.all()
        context['picture_url'] = student.picture_url
        #return render(request, 'socialnetwork/profile.html', context)

        return redirect('/socialnetwork/profile/' + str(request.user.id))
    except Student.DoesNotExist:
        print 'F**K'
        context = { 'message': 'Record with id={0} does not exist'.format(id) }
        return render(request, 'socialnetwork/edit.html', context)
def edit_profile(request):
    context={}
    if request.method == 'GET':
        
        user = request.user

        initial = {}
        initial['first_name'] = user.first_name
        initial['last_name'] = user.last_name
        if user.userprofile.age:
            initial['age'] = user.userprofile.age
        if user.userprofile.bio:
            initial['bio'] = user.userprofile.bio

        form = EditProfileForm(initial=initial)

        context['form'] = form
        return render(request, 'socialnetwork/edit_profile.html',context)

    form = EditProfileForm(request.POST,request.FILES)
    context['form']=form

    if not form.is_valid():
        return render(request, 'socialnetwork/edit_profile.html',context) 

    user = request.user
    if ('first_name' in form.cleaned_data) and form.cleaned_data['first_name']:
        user.first_name = form.cleaned_data['first_name']
    if ('last_name' in form.cleaned_data) and form.cleaned_data['last_name']:
        user.last_name = form.cleaned_data['last_name']
    
    if ('age' in form.cleaned_data) and form.cleaned_data['age']:
        user.userprofile.age = form.cleaned_data['age']
    if ('bio' in form.cleaned_data) and form.cleaned_data['bio']:
        user.userprofile.bio = form.cleaned_data['bio']

    if ('picture' in form.cleaned_data) and form.cleaned_data['picture']:
        if form.cleaned_data['picture']:
            url = s3_upload(form.cleaned_data['picture'], user.id)
            user.userprofile.picture_url = url

    if ('password1' in form.cleaned_data) and form.cleaned_data['password1']:
        user.set_password(form.cleaned_data['password1'])


    user.userprofile.save()
    user.save()

    if ('password1' in form.cleaned_data) and form.cleaned_data['password1']:
        new_user = authenticate(username=user.username,
                            password=form.cleaned_data['password1'])
        login(request, new_user)

    return redirect(reverse('profile', args=[request.user.username]))
Exemplo n.º 10
0
def edit_profile(request):
    context = {}
    profile = Profile.objects.get(account=request.user)
    context['profile'] = profile
    if request.method == 'GET':
        return render(request, 'edit_profile.html', context)
    form = EditProfileForm(request.POST, request.FILES, instance=profile)
    if not form.is_valid():
        context['form'] = form
        return render(request, 'edit_profile.html', context)
    if form.cleaned_data['avatar']:
        url = s3_upload(form.cleaned_data['avatar'], profile.id)
        profile.picture_url = url
    form.save()
    return redirect(reverse('profile', kwargs={'user_id': request.user.id}))
Exemplo n.º 11
0
def editprofile(request):
    context={}
    user=request.user
    if request.method == 'GET': 
        #populating it with the values saved in the database
        context['registration_form'] = EditRegistrationForm(initial={'first_name': user.first_name,
        'last_name': user.last_name})
        profile_of_user = User.objects.get(id=user.id)
        userprofileinfo = Profile.objects.get(user=profile_of_user)
        addpost_form = AddPostForm(request.POST)
        context['addpost_form'] = addpost_form
        context['profile_form'] = EditProfile(instance=userprofileinfo)
        return render(request, 'socialnetwork/editprofile.html', context)

    registration = User.objects.get(id=user.id)
    registration_form=EditRegistrationForm(request.POST)
    edit_profile = Profile.objects.get(user=registration)
    profile_form = EditProfile(request.POST, request.FILES, instance=edit_profile)

    if not registration_form.is_valid():
        return render(request, 'socialnetwork/editprofile.html', {'registration_form':registration_form, 'profile_form':profile_form})
  
    registration.first_name=registration_form.cleaned_data['first_name']
    registration.last_name=registration_form.cleaned_data['last_name']
    registration.save()

    if not profile_form.is_valid():
        return render(request, 'socialnetwork/editprofile.html', {'registration_form':registration_form, 'profile_form':profile_form})
    if profile_form.cleaned_data['picture']:
        url = s3_upload(profile_form.cleaned_data['picture'], registration)
        profile=Profile.objects.get(user=request.user)
        profile.picture_url = url

    profile_form.save()
    posts = Posts.objects.all().order_by('date_time').reverse()
    profiles= Profile.objects.all()
    comments = Comment.objects.all()
    addpost_form = AddPostForm(request.POST)
    context['addpost_form']=addpost_form
    context = {'posts' : posts, 'comments':comments, 'profiles':profiles, 'addpost_form':addpost_form}
    return render(request, 'socialnetwork/globalstream.html', context)
Exemplo n.º 12
0
def edit_profile(request, user):
    messages = []
    errors = []
    context = {
        'errors': errors, 
        'messages': messages,
        'userinfo_form': EditUserInfoForm(),
        'customuser_form': CustomUserForm(),
    }
    u = User.objects.get(username=user)
    customuser_form = CustomUserForm(request.POST, request.FILES, instance=request.user)
    userinfo_form = EditUserInfoForm(request.POST, instance=request.user)
    context['customuser_form'] = customuser_form
    context['userinfo_form'] = userinfo_form
    context['thisUser'] = u

   #Validates the form
    if not userinfo_form.is_valid():
        return render(request, 'edit_profile.html', context)
    if not customuser_form.is_valid():
        return render(request, 'edit_profile.html', context)
    
    u.first_name = userinfo_form.cleaned_data['first_name']
    u.last_name = userinfo_form.cleaned_data['last_name']
    u.save()   
    
    custom_user = CustomUser.objects.get(user__username=user)
    custom_user.age = customuser_form.cleaned_data['age']
    custom_user.shortbio = customuser_form.cleaned_data['shortbio']
    #set value id
    custom_user.save()
    
    if (customuser_form.cleaned_data['picture']):
        url = s3_upload(customuser_form.cleaned_data['picture'], custom_user.id)
        custom_user.picture_url = url
        custom_user.save()      

    context['custom_user'] = custom_user
    return render(request, 'profile.html', context)
Exemplo n.º 13
0
def create_profile(request):
    if request.method == 'GET':
        context = {'form': profileform()}
        return render(request, 'createprofile.html', context)

    entry = profile(user=request.user, username=request.user)
    form = profileform(request.POST, request.FILES, instance=entry)

    if not form.is_valid():
        context = {'form': form}
        return render(request, 'createprofile.html', context)

    # Save the new record
    form.save()
    #url
    profile_to_edit = get_object_or_404(profile, user=request.user)
    if form.cleaned_data['pic']:
        url = s3_upload(form.cleaned_data['pic'], profile_to_edit.id)
        profile_to_edit.picture = url
        profile_to_edit.save()
        #form.save()

    return redirect(reverse('home'))
Exemplo n.º 14
0
def add_post_studygroup(request, id):
	student = Student.objects.get(user=request.user)
	studygroup = StudyGroup.objects.get(id=id)
	if student not in studygroup.members.all():
		return home(request, "You arent in this studygroup")
	errors = []
	form = PostForm(request.POST, request.FILES)
	if(form.is_valid()):
		post = Post(text=form.cleaned_data['text'], title=form.cleaned_data['title'])
		
		
		post.studygroup = studygroup
		post.student = student
		post.upvotes = 0
		post.save()
		if form.cleaned_data['attachment']:
		    url = s3_upload(form.cleaned_data['attachment'], post.id)
		    post.attachment_url = url
		    if form.cleaned_data['attachment_name']:
		    	post.attachment_name = form.cleaned_data['attachment_name']
		    else:
		    	post.attachment_name = post.title
		    post.save()
	return show_post_studygroup(request, post.id)
Exemplo n.º 15
0
def add_post(request, name):
    
    errors = []
    form = PostForm(request.POST, request.FILES)
    if(form.is_valid()):
        post = Post(text=form.cleaned_data['text'], title=form.cleaned_data['title'])
        student = Student.objects.get(user=request.user)
        classroom = Classroom.objects.get(name=name)
        post.classroom = classroom
        post.student = student
        post.upvotes = 0
        post.save()
        if form.cleaned_data['attachment']:
            url = s3_upload(form.cleaned_data['attachment'], post.id)
            post.attachment_url = url
            if form.cleaned_data['attachment_name']:
                post.attachment_name = form.cleaned_data['attachment_name']
            else:
                post.attachment_name = post.title
            post.save()
        return show_post(request, post.id)
    else:
        print 'FORM NOT VALID'
    return change_class(request, name)
Exemplo n.º 16
0
def create_profile(request):
    if request.method == 'GET':
        context = { 'form': profileform()}
        return render(request, 'createprofile.html', context)


    entry = profile(user=request.user,username=request.user)
    form = profileform(request.POST, request.FILES, instance=entry)

    if not form.is_valid():
        context = { 'form': form }
        return render(request, 'createprofile.html', context)
   
    # Save the new record
    form.save()
    #url
    profile_to_edit=get_object_or_404(profile,user=request.user)
    if form.cleaned_data['pic']:
        url = s3_upload(form.cleaned_data['pic'], profile_to_edit.id)
        profile_to_edit.picture = url
        profile_to_edit.save()
        #form.save()

    return redirect(reverse('home'))
Exemplo n.º 17
0
def register(request):
    context = {}
    print request.FILES
    # Just display the registration form if this is a GET request.
    if request.method == 'GET':
        context['registration_form'] = RegistrationForm()
        context['profile_form'] = EditProfile()
        return render(request, 'socialnetwork/register.html', context)

    # Creates a bound form from the request POST parameters and makes the 
    # form available in the request context dictionary.
    registration_form = RegistrationForm(request.POST)
    profile_form = EditProfile(request.POST, request.FILES)

    # Validates the form.
    if not registration_form.is_valid():
        print "reg form not valid"
        return render(request, 'socialnetwork/register.html', {'registration_form':registration_form, 'profile_form':profile_form})

    if not profile_form.is_valid():
        print "profile form not valid"
        return render(request, 'socialnetwork/register.html', {'registration_form':registration_form, 'profile_form':profile_form})

    # At this point, the form data is valid.  Register and login the user.
    new_user = User.objects.create_user(username=registration_form.cleaned_data['username'], 
                                        password=registration_form.cleaned_data['password1'],
                                        first_name=registration_form.cleaned_data['first_name'],
                                        last_name=registration_form.cleaned_data['last_name'],
                                        email=registration_form.cleaned_data['email'])

    # Mark the user as inactive to prevent login before email confirmation.
    new_user.is_active = False
    new_user.save()

    new_profile = Profile()
    new_profile.user = new_user
    new_profile.age=profile_form.cleaned_data['age'] 
    new_profile.bio=profile_form.cleaned_data['bio']

    if profile_form.cleaned_data['picture']:
        url = s3_upload(profile_form.cleaned_data['picture'], new_user.id)
        new_profile.picture_url = url
    new_profile.save()

    # Generate a one-time use token and an email message body
    token = default_token_generator.make_token(new_user)

    email_body = """
Welcome to your Social Network. Please click the link below to
verify your email address and complete the registration of your account:

  http://%s%s
""" % (request.get_host(), 
       reverse('confirm', args=(new_user.username, token)))

    send_mail(subject="Verify your email address",
              message= email_body,
              from_email="*****@*****.**",
              recipient_list=[new_user.email])

    context['email'] = registration_form.cleaned_data['email']
    return render(request, 'socialnetwork/needsconfirmation.html', context)