Exemplo n.º 1
0
def create(request):

    """
	Combines an advertForm and a keywordForm to
	collect input from the user about each new advert.
	Processes the comma separated list of keywords
	to create advertkeyword objects.
	Creates objects from the advertclick and
	advertimpression models
	"""

    if request.method == "POST":

        advertForm = AdvertForm(request.POST, request=request)
        keywordForm = KeywordForm(request.POST)

        if advertForm.is_valid() and keywordForm.is_valid():

            advert = advertForm.save(commit=False)

            advert.user = request.user
            advert.save()

            keywords = keywordForm.cleaned_data["keyword"]

            temp = keywords.split(",")
            keywordList = []
            for t in temp:
                keywordList.append(t)

            for keyword in keywordList:
                temp = AdvertKeyword(advert=advert, keyword=keyword)
                temp.save()

            ad_i = AdvertImpression(advert=advert)
            ad_i.save()

            ad_c = AdvertClick(advert=advert)
            ad_c.save()

            return redirect("advert.views.list")
            # Not a POST request - render empty forms
    else:

        advertForm = AdvertForm()
        keywordForm = KeywordForm()

    return render(request, "advert/add.html", {"advertForm": advertForm, "keywordForm": keywordForm})
Exemplo n.º 2
0
def edit(request, advert_id=None):

    """
	Provides the functionality of editing and advert.
	Processes the submitted forms and updates the required fields.
	"""

    if advert_id:

        advert = get_object_or_404(Advert, pk=advert_id, user=request.user)

        list = []
        keywords = AdvertKeyword.objects.filter(advert=advert).order_by("pk")

        for keyword in keywords:
            list.append(keyword.keyword)

        string_list = ",".join(list)

        if request.method == "POST":
            advertForm = AdvertForm(request.POST, instance=advert, request=request, edit=True, advert_id=advert.pk)
            keywordForm = KeywordForm(request.POST)

            if advertForm.is_valid() and keywordForm.is_valid():
                advert = advertForm.save()

                AdvertKeyword.objects.filter(advert=advert).delete()

                keywords = keywordForm.cleaned_data["keyword"]
                temp = keywords.split(",")
                keywordList = []
                for t in temp:
                    keywordList.append(t.strip())

                for keyword in keywordList:
                    temp = AdvertKeyword(advert=advert, keyword=keyword)
                    temp.save()

                return redirect("advert.views.list")

        else:
            advertForm = AdvertForm(instance=advert, request=request)
            keywordForm = KeywordForm(initial={"keyword": string_list})

        cd = {"advertForm": advertForm, "keywordForm": keywordForm, "advert": advert, "keywords": string_list}

        return render(request, "advert/edit.html", cd)