def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.is_active = False user.save() profile = Profile.create_profile(user) profile.host = "http://cs410.cs.ualberta.ca:41024" profile.save() registered = True else: print user_form.errors else: user_form = UserForm() if registered == True: return HttpResponse("<script>alert(\"Account Successfully Created!!\"); window.location = \'/login\';</script>") else: return render_to_response('authors/register.html', {'user_form': user_form, 'registered': registered}, context)
def posts(request): context = RequestContext(request) # retrieve form data if request.method == 'POST': post_form = PostForm(request.user,data=request.POST) if post_form.is_valid(): privacy = post_form.cleaned_data['privacy'] post_text = post_form.cleaned_data['post_text'] title = post_form.cleaned_data['title'] description = post_form.cleaned_data['description'] content_type = post_form.cleaned_data['content_type'] date = timezone.now() # check if a new photo was uploaded try: image = request.FILES['image'] except: image="" # get current user currentUser=request.user # if profile for current user already exists, retrieve it try: author=Profile.objects.get(user=currentUser) # create new profile for current user if one doesn't exist except: userObject = User.objects.get(username=currentUser) profile = Profile.create_profile(userObject) profile.host = request.get_host() profile.save() author = profile # create a new post given the form submission data newPost = Post(post_text=post_text, description=description, title=title, date=date,author=author,privacy=privacy, image=image, content_type=content_type) # save the new post in the database newPost.save() # special privacy settings: friend of a friend if privacy=="3": all_friends=Profile.objects.get(user=currentUser) for friend in all_friends.friends.all(): foaf=Profile.objects.get(user=User.objects.get(username=friend.user)) for second_friend in foaf.friends.all(): try: newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=second_friend.user))) except: continue newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=author))) # special privacy settings: friends on this server elif privacy=="4": all_friends=Profile.objects.get(user=currentUser) for friend in all_friends.friends.all(): f=Profile.objects.get(user=User.objects.get(username=friend.user)) if f.host == 'http://cs410.cs.ualberta.ca:41024': newPost.allowed.add(f) newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=author))) # special privacy settings: friends elif privacy=="5": all_friends=Profile.objects.get(user=currentUser) for friend in all_friends.friends.all(): newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=friend.user))) newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=author))) # special privacy settings: private elif privacy=="2": newPost.allowed.add(Profile.objects.get(user=User.objects.get(username=author))) # once the new post is added, return to homepage return redirect('/') # display error if fields aren't filled properly else: print post_form.errors # display the post form else: post_form = PostForm(request.user) my_profile = Profile.objects.get(user=request.user) return render(request, 'posts/posts.html', {'post_form':post_form, 'my_profile':my_profile})