Ejemplo n.º 1
0
def add_place(request, slug):
    city = get_object_or_404(City, slug=slug)

    if request.method == "POST":
        form = AddPlaceForm(request.POST)
        if form.is_valid():
            laowai = get_object_or_404(Laowai, user=request.user)
            slug = get_slugify(form.cleaned_data["name"])
            while Place.objects.filter(slug__iexact=slug).count():
                current_number_suffix_match = re.search("\d+$", slug)
                current_number_suffix = current_number_suffix_match and current_number_suffix_match.group() or 0
                next = str(int(current_number_suffix) + 1)
                slug = re.sub("(\d+)?$", next, slug)

            if not form.cleaned_data["longitude"]:
                longitude = None
            else:
                longitude = form.cleaned_data["longitude"]

            if not form.cleaned_data["latitude"]:
                latitude = None
            else:
                latitude = form.cleaned_data["latitude"]

            creation_args = {
                "name": form.cleaned_data["name"],
                "chinese_name": form.cleaned_data["chinese_name"],
                "description": form.cleaned_data["description"],
                "location": form.cleaned_data["location"],
                "city": form.cleaned_data["city"],
                "owner": laowai,
                "slug": slug,
                "category": form.cleaned_data["category"],
                "longitude": longitude,
                "latitude": latitude,
            }

            place = NewPlace.objects.create(**creation_args)

            message = '%s has been added! You can <a href="%s">view it here</a>' % (
                place.name,
                place.get_absolute_url(),
            )
            set_message(request, message)

            redirect_url = reverse("places", args=[city.slug])
            return HttpResponseRedirect(redirect_url)

        # if errors...
        else:
            form = AddPlaceForm(request.POST)
            return render(request, "places/forms/add_place.html", locals())
    else:
        form = AddPlaceForm()

    return render(request, "places/forms/add_place.html", locals())
Ejemplo n.º 2
0
def add_blog(request, slug):
    city = get_object_or_404(City, slug=slug)
    
    if request.method == 'POST':
        form = BlogAddForm(request.POST)
        if form.is_valid():
            
            # check that this doesn't already exist...
            if Blog.objects.filter(url__icontains=form.cleaned_data['url']):
                pass
            
            else:
                newblog = Blog.objects.create(
                    url=form.cleaned_data['url'],
                    city=city,
                )
                
                newblog.feed = form.cleaned_data['feed']
                newblog.name = form.cleaned_data['name']
                
                newblog.save()
            
            message = "%s has been submitted to the %s blog roll and is awaiting approval. Thanks!" % (newblog.url, newblog.city)
            set_message(request, message)
            
            url = reverse('news_feed', args=[city.slug])
            return HttpResponseRedirect(url)
        
    else:
        form = BlogAddForm()
    

    return render(request, "cities/forms/add_blog_form.html", locals())