Edit sandwich method added - not completely working yet. Adds a new sandwich instead...
authorKriti Godey <kriti.godey@gmail.com>
Thu, 18 Mar 2010 21:55:51 +0000 (17:55 -0400)
committerKriti Godey <kriti.godey@gmail.com>
Thu, 18 Mar 2010 21:55:51 +0000 (17:55 -0400)
forms.py
templates/allsandwiches.html
templates/editsandwich.html [new file with mode: 0644]
templates/onesandwich.html
urls.py
views.py

index 17ed40e..e471c2a 100644 (file)
--- 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):
index 17c40df..4f3a901 100644 (file)
@@ -3,7 +3,17 @@
 {% block content %}
                {% for s in allsandwiches %}
                        <h3 class="sandwichtitle">{{ s.adjective }}</h3>
-                       <p class="metadata">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 %}</p>
+                       <p class="metadata">Made on {{ s.date_made|date:"F j Y" }} and added by  
+                               {% ifequal s.user request.user %} you. 
+                                       <a href="{% url edit_sandwich slug=s.slug %}"> (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 %}</p>
                        {% if s.picture %}
                                <img class="sandwichimg" src="{{ s.picture.url }}">
                        {% endif %}
diff --git a/templates/editsandwich.html b/templates/editsandwich.html
new file mode 100644 (file)
index 0000000..f70bc85
--- /dev/null
@@ -0,0 +1,41 @@
+{% extends "base.html" %}
+{% block extrahead %}
+<script type="text/javascript" src="{{ media_url }}sandwiches/jquery-1.4.2.min.js"></script>
+<script type="text/javascript" src="{{ media_url }}sandwiches/jquery.tokeninput.js"></script>
+<script type="text/javascript" src="{{ media_url }}sandwiches/date.js"></script>
+<!--[if IE]><script type="text/javascript" src="http://127.0.0.1:8000/sandwiches/jquery.bgiframe.js"></script><![endif]-->
+<script type="text/javascript" src="{{ media_url }}sandwiches/jquery.datePicker.js"></script>
+<link rel="stylesheet" href="{{ media_url }}sandwiches/token-input.css" type="text/css" />
+<link rel="stylesheet" href="{{ media_url }}sandwiches/datePicker.css" type="text/css" />
+<script type="text/javascript">
+$(document).ready(function () {
+  $("#id_ing").tokenInput("/ajaxfun/", {
+       hintText: "Type an ingredient!",
+       noResultsText: "No ingredients!",
+       searchingText: "Searching..."
+});
+});
+</script>
+<script type="text/javascript">
+Date.firstDayOfWeek = 0;
+Date.format = 'yyyy-mm-dd';
+$(function()
+{
+       $('#id_date_made').datePicker({startDate:'1996-01-01'}).val(new Date().asString()).trigger('change');
+});
+</script>
+{% endblock %}
+{% block title %}Edit sandwich{% endblock %}
+{% block content %}
+       <h1 class="pagetitle">Edit sandwich</h1>
+       <span class="formspan">
+       <form enctype="multipart/form-data" action="{% url add_sandwich %}" method="post" class="cssform">
+       {{ sform.as_p }}
+       {% if s.picture %}
+               <img src="{{ s.picture.url }}" width=400px vspace=10px>
+       {% endif %}
+       <p><label for="id_ing">Ingredients:</label> <input id="id_ing" type="text" name="ing"/></p>
+       <input type="submit" value="Submit" />
+       </form>
+       </span>
+{% endblock %}
\ No newline at end of file
index e1654fa..d88cfbd 100644 (file)
@@ -4,7 +4,17 @@
 
 {% block content %}
        <h3 class="sandwichtitle">{{ s.adjective }}</h3>
-       <p class="metadata">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 %}</p>
+       <p class="metadata">Made on {{ s.date_made|date:"F j Y" }} and added by  
+               {% ifequal s.user request.user %} you. 
+                       <a href="{% url edit_sandwich slug=s.slug %}"> (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 %}</p>
        {% if s.picture %}
                <img class="sandwichimg" src="{{ s.picture.url }}">
        {% endif %}
diff --git a/urls.py b/urls.py
index 9eefa92..2f8f17e 100644 (file)
--- 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<year>[-\w]+)/(?P<month>[-\w]+)/$', views.sandwich_month, name='sandwich_by_month'),
+       url(r'^sandwich/(?P<slug>[-\w]+)/edit/', views.edit_sandwich, name='edit_sandwich'),
        url(r'^sandwich/(?P<slug>[-\w]+)/$', views.specific_sandwich, name='sandwich_by_slug'),
+       url(r'^sandwich/(?P<year>[-\w]+)/(?P<month>[-\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'),
index d653118..554fbac 100644 (file)
--- 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():