# Path: catalog/urls.py
# def catalog_url(url_string, view, name, extra_url=None):
#     """
#     Url wrapper function that gets an url attribute from settings
#     if it exists and prepends it to the url. If it doesn't exist, the
#     given string is used as a base url. It also prefixes the name with
#     the string 'catalog_'.
#     """
#     url_string = getattr(scs, '{}_URL'.format(url_string.upper()), url_string)
#     url_string = slugify(url_string)
# 
#     if extra_url is not None:
#         url_string = '{}/{}'.format(url_string, extra_url)
#     url_string = url_string.strip('/')
# 
#     if url_string:
#         url_string = '{}/'.format(url_string)
#     url_string = r'^{}$'.format(url_string)
# 
#     return url(url_string, view, name='catalog_{}'.format(name))
# 
# Path: catalog/reviews/views.py
# class ReviewListView(ProductMixin, ListView):
#     model = Review
#     template_name = 'shop/reviews/review_list.html'
# 
#     def get_queryset(self):
#         qs = super(ReviewListView, self).get_queryset()
#         return qs.filter(product=self.product)
# 
# class ReviewDetailView(ProductMixin, DetailView):
#     model = Review
#     template_name = 'shop/reviews/review_detail.html'
# 
# class ReviewCreateView(ProductMixin, CreateView):
#     model = Review
#     fields = ['body', 'rating']
#     template_name = 'shop/reviews/review_create.html'
# 
#     def get(self, request, *args, **kwargs):
#         """
#         If review already written for this user/session redirect to
#         update view for that review.
#         """
#         user = getattr(request, 'user', None)
#         product = get_by_slug_or_404(Product, kwargs.get('slug', None))
#         filters = {'product': product}
# 
#         if user and user.is_authenticated():
#             filters['user'] = user
#         else:
#             filters['session_key'] = request.session.session_key
# 
#         if len(filters) > 1:
#             try:
#                 review = Review.objects.get(**filters)
#                 return HttpResponseRedirect(reverse_lazy(
#                     'catalog_review_update',
#                     args=[product.get_slug(), review.pk]))
#             except Review.DoesNotExist:
#                 pass
# 
#         return super(ReviewCreateView, self).get(request, *args, **kwargs)
# 
#     def form_valid(self, form):
#         """
#         Force product and user values into a form after it's validated
#         and save review_id to session.
#         """
#         product = get_by_slug_or_404(Product, self.kwargs.get('slug', None))
#         form.instance.product = product
# 
#         user = getattr(self.request, 'user', None)
#         form.instance.user = user if user and user.is_authenticated() else None
#         form.instance.session_key = self.request.session.session_key
# 
#         return super(ReviewCreateView, self).form_valid(form)
# 
# class ReviewUpdateView(OwnerRequiredMixin, ProductMixin, UpdateView):
#     model = Review
#     fields = ['body', 'rating']
#     template_name = 'shop/reviews/review_update.html'
# 
# class ReviewDeleteView(OwnerRequiredMixin, ProductMixin, DeleteView):
#     model = Review
#     template_name = 'shop/reviews/review_delete.html'
# 
#     def get_success_url(self):
#         return reverse_lazy(
#             'catalog_review_list', args=[self.object.product.get_slug()])


from catalog.urls import catalog_url
from catalog.reviews.views import (
    ReviewListView, ReviewDetailView, ReviewCreateView, ReviewUpdateView,
    ReviewDeleteView)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals



