From eb29f2a3fc433fc4dfd510ddb3e20ce03aca80b0 Mon Sep 17 00:00:00 2001 From: Kriti Godey Date: Thu, 18 Mar 2010 17:55:51 -0400 Subject: [PATCH] Edit sandwich method added - not completely working yet. Adds a new sandwich instead of editing, and the ingredients don't transfer --- forms.py | 1 + templates/allsandwiches.html | 12 ++++++++++- templates/editsandwich.html | 41 ++++++++++++++++++++++++++++++++++++ templates/onesandwich.html | 12 ++++++++++- urls.py | 3 ++- views.py | 35 ++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 templates/editsandwich.html diff --git a/forms.py b/forms.py index 17ed40e..e471c2a 100644 --- a/forms.py +++ b/forms.py @@ -9,6 +9,7 @@ class SandwichForm(ModelForm): class Meta: model = Sandwich exclude = ('slug', 'user', 'ingredients') + fields = ('adjective', 'date_made', 'notes', 'picture') class IngredientForm(ModelForm): diff --git a/templates/allsandwiches.html b/templates/allsandwiches.html index 17c40df..4f3a901 100644 --- a/templates/allsandwiches.html +++ b/templates/allsandwiches.html @@ -3,7 +3,17 @@ {% block content %} {% for s in allsandwiches %}

{{ s.adjective }}

-

Made on {{ s.date_made|date:"F j Y" }} and added by {% if s.user.first_name %} {{ s.user.first_name }} {% if s.user.last_name %} {{ s.user.last_name }} {% endif %} {% else %} {{ s.user.username }}. {% endif %}

+

Made on {{ s.date_made|date:"F j Y" }} and added by + {% ifequal s.user request.user %} you. + (Edit) + {% else %} + {% if s.user.first_name %} {{ s.user.first_name }} + {% if s.user.last_name %} {{ s.user.last_name }} + {% endif %} + {% else %} + {{ s.user.username }}. + {% endif %} + {% endifequal %}

{% if s.picture %} {% endif %} diff --git a/templates/editsandwich.html b/templates/editsandwich.html new file mode 100644 index 0000000..f70bc85 --- /dev/null +++ b/templates/editsandwich.html @@ -0,0 +1,41 @@ +{% extends "base.html" %} +{% block extrahead %} + + + + + + + + + +{% endblock %} +{% block title %}Edit sandwich{% endblock %} +{% block content %} +

Edit sandwich

+ +
+ {{ sform.as_p }} + {% if s.picture %} + + {% endif %} +

+ +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/onesandwich.html b/templates/onesandwich.html index e1654fa..d88cfbd 100644 --- a/templates/onesandwich.html +++ b/templates/onesandwich.html @@ -4,7 +4,17 @@ {% block content %}

{{ s.adjective }}

- +

Made on {{ s.date_made|date:"F j Y" }} and added by + {% ifequal s.user request.user %} you. + (Edit) + {% else %} + {% if s.user.first_name %} {{ s.user.first_name }} + {% if s.user.last_name %} {{ s.user.last_name }} + {% endif %} + {% else %} + {{ s.user.username }}. + {% endif %} + {% endifequal %}

{% if s.picture %} {% endif %} diff --git a/urls.py b/urls.py index 9eefa92..2f8f17e 100644 --- a/urls.py +++ b/urls.py @@ -6,8 +6,9 @@ urlpatterns = patterns('', url(r'^sandwich/add/$', views.add_sandwich, name='add_sandwich'), url(r'^sandwich/addingredient/$', views.add_ingredient, name='add_ingredient'), url(r'^sandwich/all/$', views.all_sandwich, name='all_sandwiches'), - url(r'^sandwich/(?P[-\w]+)/(?P[-\w]+)/$', views.sandwich_month, name='sandwich_by_month'), + url(r'^sandwich/(?P[-\w]+)/edit/', views.edit_sandwich, name='edit_sandwich'), url(r'^sandwich/(?P[-\w]+)/$', views.specific_sandwich, name='sandwich_by_slug'), + url(r'^sandwich/(?P[-\w]+)/(?P[-\w]+)/$', views.sandwich_month, name='sandwich_by_month'), url(r'^login/$', views.login_view, name='login'), url(r'^login2/$', views.login_view2, name='login2'), url(r'^logout/$', views.logout_view, name='logout'), diff --git a/views.py b/views.py index d653118..554fbac 100644 --- a/views.py +++ b/views.py @@ -51,6 +51,41 @@ def add_sandwich(request): return render_to_response('sandwich.html', {'sform': form,}, context_instance=RequestContext(request)) else: return HttpResponseRedirect(reverse('login2')) + +def edit_sandwich(request, slug): + sedit = Sandwich.objects.get(slug=slug) + if sedit.picture: + savedpicture = sedit.picture.url + if request.user.is_authenticated(): + if not sedit.user == request.user: + return HttpResponseRedirect(reverse('all_sandwiches')) + else: + if request.method == 'POST': + sform = SandwichForm(request.POST, request.FILES, instance=sedit) + if sform.is_valid(): # All validation rules pass + sform.save() + x = request.POST['ing'] + x = x.strip() + y = x.split(',') + for n in y: + if n.isdigit(): + sedit.ingredients.add(Ingredient.objects.get(id=n)) + else: + n = n.lstrip('new:') + newingredient = Ingredient(name=n, slug=SlugifyUniquely(n, Ingredient)) + newingredient.save() + sedit.ingredients.add(newingredient) + if not sedit.picture: + if savedpicture: + sedit.picture = savedpicture + sedit.slug = slug + sedit.save() + return HttpResponseRedirect(sedit.get_absolute_url()) + else: + sform = SandwichForm(instance=sedit) + return render_to_response('editsandwich.html', {'sform': sform, 's':sedit,}, context_instance=RequestContext(request)) + else: + return HttpResponseRedirect(reverse('login2')) def add_ingredient(request): if request.user.is_authenticated(): -- 2.20.1