def new_post(request, post_type): """View for creating a new post of any of the three types. This view renders a PostForm for the user to fill out and submit when called with an HTTP GET request. If called with an HTTP POST request, this view will validate the form data and if it is valid, will process further: -If user has a registered account and is logged in, will mark post as verified, save the post object to the database and redirect the user to their newly created post url. -If user is not registered or logged in, will mark post as unverified, save the post object to the database, generate a post edit/verification link and email it to the email address provided by the user. This view also handles photo uploads for a post. Arguments: request -- the HTTP request from the users browser post_type -- the type of the posts to be retrieved form the database """ # Check for logged in user and set template parameters accordingly if request.user.is_authenticated(): logtext = "Logout" accounttext = "My Account" welcometext = request.user.username logparams=[logtext,accounttext, welcometext] else: logtext = "Login" accounttext = "Sign Up" logparams=[logtext,accounttext] # only registered users are allowed to create posts, if not register render non valid user page return render_to_response("users/not_user.html", {'logparams': logparams}, context_instance=RequestContext(request)) if post_type == "blog" and request.user.is_superuser == False: # only super users (site admins) are allowed to create blog posts, if not register render non valid admin page return render_to_response("users/not_admin.html", {'logparams': logparams}, context_instance=RequestContext(request)) # format the post_type that was in the URL to lowercase string post_type = str(post_type.lower()) pictureform = UploadForm() #action for submit button submit_action = URL_PATHS.get(post_type+'_new') # Generate and render a fresh post creation form on HTTP GET requests. if request.method == 'GET': # setup the post creation form and initialize creator fields and email verification fields # (email verification left here so that it will be a simple matter to open non blog post creation to # unregistered users if desired. form = PostForm(instance=Post(), initial={'creator':request.user.email, 'email_verification':request.user.email}) if request.user.is_authenticated(): form.fields['creator'].widget = forms.HiddenInput() form.fields['email_verification'].widget = forms.HiddenInput() form_args = {'form':form, 'submit_action':submit_action, 'message':None, 'post_type_title':POST_TYPE_TITLES.get(post_type), 'pictureform':pictureform, 'logparams': logparams} # render the form for the user to fill out and submit return render_to_response(TEMPLATE_PATHS.get("posts_new"), form_args, context_instance=RequestContext(request)) # Validate a post creation form on HTTP POST requests and render appropriate response if request.method == 'POST': # get the completed form from the request and process post_form = PostForm(request.POST) if request.user.is_authenticated(): post_form.fields['creator'].widget = forms.HiddenInput() post_form.fields['email_verification'].widget = forms.HiddenInput() if post_form.is_valid() and request.POST.get("notnewpost") == None: # generate values for non-user defined fields in post object, # write to db and return post object post = post_form.save(commit=False) post.set_type(post_type.lower()) post.set_url( tag_maker("_", post) ) post_url = post.get_url() post_url = HttpRequest.build_absolute_uri(request, post_url) if request.user.is_authenticated(): post.verified = True post.save() postid = post.id form = UploadForm(request.POST, request.FILES) if request.POST.get("notnewpost") != None: postid = request.POST.get("postid") post = Post.objects.get(id = postid ) #post.set_type(post_type.lower()) #post.set_url( tag_maker("_", post) ) post_url = post.get_url() form_args = {'form':post_form, 'submit_action':submit_action, 'post_url' : post_url, 'post':post, 'logparams':logparams} if form.is_valid(): form_args = {'post':post, 'post_url': post_url, 'logparams':logparams} photo = Photo(photo = request.FILES['picture'], post = post, caption = request.POST.get('caption') ) photo.save() if request.POST.get('pictureform') == "1" and request.POST.get("issubmit") != "1": #photolist = Photo.objects.filter(post_id = post.id) addanotherprevious = list() for o in Photo.objects.filter(post_id = post.id): item = [o.photo.name,o.id] addanotherprevious.append(item) form_args = {'form':post_form, 'submit_action':submit_action, 'pictureform': pictureform, 'postid' :postid, 'addanotherprevious' : addanotherprevious, 'logparams':logparams} return render_to_response("posts/posts_new.html", form_args, context_instance=RequestContext(request)) #==================================================================== # Testing - REMOVE LATER - this just creates x # of posts of a given # type whenever a single one is created from the web, just used to # populate db for testing purposes #==================================================================== #multiple_entries_for_testing(10000) if post.is_verified(): # if post is already verified, redirect user to their newly created post return redirect("/posts/" + post_type +"/" + post.url, context_instance=RequestContext(request)) # create a verification/edit link and send with mailer then direct to success message page user_email = post.get_creator() verify_url = '%s%s?post_id=%s&uuid=%s' % (Site.objects.get_current(), URL_PATHS.get('posts_edit-verify'), post.id, post.get_uuid()) send_post_verification_email(verify_url, user_email, post_type) return render_to_response(TEMPLATE_PATHS.get("posts_success"), form_args, context_instance=RequestContext(request)) else: # if form submission not valid, redirect back to form with error messages form_args = {'form':post_form, 'submit_action':submit_action, 'message':None, 'post_type_title':POST_TYPE_TITLES.get(post_type), 'pictureform': pictureform, 'post_type': post_type} return render_to_response(TEMPLATE_PATHS.get("posts_new"), form_args, context_instance=RequestContext(request)) raise Http404
def create_listing(request): """ Loads the page for the users to post a new listing (either sale items or wanted items) and when a new listing is submitted, it is saved in the database """ if request.user.is_authenticated(): logtext = "Logout" accounttext = "My Account" welcometext = request.user.username logparams = [logtext, accounttext, welcometext] else: logtext = "Login" accounttext = "Sign Up" logparams = [logtext, accounttext] submit_action = URL_PATHS.get("listing_new") # create a new upload form that will be rendered to page pictureform = UploadForm() if request.method == "GET": if request.user.is_authenticated(): city = UserProfile.objects.get(username=request.user.email) form = ListingForm( instance=Listing(), initial={"creator": request.user.email, "email_verification": request.user.email, "city": city}, ) form.fields["creator"].widget = forms.HiddenInput() form.fields["email_verification"].widget = forms.HiddenInput() else: form = ListingForm(instance=Listing()) form_args = {"form": form, "submit_action": submit_action, "pictureform": pictureform, "logparams": logparams} return render_to_response( TEMPLATE_PATHS.get("listings_new"), form_args, context_instance=RequestContext(request) ) if request.method == "POST": pictureform = UploadForm(request.POST, request.FILES) listing_form = ListingForm(request.POST) form_args = {} if request.user.is_authenticated(): listing_form.fields["creator"].widget = forms.HiddenInput() listing_form.fields["email_verification"].widget = forms.HiddenInput() # if the listing form is valid and the listing has not yet been created if listing_form.is_valid() and request.POST.get("notnewlisting") == None: listing = listing_form.save(commit=False) # if user is logged in, then verify post if request.user.is_authenticated(): listing.verified = True listing.set_url(tag_maker("_", listing)) listing_url = listing.url listing_url = HttpRequest.build_absolute_uri(request, listing_url) listing.save() listingid = listing.id logger.debug("format", "createListing: debug") if request.POST.get("notnewlisting") != None: listingid = request.POST.get("listingid") listing = Listing.objects.get(id=listingid) listing_url = listing.get_url() form_args = { "form": listing_form, "submit_action": submit_action, "listing_url": listing_url, "listing": listing, "logparams": logparams, } # if the picture form is valid, save the picture if pictureform.is_valid(): form_args = {"listing": listing, "listing_url": listing_url, "logparams": logparams} photo = Photo(photo=request.FILES["picture"], listing=listing) photo.save() # if user wants to add another picture if request.POST.get("pictureform") == "1" and request.POST.get("issubmit") != 1: photolist = Photo.objects.filter(listing_id=listing.id) addanotherprevious = list() # get all the names of the previously added pictures for o in Photo.objects.filter(listing_id=listing.id): addanotherprevious.append(o.photo.name) form_args = { "form": listing_form, "submit_action": submit_action, "pictureform": pictureform, "listingid": listingid, "addanotherprevious": addanotherprevious, "logparams": logparams, } return render_to_response( "listings/listings_new.html", form_args, context_instance=RequestContext(request) ) # ==================================================================== # Testing purposes only - this just creates x # of listings # whenever a single one is created from the web, just used to # populate db for testing purposes # ==================================================================== # multiple_entries_for_testing(10000) if listing.verified: # if post is already verified, redirect user to their newly created post) return redirect("/listings/" + listing.url, context_instance=RequestContext(request)) # create a verification/edit link and send with mailer then direct to success message page user_email = listing.get_creator() verify_url = "%s%s?listing_id=%s&uuid=%s" % ( Site.objects.get_current(), URL_PATHS.get("listings_edit-verify"), listing.id, listing.get_uuid(), ) send_post_verification_email(verify_url, user_email, "list") return render_to_response( TEMPLATE_PATHS.get("listings_success"), form_args, context_instance=RequestContext(request) ) raise Http404
def new_post(request, post_type): """View for creating a new post of any of the three types. This view renders a PostForm for the user to fill out and submit when called with an HTTP GET request. If called with an HTTP POST request, this view will validate the form data and if it is valid, will process further: -If user has a registered account and is logged in, will mark post as verified, save the post object to the database and redirect the user to their newly created post url. -If user is not registered or logged in, will mark post as unverified, save the post object to the database, generate a post edit/verification link and email it to the email address provided by the user. This view also handles photo uploads for a post. Arguments: request -- the HTTP request from the users browser post_type -- the type of the posts to be retrieved form the database """ # Check for logged in user and set template parameters accordingly if request.user.is_authenticated(): logtext = "Logout" accounttext = "My Account" welcometext = request.user.username logparams = [logtext, accounttext, welcometext] else: logtext = "Login" accounttext = "Sign Up" logparams = [logtext, accounttext] # only registered users are allowed to create posts, if not register render non valid user page return render_to_response( "users/not_user.html", {"logparams": logparams}, context_instance=RequestContext(request) ) if post_type == "blog" and request.user.is_superuser == False: # only super users (site admins) are allowed to create blog posts, if not register render non valid admin page return render_to_response( "users/not_admin.html", {"logparams": logparams}, context_instance=RequestContext(request) ) # format the post_type that was in the URL to lowercase string post_type = str(post_type.lower()) pictureform = UploadForm() # action for submit button submit_action = URL_PATHS.get(post_type + "_new") # Generate and render a fresh post creation form on HTTP GET requests. if request.method == "GET": # setup the post creation form and initialize creator fields and email verification fields # (email verification left here so that it will be a simple matter to open non blog post creation to # unregistered users if desired. form = PostForm( instance=Post(), initial={"creator": request.user.email, "email_verification": request.user.email} ) if request.user.is_authenticated(): form.fields["creator"].widget = forms.HiddenInput() form.fields["email_verification"].widget = forms.HiddenInput() form_args = { "form": form, "submit_action": submit_action, "message": None, "post_type_title": POST_TYPE_TITLES.get(post_type), "pictureform": pictureform, "logparams": logparams, } # render the form for the user to fill out and submit return render_to_response(TEMPLATE_PATHS.get("posts_new"), form_args, context_instance=RequestContext(request)) # Validate a post creation form on HTTP POST requests and render appropriate response if request.method == "POST": # get the completed form from the request and process post_form = PostForm(request.POST) if request.user.is_authenticated(): post_form.fields["creator"].widget = forms.HiddenInput() post_form.fields["email_verification"].widget = forms.HiddenInput() if post_form.is_valid() and request.POST.get("notnewpost") == None: # generate values for non-user defined fields in post object, # write to db and return post object post = post_form.save(commit=False) post.set_type(post_type.lower()) post.set_url(tag_maker("_", post)) post_url = post.get_url() post_url = HttpRequest.build_absolute_uri(request, post_url) if request.user.is_authenticated(): post.verified = True post.save() postid = post.id form = UploadForm(request.POST, request.FILES) if request.POST.get("notnewpost") != None: postid = request.POST.get("postid") post = Post.objects.get(id=postid) # post.set_type(post_type.lower()) # post.set_url( tag_maker("_", post) ) post_url = post.get_url() form_args = { "form": post_form, "submit_action": submit_action, "post_url": post_url, "post": post, "logparams": logparams, } if form.is_valid(): form_args = {"post": post, "post_url": post_url, "logparams": logparams} photo = Photo(photo=request.FILES["picture"], post=post, caption=request.POST.get("caption")) photo.save() if request.POST.get("pictureform") == "1" and request.POST.get("issubmit") != "1": # photolist = Photo.objects.filter(post_id = post.id) addanotherprevious = list() for o in Photo.objects.filter(post_id=post.id): item = [o.photo.name, o.id] addanotherprevious.append(item) form_args = { "form": post_form, "submit_action": submit_action, "pictureform": pictureform, "postid": postid, "addanotherprevious": addanotherprevious, "logparams": logparams, } return render_to_response("posts/posts_new.html", form_args, context_instance=RequestContext(request)) # ==================================================================== # Testing - REMOVE LATER - this just creates x # of posts of a given # type whenever a single one is created from the web, just used to # populate db for testing purposes # ==================================================================== # multiple_entries_for_testing(10000) if post.is_verified(): # if post is already verified, redirect user to their newly created post return redirect("/posts/" + post_type + "/" + post.url, context_instance=RequestContext(request)) # create a verification/edit link and send with mailer then direct to success message page user_email = post.get_creator() verify_url = "%s%s?post_id=%s&uuid=%s" % ( Site.objects.get_current(), URL_PATHS.get("posts_edit-verify"), post.id, post.get_uuid(), ) send_post_verification_email(verify_url, user_email, post_type) return render_to_response( TEMPLATE_PATHS.get("posts_success"), form_args, context_instance=RequestContext(request) ) else: # if form submission not valid, redirect back to form with error messages form_args = { "form": post_form, "submit_action": submit_action, "message": None, "post_type_title": POST_TYPE_TITLES.get(post_type), "pictureform": pictureform, "post_type": post_type, } return render_to_response(TEMPLATE_PATHS.get("posts_new"), form_args, context_instance=RequestContext(request)) raise Http404
def create_listing(request): ''' Loads the page for the users to post a new listing (either sale items or wanted items) and when a new listing is submitted, it is saved in the database ''' if request.user.is_authenticated(): logtext = "Logout" accounttext = "My Account" welcometext = request.user.username logparams = [logtext, accounttext, welcometext] else: logtext = "Login" accounttext = "Sign Up" logparams = [logtext, accounttext] submit_action = URL_PATHS.get('listing_new') #create a new upload form that will be rendered to page pictureform = UploadForm() if request.method == 'GET': if request.user.is_authenticated(): city = UserProfile.objects.get(username=request.user.email) form = ListingForm(instance=Listing(), initial={ 'creator': request.user.email, 'email_verification': request.user.email, 'city': city }) form.fields['creator'].widget = forms.HiddenInput() form.fields['email_verification'].widget = forms.HiddenInput() else: form = ListingForm(instance=Listing()) form_args = { 'form': form, 'submit_action': submit_action, 'pictureform': pictureform, 'logparams': logparams } return render_to_response(TEMPLATE_PATHS.get('listings_new'), form_args, context_instance=RequestContext(request)) if request.method == 'POST': pictureform = UploadForm(request.POST, request.FILES) listing_form = ListingForm(request.POST) form_args = {} if request.user.is_authenticated(): listing_form.fields['creator'].widget = forms.HiddenInput() listing_form.fields[ 'email_verification'].widget = forms.HiddenInput() #if the listing form is valid and the listing has not yet been created if listing_form.is_valid() and request.POST.get( "notnewlisting") == None: listing = listing_form.save(commit=False) #if user is logged in, then verify post if request.user.is_authenticated(): listing.verified = True listing.set_url(tag_maker("_", listing)) listing_url = listing.url listing_url = HttpRequest.build_absolute_uri(request, listing_url) listing.save() listingid = listing.id logger.debug('format', "createListing: debug") if request.POST.get("notnewlisting") != None: listingid = request.POST.get("listingid") listing = Listing.objects.get(id=listingid) listing_url = listing.get_url() form_args = { 'form': listing_form, 'submit_action': submit_action, 'listing_url': listing_url, 'listing': listing, 'logparams': logparams } # if the picture form is valid, save the picture if pictureform.is_valid(): form_args = { 'listing': listing, 'listing_url': listing_url, 'logparams': logparams } photo = Photo(photo=request.FILES['picture'], listing=listing) photo.save() #if user wants to add another picture if request.POST.get('pictureform' ) == "1" and request.POST.get("issubmit") != 1: photolist = Photo.objects.filter(listing_id=listing.id) addanotherprevious = list() #get all the names of the previously added pictures for o in Photo.objects.filter(listing_id=listing.id): addanotherprevious.append(o.photo.name) form_args = { 'form': listing_form, 'submit_action': submit_action, 'pictureform': pictureform, 'listingid': listingid, 'addanotherprevious': addanotherprevious, 'logparams': logparams } return render_to_response( "listings/listings_new.html", form_args, context_instance=RequestContext(request)) #==================================================================== # Testing purposes only - this just creates x # of listings # whenever a single one is created from the web, just used to # populate db for testing purposes #==================================================================== #multiple_entries_for_testing(10000) if listing.verified: # if post is already verified, redirect user to their newly created post) return redirect("/listings/" + listing.url, context_instance=RequestContext(request)) # create a verification/edit link and send with mailer then direct to success message page user_email = listing.get_creator() verify_url = '%s%s?listing_id=%s&uuid=%s' % ( Site.objects.get_current(), URL_PATHS.get('listings_edit-verify'), listing.id, listing.get_uuid()) send_post_verification_email(verify_url, user_email, 'list') return render_to_response(TEMPLATE_PATHS.get('listings_success'), form_args, context_instance=RequestContext(request)) raise Http404