# Path: backend/views.py
# class BackendHomeView(ListView, BackendBase):
#     model = DDAHInstanceWeb
#     template_name = 'home.html'
#     context_object_name = 'instances'
# 
#     def get_queryset(self):
#         qs = super(BackendHomeView, self).get_queryset()
#         qs = qs.filter(users=self.request.user)
#         return qs
# 
# class InstanceDetailView(DetailView, InstanceBase):
#     model = DDAHInstanceWeb
#     template_name = 'instance_detail.html'
#     context_object_name = 'instance'
#     slug_field = 'label'
# 
# class CSVUploadView(FormView, InstanceBase):
#     template_name = 'csv_upload.html'
#     form_class = CSVUploadForm
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         self.instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         if self.request.user not in self.instance.users.all():
#             return HttpResponse('Unauthorized', status=401)
#         return super(InstanceBase, self).dispatch(*args, **kwargs)
# 
#     def get_form_kwargs(self):
#         kwargs = super(CSVUploadView, self).get_form_kwargs()
#         kwargs.update({'instance': self.instance})
#         return kwargs
# 
#     def form_valid(self, form):
#         form.upload()
#         return super(CSVUploadView, self).form_valid(form)
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.instance.label})
# 
# class StyleView(InstanceBase, FormView):
#     template_name = 'style_color_picker.html'
#     form_class = ColorPickerForm
# 
#     def get_object(self):
#         self.instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         return self.instance
# 
#     def get_form_kwargs(self):
#         kwargs = super(StyleView, self).get_form_kwargs()
#         kwargs.update({'instance': self.instance})
#         return kwargs
# 
#     def get_success_url(self):
#         url = reverse('backend:instance_style', kwargs={'slug': self.instance.label})
#         return url
# 
#     def form_valid(self, form):
#         form.update_colors()
#         return super(StyleView, self).form_valid(form)
# 
# class CategoryCreateView(CreateView, InstanceBase):
#     form_class = CategoryCreateForm
#     model = DDAHCategory
#     template_name = 'category_create.html'
# 
#     def get_object(self):
#         instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         return instance
# 
#     def get_context_data(self, **kwargs):
#         context = super(CategoryCreateView, self).get_context_data(**kwargs)
#         context['instance'] = self.ddah_instance
#         return context
# 
#     def get_form_kwargs(self):
#         kwargs = super(CategoryCreateView, self).get_form_kwargs()
#         kwargs.update({'ddah_instance': self.ddah_instance})
#         return kwargs
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.ddah_instance.label})
# 
# class PromiseCreateView(CreateView):
#     form_class = PromiseCreateForm
#     model = Promise
# 
#     def get_form_kwargs(self):
#         kwargs = super(PromiseCreateView, self).get_form_kwargs()
#         self.category = DDAHCategory.objects.get(id=self.kwargs['category_id'])
#         kwargs.update({'ddah_category': self.category})
#         return kwargs
# 
#     def get_context_data(self, **kwargs):
#         context = super(PromiseCreateView, self).get_context_data(**kwargs)
#         context['category'] = self.category
#         return context
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.category.instance.label})
# 
# class PromiseUpdateView(UpdateView):
#     model = Promise
#     form_class = PromiseUpdateForm
#     template_name = "promises/promise_update.html"
# 
#     def get_success_url(self):
#         ddah_category = DDAHCategory.objects.get(id=self.object.category.id)
#         return reverse('backend:instance', kwargs={'slug': ddah_category.instance.label})
# 
# class CreateInstanceView(CreateView):
#     model = DDAHInstanceWeb
#     form_class = InstanceCreateForm
#     template_name = 'instances/create.html'
# 
#     def form_valid(self, form):
#         self.ddah_instance = form.save()
#         self.ddah_instance.users.add(self.request.user)
#         return HttpResponseRedirect(self.get_success_url())
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.ddah_instance.label})
# 
# class TemplateUpdateView(UpdateView):
#     model = DDAHTemplate
#     fields = ('content', 'flat_page_content', 'head', 'header', 'style', 'footer', )
#     template_name = 'instances/update_template.html'
#     slug_field = 'instance__label'
# 
#     def get_success_url(self):
#         return reverse('backend:update_template', kwargs={'slug': self.object.instance.label})
# 
# class CategoryDeleteView(InstanceBase, DeleteView):
#     model = DDAHCategory
#     template_name = 'category_confirm_delete.html'
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.object.instance.label})
# 
#     def determine_instance(self):
#         return self.get_object().instance


from django.conf.urls import patterns, url
from .views import BackendHomeView, InstanceDetailView, CSVUploadView, StyleView, CategoryCreateView, \
    PromiseCreateView, PromiseUpdateView, CreateInstanceView, TemplateUpdateView, CategoryDeleteView