# Pats are automatically inserted into urlpatterns in catalog urls.py
# so urlpatterns are not specified here.
pats = [
    catalog_url('product', ReviewListView.as_view(),
                'review_list',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/'),

    catalog_url('product', ReviewDetailView.as_view(),
                'review_detail',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/(?P<pk>[\d+])/'),

    catalog_url('product', ReviewCreateView.as_view(),
                'review_create',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/create/'),


# Path: catalog/urls.py
# def catalog_url(url_string, view, name, extra_url=None):
#     """
#     Url wrapper function that gets an url attribute from settings
#     if it exists and prepends it to the url. If it doesn't exist, the
#     given string is used as a base url. It also prefixes the name with
#     the string 'catalog_'.
#     """
#     url_string = getattr(scs, '{}_URL'.format(url_string.upper()), url_string)
#     url_string = slugify(url_string)
# 
#     if extra_url is not None:
#         url_string = '{}/{}'.format(url_string, extra_url)
#     url_string = url_string.strip('/')
# 
#     if url_string:
#         url_string = '{}/'.format(url_string)
#     url_string = r'^{}$'.format(url_string)
# 
#     return url(url_string, view, name='catalog_{}'.format(name))
# 
# Path: catalog/reviews/views.py
# class ReviewListView(ProductMixin, ListView):
#     model = Review
#     template_name = 'shop/reviews/review_list.html'
# 
#     def get_queryset(self):
#         qs = super(ReviewListView, self).get_queryset()
#         return qs.filter(product=self.product)
# 
# class ReviewDetailView(ProductMixin, DetailView):
#     model = Review
#     template_name = 'shop/reviews/review_detail.html'
# 
# class ReviewCreateView(ProductMixin, CreateView):
#     model = Review
#     fields = ['body', 'rating']
#     template_name = 'shop/reviews/review_create.html'
# 
#     def get(self, request, *args, **kwargs):
#         """
#         If review already written for this user/session redirect to
#         update view for that review.
#         """
#         user = getattr(request, 'user', None)
#         product = get_by_slug_or_404(Product, kwargs.get('slug', None))
#         filters = {'product': product}
# 
#         if user and user.is_authenticated():
#             filters['user'] = user
#         else:
#             filters['session_key'] = request.session.session_key
# 
#         if len(filters) > 1:
#             try:
#                 review = Review.objects.get(**filters)
#                 return HttpResponseRedirect(reverse_lazy(
#                     'catalog_review_update',
#                     args=[product.get_slug(), review.pk]))
#             except Review.DoesNotExist:
#                 pass
# 
#         return super(ReviewCreateView, self).get(request, *args, **kwargs)
# 
#     def form_valid(self, form):
#         """
#         Force product and user values into a form after it's validated
#         and save review_id to session.
#         """
#         product = get_by_slug_or_404(Product, self.kwargs.get('slug', None))
#         form.instance.product = product
# 
#         user = getattr(self.request, 'user', None)
#         form.instance.user = user if user and user.is_authenticated() else None
#         form.instance.session_key = self.request.session.session_key
# 
#         return super(ReviewCreateView, self).form_valid(form)
# 
# class ReviewUpdateView(OwnerRequiredMixin, ProductMixin, UpdateView):
#     model = Review
#     fields = ['body', 'rating']
#     template_name = 'shop/reviews/review_update.html'
# 
# class ReviewDeleteView(OwnerRequiredMixin, ProductMixin, DeleteView):
#     model = Review
#     template_name = 'shop/reviews/review_delete.html'
# 
#     def get_success_url(self):
#         return reverse_lazy(
#             'catalog_review_list', args=[self.object.product.get_slug()])


# Path: catalog/reviews/urls.py
from catalog.urls import catalog_url
from catalog.reviews.views import (
    ReviewListView, ReviewDetailView, ReviewCreateView, ReviewUpdateView,
    ReviewDeleteView)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals



# Pats are automatically inserted into urlpatterns in catalog urls.py
# so urlpatterns are not specified here.
pats = [
    catalog_url('product', ReviewListView.as_view(),
                'review_list',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/'),

    catalog_url('product', ReviewDetailView.as_view(),
                'review_detail',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/(?P<pk>[\d+])/'),

    catalog_url('product', ReviewCreateView.as_view(),
                'review_create',
                '(?P<slug>[0-9A-Za-z-_.//]+)/reviews/create/'),


    catalog_url('product', ReviewUpdateView.as_view(),