Scott Nixon avatar Scott Nixon committed 7bcaa1b Draft

Updated api to tastypie. Fixed the Contact form.

Comments (0)

Files changed (6)

article/api/views.py

+from tastypie.resources import ModelResource
+from tastypie import fields, utils
+
+from article.models import Recipe, Ingredient, RecipeIngredient, Measurement
+
+
+class RecipeResource(ModelResource):
+    ingredients = fields.ToManyField('article.api.views.RecipeIngredientResource', 'recipe_ingred', related_name='recipeingredient', full=True)
+
+    class Meta:
+        queryset = Recipe.active.select_related()
+        resource_name = 'recipe'
+
+class IngredientResource(ModelResource):
+    class Meta:
+        queryset = Ingredient.objects.all()
+
+class RecipeIngredientResource(ModelResource):
+    ingred_name = fields.ToOneField('article.api.views.IngredientResource', 'ri_ingred', full=True)
+    measurement = fields.ToOneField('article.api.views.MeasurementResource', 'ri_measurement', full=True)
+
+    class Meta:
+        queryset = RecipeIngredient.objects.all()
+        resource_name = 'recipeingredient'
+        excludes = ['ri_date']
+
+class MeasurementResource(ModelResource):
+    class Meta:
+        queryset = Measurement.objects.all()
+
         model = get_model('article', 'recipe')   
 
 class FlatPagesAdminModelForm(FlatpageForm):
-    content = forms.CharField(widget=WYMEditor())
+    #content = forms.CharField(widget=WYMEditor())
 
     class Meta:
         model = FlatPage
 
     class Meta:
         model = get_model('article', 'recipephotos')
+
+class ContactForm(forms.Form):
+    name = forms.CharField()
+    email = forms.EmailField()
+    message = forms.CharField(widget=forms.Textarea(attrs={'class':'span5'}))

article/models.py

     if obj.is_published and obj.published_on is None:
         obj.published_on = datetime.now()
 
+def post_tag_check(sender, **kwargs):
+    obj = kwargs['instance']
+    if obj.post_tags:
+        from string import strip
+        obj.post_tags = strip(obj.post_tags)
+
+def recipe_tag_check(sender, **kwargs):
+    obj = kwargs['instance']
+    if obj.rec_tags:
+        from string import strip
+        obj.rec_tags = strip(obj.rec_tags)
+
 pre_save.connect(pub_date_check, sender=Post)
+pre_save.connect(post_tag_check, sender=Post)
 pre_save.connect(pub_date_check, sender=Recipe)
+pre_save.connect(recipe_tag_check, sender=Recipe)
 
 try:
     tagging.register(Post)

article/templates/contact.html

 {% block extra_head %}
 {% endblock %}
 
-{% block head_title %} {{ flatpage.title }} {% endblock %}
+{% block head_title %} Contact Form {% endblock %}
 
 {% block body %}   
 
       <div class="span15">
         <ul class="breadc clearfix">
           <li><a href="/">Home</a></li>
-          <li><a href="/contact/">{{ flatpage.title }}</a></li>
+          <li><a href="{% url contact %}">Contact</a></li>
         </ul>
-        <h1 class="page-title">{{ flatpage.title }}</h1>
+        <h1 class="page-title">Contact</h1>
       </div>
     </div>
   </div>
   <div class="row">
     <div class="wrapper-box">
       <div class="span7">
-	    <div>
-		  <script type="text/javascript">var host = (("https:" == document.location.protocol) ? "https://secure." : "http://");document.write(unescape("%3Cscript src='" + host + "wufoo.com/scripts/embed/form.js' type='text/javascript'%3E%3C/script%3E"));</script>
+        {% if success %}
+            <span class="btn btn-success"><i class="icon-ok icon-white"></i> Your message has been sent!</span>
+        {% else %}
+	    <div><style>label {color: #000;}</style>
+          <form action="{% url contact %}" method="post">
+            {% csrf_token %}
+            {{ form.as_p }}
+            <input type="submit" value="Send Email" />
+          </form>
+        {% endif %}
 
-          <script type="text/javascript">
-            var z7x3k7 = new WufooForm();
-            z7x3k7.initialize({
-            'userName':'happyherbivore', 
-            'formHash':'z7x3k7', 
-            'autoResize':true,
-            'height':'557'});
-            z7x3k7.display();
-          </script>
         </div>
       </div>
     
     url(r'^topic/(?P<category>[-\w]+)/$', 'post_category_index', name="category"),
     url(r'^get-started/$', TemplateView.as_view(template_name='new_here.html'), name="new_here"),
     url(r'^video/$', 'video_index'),
-    url(r'^join/$', 'mc_signup'),                       
+    url(r'^join/$', 'mc_signup'),
+    url(r'^about/contact/$', 'contact', name='contact'),
 )
 from django.contrib.sessions.models import Session
 from django.views.decorators.csrf import csrf_exempt
 from django.views.generic.dates import ArchiveIndexView, MonthArchiveView, YearArchiveView
+from django.core.mail import send_mail
 
 from tagging.models import TaggedItem, Tag
 from mailsnake import MailSnake
 from article.models import Post, Recipe, Ingredient
 from article.models import RecipeIngredient, RecipePhotos, UserPhotos
 from community.models import UserProfile, Transaction
+from article.forms import ContactForm
 
 
 def main_index(request):
 
     else:
         return HttpResponse("Your request is not valid for MailChimp signup.")
+
+def contact(request):
+    if request.method == 'POST':
+        form = ContactForm(request.POST) 
+        if form.is_valid():
+            send_mail('Contact Form email from {0}'.format(request.POST['name']), request.POST['message'], request.POST['email'], ['lindsay@happyherbivore.com'], fail_silently=False)
+            success = True
+    else:
+        form = ContactForm()
+
+    return render_to_response('contact.html', locals(), context_instance=RequestContext(request))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.