urlpatterns = patterns('',
                       url(r'^/?$', BackendHomeView.as_view(), name='home'),
                       url(r'^/create-instance/?$', CreateInstanceView.as_view(), name='create_instance'),
                       url(r'^detail/(?P<slug>[\w-]+)/?$', InstanceDetailView.as_view(), name='instance'),

# Path: backend/views.py
# class BackendHomeView(ListView, BackendBase):
#     model = DDAHInstanceWeb
#     template_name = 'home.html'
#     context_object_name = 'instances'
# 
#     def get_queryset(self):
#         qs = super(BackendHomeView, self).get_queryset()
#         qs = qs.filter(users=self.request.user)
#         return qs
# 
# class InstanceDetailView(DetailView, InstanceBase):
#     model = DDAHInstanceWeb
#     template_name = 'instance_detail.html'
#     context_object_name = 'instance'
#     slug_field = 'label'
# 
# class CSVUploadView(FormView, InstanceBase):
#     template_name = 'csv_upload.html'
#     form_class = CSVUploadForm
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         self.instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         if self.request.user not in self.instance.users.all():
#             return HttpResponse('Unauthorized', status=401)
#         return super(InstanceBase, self).dispatch(*args, **kwargs)
# 
#     def get_form_kwargs(self):
#         kwargs = super(CSVUploadView, self).get_form_kwargs()
#         kwargs.update({'instance': self.instance})
#         return kwargs
# 
#     def form_valid(self, form):
#         form.upload()
#         return super(CSVUploadView, self).form_valid(form)
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.instance.label})
# 
# class StyleView(InstanceBase, FormView):
#     template_name = 'style_color_picker.html'
#     form_class = ColorPickerForm
# 
#     def get_object(self):
#         self.instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         return self.instance
# 
#     def get_form_kwargs(self):
#         kwargs = super(StyleView, self).get_form_kwargs()
#         kwargs.update({'instance': self.instance})
#         return kwargs
# 
#     def get_success_url(self):
#         url = reverse('backend:instance_style', kwargs={'slug': self.instance.label})
#         return url
# 
#     def form_valid(self, form):
#         form.update_colors()
#         return super(StyleView, self).form_valid(form)
# 
# class CategoryCreateView(CreateView, InstanceBase):
#     form_class = CategoryCreateForm
#     model = DDAHCategory
#     template_name = 'category_create.html'
# 
#     def get_object(self):
#         instance = get_object_or_404(DDAHInstanceWeb, label=self.kwargs['slug'])
#         return instance
# 
#     def get_context_data(self, **kwargs):
#         context = super(CategoryCreateView, self).get_context_data(**kwargs)
#         context['instance'] = self.ddah_instance
#         return context
# 
#     def get_form_kwargs(self):
#         kwargs = super(CategoryCreateView, self).get_form_kwargs()
#         kwargs.update({'ddah_instance': self.ddah_instance})
#         return kwargs
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.ddah_instance.label})
# 
# class PromiseCreateView(CreateView):
#     form_class = PromiseCreateForm
#     model = Promise
# 
#     def get_form_kwargs(self):
#         kwargs = super(PromiseCreateView, self).get_form_kwargs()
#         self.category = DDAHCategory.objects.get(id=self.kwargs['category_id'])
#         kwargs.update({'ddah_category': self.category})
#         return kwargs
# 
#     def get_context_data(self, **kwargs):
#         context = super(PromiseCreateView, self).get_context_data(**kwargs)
#         context['category'] = self.category
#         return context
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.category.instance.label})
# 
# class PromiseUpdateView(UpdateView):
#     model = Promise
#     form_class = PromiseUpdateForm
#     template_name = "promises/promise_update.html"
# 
#     def get_success_url(self):
#         ddah_category = DDAHCategory.objects.get(id=self.object.category.id)
#         return reverse('backend:instance', kwargs={'slug': ddah_category.instance.label})
# 
# class CreateInstanceView(CreateView):
#     model = DDAHInstanceWeb
#     form_class = InstanceCreateForm
#     template_name = 'instances/create.html'
# 
#     def form_valid(self, form):
#         self.ddah_instance = form.save()
#         self.ddah_instance.users.add(self.request.user)
#         return HttpResponseRedirect(self.get_success_url())
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.ddah_instance.label})
# 
# class TemplateUpdateView(UpdateView):
#     model = DDAHTemplate
#     fields = ('content', 'flat_page_content', 'head', 'header', 'style', 'footer', )
#     template_name = 'instances/update_template.html'
#     slug_field = 'instance__label'
# 
#     def get_success_url(self):
#         return reverse('backend:update_template', kwargs={'slug': self.object.instance.label})
# 
# class CategoryDeleteView(InstanceBase, DeleteView):
#     model = DDAHCategory
#     template_name = 'category_confirm_delete.html'
# 
#     def get_success_url(self):
#         return reverse('backend:instance', kwargs={'slug': self.object.instance.label})
# 
#     def determine_instance(self):
#         return self.get_object().instance


# Path: backend/urls.py
from django.conf.urls import patterns, url
from .views import BackendHomeView, InstanceDetailView, CSVUploadView, StyleView, CategoryCreateView, \
    PromiseCreateView, PromiseUpdateView, CreateInstanceView, TemplateUpdateView, CategoryDeleteView



urlpatterns = patterns('',
                       url(r'^/?$', BackendHomeView.as_view(), name='home'),
                       url(r'^/create-instance/?$', CreateInstanceView.as_view(), name='create_instance'),
                       url(r'^detail/(?P<slug>[\w-]+)/?$', InstanceDetailView.as_view(), name='instance'),

                       url(r'^detail/(?P<slug>[\w-]+)/category_create/?$', CategoryCreateView.as_view(), name='create_category'),