Esempio n. 1
0
def edit_verify_listing(request):
    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]
    pictureform = UploadForm()
    listing_id = request.GET.get("listing_id")
    uuid = request.GET.get("uuid")
    # action for submit button
    submit_action = URL_PATHS.get("listings_edit-verify") + "?listing_id=" + listing_id + "&uuid=" + uuid
    listing_url = None
    listing = None
    message = None

    try:
        listing = get_object_or_404(Listing, id=str(listing_id))
    except:
        raise Http404
    listpictures = list()
    if request.POST.get("deletephoto") != None and request.POST.get("deletephotoyes") == "true":
        deletephoto = Photo.objects.get(id=request.POST.get("deletephoto"))
        deletephoto.delete()
    if request.method == "GET":

        if not listing.is_verified():
            if listing and (listing.get_uuid() == str(uuid)):
                listing.mark_verified()
                listing.save()
                message = MESSAGES.get("verified_listing")
            else:
                # the listing_id and uuid provided do not match anything in db correctly
                # so redirect to 404 as this page doesn't exist for this combination
                raise Http404
        # post verified by this point, render edit page with message
        edit_form = EditListingForm(instance=listing)
        try:
            for o in Photo.objects.filter(listing_id=listing_id):
                item = [o.photo.name, o.id]
                listpictures.append(item)
        except:
            listpictures = None
        form_args = {
            "form": edit_form,
            "message": message,
            "submit_action": submit_action,
            "logparams": logparams,
            "listing": listing,
            "listpictures": listpictures,
            "pictureform": pictureform,
        }
        return render_to_response(
            TEMPLATE_PATHS.get("listings_edit"), form_args, context_instance=RequestContext(request)
        )

    if request.method == "POST":
        form = UploadForm(request.POST, request.FILES)
        edit_form = EditListingForm(request.POST, instance=listing)

        # if post_form valid, process new post
        if edit_form.is_valid():

            listing_url = HttpRequest.build_absolute_uri(request, edit_form.cleaned_data.get("url"))
            edit_form.save()
            if form.is_valid():
                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()
            # This redirects back to edit form, should change to a render_to_response with a message that edit successful
            try:
                for o in Photo.objects.filter(listing_id=listing_id):
                    item = [o.photo.name, o.id]
                    listpictures.append(item)
            except:
                listpictures = None
            form_args = {
                "form": edit_form,
                "submit_action": submit_action,
                "message": MESSAGES.get("edit_success"),
                "listing_url": listing_url,
                "logparams": logparams,
                "pictureform": pictureform,
                "listpictures": listpictures,
            }
            return render_to_response(
                TEMPLATE_PATHS.get("listings_edit"), form_args, context_instance=RequestContext(request)
            )
        else:

            # if form submission not valid, redirect back to form with error messages
            form_args = {
                "form": edit_form,
                "submit_action": submit_action,
                "message": None,
                "logparams": logparams,
                "listpictures": listpictures,
                "pictureform": pictureform,
            }
            return render_to_response(
                TEMPLATE_PATHS.get("listings_edit"), form_args, context_instance=RequestContext(request)
            )

        raise Http404
Esempio n. 2
0
def edit_verify_listing(request):
    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]
    pictureform = UploadForm()
    listing_id = request.GET.get('listing_id')
    uuid = request.GET.get('uuid')
    #action for submit button
    submit_action = URL_PATHS.get(
        'listings_edit-verify') + '?listing_id=' + listing_id + '&uuid=' + uuid
    listing_url = None
    listing = None
    message = None

    try:
        listing = get_object_or_404(Listing, id=str(listing_id))
    except:
        raise Http404
    listpictures = list()
    if (request.POST.get("deletephoto") != None
            and request.POST.get("deletephotoyes") == "true"):
        deletephoto = Photo.objects.get(id=request.POST.get("deletephoto"))
        deletephoto.delete()
    if request.method == 'GET':

        if not listing.is_verified():
            if listing and (listing.get_uuid() == str(uuid)):
                listing.mark_verified()
                listing.save()
                message = MESSAGES.get('verified_listing')
            else:
                # the listing_id and uuid provided do not match anything in db correctly
                # so redirect to 404 as this page doesn't exist for this combination
                raise Http404
        #post verified by this point, render edit page with message
        edit_form = EditListingForm(instance=listing)
        try:
            for o in Photo.objects.filter(listing_id=listing_id):
                item = [o.photo.name, o.id]
                listpictures.append(item)
        except:
            listpictures = None
        form_args = {
            'form': edit_form,
            'message': message,
            'submit_action': submit_action,
            'logparams': logparams,
            'listing': listing,
            'listpictures': listpictures,
            'pictureform': pictureform
        }
        return render_to_response(TEMPLATE_PATHS.get("listings_edit"),
                                  form_args,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        edit_form = EditListingForm(request.POST, instance=listing)

        #if post_form valid, process new post
        if edit_form.is_valid():

            listing_url = HttpRequest.build_absolute_uri(
                request, edit_form.cleaned_data.get('url'))
            edit_form.save()
            if form.is_valid():
                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()
            # This redirects back to edit form, should change to a render_to_response with a message that edit successful
            try:
                for o in Photo.objects.filter(listing_id=listing_id):
                    item = [o.photo.name, o.id]
                    listpictures.append(item)
            except:
                listpictures = None
            form_args = {
                'form': edit_form,
                'submit_action': submit_action,
                'message': MESSAGES.get('edit_success'),
                'listing_url': listing_url,
                'logparams': logparams,
                'pictureform': pictureform,
                'listpictures': listpictures
            }
            return render_to_response(TEMPLATE_PATHS.get("listings_edit"),
                                      form_args,
                                      context_instance=RequestContext(request))
        else:

            # if form submission not valid, redirect back to form with error messages
            form_args = {
                'form': edit_form,
                'submit_action': submit_action,
                'message': None,
                'logparams': logparams,
                'listpictures': listpictures,
                'pictureform': pictureform
            }
            return render_to_response(TEMPLATE_PATHS.get("listings_edit"),
                                      form_args,
                                      context_instance=RequestContext(request))

        raise Http404
Esempio n. 3
0
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
Esempio n. 4
0
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