Permalink
Browse files

chapter 11

  • Loading branch information...
1 parent 6add201 commit 32d879c175a11ee6ffac6397f15431bd13d6e19d @ButuzGOL committed Apr 21, 2010
View
@@ -1,5 +1,5 @@
from django.contrib import admin
-from ecomstore.catalog.models import Product, Category
+from ecomstore.catalog.models import Product, Category, ProductReview
from ecomstore.catalog.forms import ProductAdminForm
class ProductAdmin(admin.ModelAdmin):
@@ -30,3 +30,10 @@ class CategoryAdmin(admin.ModelAdmin):
admin.site.register(Category, CategoryAdmin)
+class ProductReviewAdmin(admin.ModelAdmin):
+ list_display = ('product', 'user', 'title', 'date', 'rating', 'is_approved')
+ list_per_page = 20
+ list_filter = ('product', 'user', 'is_approved')
+ ordering = ['date']
+ search_fields = ['user','content','title']
+admin.site.register(ProductReview, ProductReviewAdmin)
View
@@ -1,5 +1,5 @@
from django import forms
-from ecomstore.catalog.models import Product
+from ecomstore.catalog.models import Product, ProductReview
class ProductAdminForm(forms.ModelForm):
class Meta:
@@ -9,3 +9,8 @@ def clean_price(self):
if self.cleaned_data['price'] <= 0:
raise forms.ValidationError('Price must be greater than zero.')
return self.cleaned_data['price']
+
+class ProductReviewForm(forms.ModelForm):
+ class Meta:
+ model = ProductReview
+ exclude = ('user', 'product', 'is_approved')
View
@@ -1,4 +1,5 @@
from django.db import models
+from django.contrib.auth.models import User
class ActiveCategoryManager(models.Manager):
def get_query_set(self):
@@ -117,3 +118,28 @@ def cross_sells_hybrid(self):
).exclude(product=self)
products = Product.active.filter(orderitem__in=items).distinct()
return products
+
+class ActiveProductReviewManager(models.Manager):
+ def all(self):
+ return super(ActiveProductReviewManager, self).all() \
+ .filter(is_approved=True)
+
+class ProductReview(models.Model):
+ RATINGS = ((5,5),(4,4),(3,3),(2,2),(1,1),)
+ product = models.ForeignKey(Product)
+ user = models.ForeignKey(User)
+ title = models.CharField(max_length=50)
+ date = models.DateTimeField(auto_now_add=True)
+ rating = models.PositiveSmallIntegerField(default=5, choices=RATINGS)
+ is_approved = models.BooleanField(default=True)
+ content = models.TextField()
+
+ objects = models.Manager()
+ approved = ActiveProductReviewManager()
+
+import tagging
+# Product model class definition here
+try:
+ tagging.register(Product)
+except tagging.AlreadyRegistered:
+ pass
View
@@ -8,4 +8,11 @@
(r'^product/(?P<product_slug>[-\w]+)/$',
'show_product', {'template_name':'catalog/product.html'},
'catalog_product'),
+ (r'^review/product/add/$', 'add_review'),
+ (r'^tag/product/add/$', 'add_tag'),
+ (r'^tag_cloud/$', 'tag_cloud', {'template_name': 'catalog/tag_cloud.html'},
+ 'tag_cloud'),
+ (r'^tag/(?P<tag>[-\w]+)/$', 'tag', {'template_name': 'catalog/tag.html'},
+ 'tag'),
+
)
View
@@ -1,6 +1,7 @@
from django.shortcuts import get_object_or_404, render_to_response
-from ecomstore.catalog.models import Category, Product
+from ecomstore.catalog.models import Category, Product, ProductReview
from django.template import RequestContext
+from ecomstore.catalog.forms import ProductReviewForm
# this stuff goes at the top of the file, below other imports
from django.core import urlresolvers
@@ -59,5 +60,67 @@ def show_product(request, product_slug, template_name="catalog/product.html"):
form.fields['product_slug'].widget.attrs['value'] = product_slug
# set the test cookie on our first GET request
request.session.set_test_cookie()
+ product_reviews = ProductReview.approved.filter(product=p).order_by('-date')
+ review_form = ProductReviewForm()
return render_to_response("catalog/product.html", locals(),
- context_instance=RequestContext(request))
+ context_instance=RequestContext(request))
+
+from django.contrib.auth.decorators import login_required
+from django.template.loader import render_to_string
+from django.utils import simplejson
+from django.http import HttpResponse
+
+@login_required
+def add_review(request):
+ form = ProductReviewForm(request.POST)
+ if form.is_valid():
+ review = form.save(commit=False)
+ slug = request.POST.get('slug')
+ product = Product.active.get(slug=slug)
+ review.user = request.user
+ review.product = product
+ review.save()
+ template = "catalog/product_review.html"
+ html = render_to_string(template, {'review': review })
+ response = simplejson.dumps({'success':'True', 'html': html})
+ else:
+ html = form.errors.as_ul()
+ response = simplejson.dumps({'success':'False', 'html': html})
+ return HttpResponse(response,
+ content_type='application/javascript; charset=utf-8')
+
+
+import tagging
+from tagging.models import Tag, TaggedItem
+
+@login_required
+def add_tag(request):
+ tags = request.POST.get('tag', '')
+ slug = request.POST.get('slug', '')
+ if len(tags) > 2:
+ p = Product.active.get(slug=slug)
+ html = u''
+ template = "catalog/tag_link.html"
+ for tag in tags.split():
+ tag.strip(',')
+ Tag.objects.add_tag(p, tag)
+ for tag in p.tags:
+ html += render_to_string(template, {'tag': tag })
+ response = simplejson.dumps({'success':'True', 'html': html })
+ else:
+ response = simplejson.dumps({'success':'False'})
+ return HttpResponse(response,
+ content_type='application/javascript; charset=utf8')
+
+def tag_cloud(request, template_name="catalog/tag_cloud.html"):
+ product_tags = Tag.objects.cloud_for_model(Product, steps=9,
+ distribution=tagging.utils.LOGARITHMIC,
+ filters={ 'is_active': True })
+ page_title = 'Product Tag Cloud'
+ return render_to_response(template_name, locals(),
+ context_instance=RequestContext(request))
+
+def tag(request, tag, template_name="catalog/tag.html"):
+ products = TaggedItem.objects.get_by_model(Product.active, tag)
+ return render_to_response(template_name, locals(),
+ context_instance=RequestContext(request))
View
@@ -91,6 +91,7 @@
'ecomstore.accounts',
'ecomstore.search',
'ecomstore.stats',
+ 'tagging',
'djangodblog',
)
View
@@ -194,4 +194,43 @@ fieldset.checkout input {
table#receipt{
width:100%;
border-collapse:collapse;
-}
+}
+#add_review{
+ cursor:pointer;
+}
+.hidden{display:none;}
+.visible{display:block;}
+.new_review{background-color:Pink;}
+
+#tags a{
+ color:DarkOrange;
+}
+#loading{
+ display:none;
+ position:fixed;
+ top:0;
+ left:45%;
+ background-color:Pink;
+ width: 200px;
+ font-size:medium;
+ color:Black;
+ font-weight:bold;
+ padding:2px;
+ border:solid 1px Red;
+ text-align: center;
+ z-index:100;
+}
+#noscript{
+ position:fixed;
+ top:0;left:0;
+ width:100%;
+ background-color:Red;
+ color:White;
+ font-weight:bold;
+ padding:5px;
+ text-align:center;
+}
+#noscript_padder{
+ height:30px;
+}
+
Oops, something went wrong.

0 comments on commit 32d879c

Please sign in to comment.