Commits

powersurge360  committed 184dd3e

Added adding photos.

  • Participants
  • Parent commits 4899d38

Comments (0)

Files changed (7)

File share_search/reviews/forms.py

 from django import forms
 
-from reviews.models import Store
+from reviews.models import Store, PhotoModel
 
 
 class StoreForm(forms.ModelForm):
     class Meta:
         model = Store
         fields = ("name", "description", "image")
+
+
+class PhotoForm(forms.ModelForm):
+    class Meta:
+        model = PhotoModel
+        fields = ("description", "image", "store")
+
+        widgets = {
+            "store": forms.HiddenInput()
+        }

File share_search/reviews/templates/reviews/photomodel_form.html

+{% load crispy_forms_tags %}
+{{ form|crispy }}

File share_search/reviews/templates/reviews/store_detail.html

 {% extends "_layouts/base.html" %}
 {% load gridify %}
+{% load crispy_forms_tags %}
 
 {% block title %}{{ block.super }} - {{ object.name }}{% endblock %}
 
         </div>
     </div>
     <div class="row">
-        <h1 class="span12">Photos</h1>
+        <h1 class="span12">Photo</h1>
+    </div>
+    <div class="row">
+        <h3 class="span12">
+            {% if user.is_authenticated %}<a href="#add-photo" data-toggle="modal">Add a photo</a>{% else %}<a href="{% url "site:register" %}">Register to add a photo.</a>{% endif %}
+        </h3>
     </div>
     <div class="row">
         <div class="span12">
-            {% if object.photos %}
+            {% if object.photos.count %}
                 <div class="gallery">
+                    {% for photo in object.photos.all %}
+                        {{ photo }}
+                    {% endfor %}
                 </div>
             {% else %}
-                <h3>No photos. {% if user.is_authenticated %}Care to <a href="#">add one?</a>{% else %}<a href="{% url "site:register" %}">Register to add one.</a>{% endif %}</h3>
+                <h3>No photos. {% if user.is_authenticated %}Care to <a href="#add-photo" data-toggle="modal">add one?</a>{% else %}<a href="{% url "site:register" %}">Register to add one.</a>{% endif %}</h3>
             {% endif %}
         </div>
     </div>
+    <div class="modal hide" id="add-photo">
+        <div class="modal-header">
+            <h2>Add a Photo</h2>
+        </div>
+        <div class="modal-body">
+            <form enctype="multipart/form-data" method="post" action="{% url "reviews:photo_create" %}">
+                {% csrf_token %}
+                {{ photo_form|crispy }}
+                <input type="hidden" name="store" value="{{ object.id }}">
+            </form>
+        </div>
+        <div class="modal-footer">
+            <button class="btn" data-dismiss="modal">Nevermind</button>
+            <button class="btn btn-primary submit-button" data-dismiss="modal">Add Photo</button>
+        </div>
+    </div>
 {% endblock %}

File share_search/reviews/urls.py

 from django.views.generic import TemplateView
 
 from reviews.views import (StoreDetailView, StoreListView,
-                           StoreUpdateView,
+                           StoreUpdateView, PhotoModelCreateView,
                            ProductDetailView, StoreCreateView)
 
 
         ProductDetailView.as_view(),
         name="product",
     ),
+    url(
+        r'^photos/create/$',
+        PhotoModelCreateView.as_view(),
+        name="photo_create"
+    ),
 )

File share_search/reviews/views.py

 from braces.views import LoginRequiredMixin
+from django.core.urlresolvers import reverse
 from django.views.generic import DetailView, ListView, CreateView, UpdateView
 
-from reviews.forms import StoreForm
-from reviews.models import Store, Product
+from reviews.forms import StoreForm, PhotoForm
+from reviews.models import Store, Product, PhotoModel
 
 
 class StoreDetailView(DetailView):
     model = Store
 
+    def get_context_data(self, *args, **kwargs):
+        context = super(StoreDetailView, self).get_context_data(
+            *args,
+            **kwargs
+        )
+
+        context["photo_form"] = PhotoForm()
+        return context
+
 
 class StoreListView(ListView):
     model = Store
 
 class ProductListView(ListView):
     model = Product
+
+
+class PhotoModelCreateView(LoginRequiredMixin, CreateView):
+    model = PhotoModel
+    form_class = PhotoForm
+
+    def get_success_url(self, *args, **kwargs):
+        return reverse("reviews:store", args=(self.object.store.pk,))

File share_search/staticfiles/js/app.js

+$(function() {
+    "use strict";
+
+    $(".modal .submit-button").on("click", function() {
+        var $this = $(this);
+
+        $this.closest(".modal").find("form").submit();
+    });
+});

File share_search/templates/_layouts/base.html

         <script src="{{ STATIC_URL }}js/jquery-1.9.1.min.js"></script>
         <script src="{{ STATIC_URL }}js/jquery.masonry.min.js"></script>
         <script src="{{ STATIC_URL }}bootstrap/js/bootstrap.min.js"></script>
+        <script src="{{ STATIC_URL }}js/app.js"></script>
     </head>
     <body>
         <div class="container">