class Meta:
model = Sandwich
exclude = ('slug', 'user', 'ingredients')
+ fields = ('adjective', 'date_made', 'notes', 'picture')
class IngredientForm(ModelForm):
{% 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 %}
--- /dev/null
+{% 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
{% 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 %}
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'),
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():