# Path: search/web/forms.py
# class CourseModelForm(forms.ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         if kwargs.get('instance'):
#             self.instance = kwargs.get('instance')
#         else:
#             source = kwargs.pop('source')
#             self.base_fields['source'].initial = source.id
#             self.base_fields['language'].initial = 'English'
#             self.instance = None
# 
#         self.base_fields['source'].widget = forms.HiddenInput()
# 
#         self.base_fields['merlot_categories'].widget = django_select2.widgets.Select2MultipleWidget()
#         self.base_fields['merlot_categories'].help_text = "Press Esc key to close the selection box after you've selected all relevant categories."
# 
#         self.base_fields['language'].widget = django_select2.widgets.Select2Widget(choices=LANGUAGE_CHOICES)
#         self.base_fields['tags'].help_text = "Separate tags with commas"
# 
#         self.base_fields['audience'].widget = forms.RadioSelect(choices=PRIMARY_AUDIENCE_CHOICES)
#         self.base_fields['creative_commons'].widget = forms.RadioSelect(choices=YES_NO_UNSURE_CHOICES)
#         self.base_fields['creative_commons_commercial'].widget = forms.RadioSelect(choices=YES_NO_UNSURE_CHOICES)
#         self.base_fields['creative_commons_derivatives'].widget = forms.RadioSelect(choices=CC_DERIV_CHOICES)
# 
#         self.helper = FormHelper()
#         self.helper.form_action = '.'
# 
#         self.helper.layout = Layout(
#             Row(
#                 Field('merlot_ignore'),
#                 Field('title'),
#             )
#         )
# 
#         if self.instance and self.instance.linkurl:
#             self.helper.layout.append(Layout(
#                 Row(
#                     HTML("<a href='%s' target='_blank'>View link</a>" % self.instance.linkurl),
#                     Field('linkurl'),
#                     )
#             ))
#         else:
#             self.helper.layout.append(Layout(
#                 Row(
#                     Field('linkurl')
#                 )
#             ))
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 Field('description'),
#                 Field('language'),
#                 Field('content_medium'),
#                 Field('author'),
#                 Field('tags'),
#             )
#         ))
# 
#         if self.instance and self.instance.categories.count():
#             self.helper.layout.append(Layout(
#                 Row(
#                     HTML("<h4>Legacy categories:</h4>"),
#                     HTML(
#                     """ {%load mptt_tags %}
#                         <p>
#                         {% for node in course.categories.all %}
#                         {{ node.get_ancestors|tree_path:" > " }} > {{ node }}<br />
#                         {% endfor %}
#                         </p>
#                     """)
#                 ))
#             )
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 HTML("<a href='http://www.merlot.org/merlot/categories.htm' target='_blank'>Full Merlot Categories list</a>"),
#                 Field('merlot_categories'),
#             )
#         ))
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 Field('image_url'),
#                 Div(Field('audience'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons_commercial'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons_derivatives'),
#                     css_class="small-8 columns left"),
#             )
#         ))
# 
#         self.helper.layout.append(
#             Row(
#                 Submit('Save', 'save'),
#             css_class="buttons")
#         )
# 
#         super(CourseModelForm, self).__init__(*args, **kwargs)
# 
#     class Meta:
#         model = Course
#         fields = ('title', 'linkurl', 'source', 'description', 'language', 'author', 'merlot_categories',
#                   'tags', 'content_medium', 'merlot_ignore',
#                   'image_url', 'audience',
#                   'creative_commons', 'creative_commons_commercial', 'creative_commons_derivatives',)


import HTMLParser
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from vanilla import ListView, DetailView, FormView
from data.models import Course, Source
from ..forms import CourseModelForm

        return context

    def get_form(self, data=None, files=None, **kwargs):
        kwargs['source'] = self.source
        return self.form_class(data, files, **kwargs)

    def form_valid(self, form):
        cleaned_data = form.cleaned_data

        cleaned_data['source'] = self.source
        cleaned_data['provider'] = self.source.provider

        # categories = cleaned_data.pop('categories')
        merlot_categories = cleaned_data.pop('merlot_categories')

        course = Course(**cleaned_data)
        course.save()

        # for cat in categories:
        #     course.categories.add(cat)

        for cat in merlot_categories:
            course.merlot_categories.add(cat)

        return redirect(self.source.get_absolute_url())


class CourseFormEditView(CourseActionMixin, FormView):
    model = Course

# Path: search/web/forms.py
# class CourseModelForm(forms.ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         if kwargs.get('instance'):
#             self.instance = kwargs.get('instance')
#         else:
#             source = kwargs.pop('source')
#             self.base_fields['source'].initial = source.id
#             self.base_fields['language'].initial = 'English'
#             self.instance = None
# 
#         self.base_fields['source'].widget = forms.HiddenInput()
# 
#         self.base_fields['merlot_categories'].widget = django_select2.widgets.Select2MultipleWidget()
#         self.base_fields['merlot_categories'].help_text = "Press Esc key to close the selection box after you've selected all relevant categories."
# 
#         self.base_fields['language'].widget = django_select2.widgets.Select2Widget(choices=LANGUAGE_CHOICES)
#         self.base_fields['tags'].help_text = "Separate tags with commas"
# 
#         self.base_fields['audience'].widget = forms.RadioSelect(choices=PRIMARY_AUDIENCE_CHOICES)
#         self.base_fields['creative_commons'].widget = forms.RadioSelect(choices=YES_NO_UNSURE_CHOICES)
#         self.base_fields['creative_commons_commercial'].widget = forms.RadioSelect(choices=YES_NO_UNSURE_CHOICES)
#         self.base_fields['creative_commons_derivatives'].widget = forms.RadioSelect(choices=CC_DERIV_CHOICES)
# 
#         self.helper = FormHelper()
#         self.helper.form_action = '.'
# 
#         self.helper.layout = Layout(
#             Row(
#                 Field('merlot_ignore'),
#                 Field('title'),
#             )
#         )
# 
#         if self.instance and self.instance.linkurl:
#             self.helper.layout.append(Layout(
#                 Row(
#                     HTML("<a href='%s' target='_blank'>View link</a>" % self.instance.linkurl),
#                     Field('linkurl'),
#                     )
#             ))
#         else:
#             self.helper.layout.append(Layout(
#                 Row(
#                     Field('linkurl')
#                 )
#             ))
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 Field('description'),
#                 Field('language'),
#                 Field('content_medium'),
#                 Field('author'),
#                 Field('tags'),
#             )
#         ))
# 
#         if self.instance and self.instance.categories.count():
#             self.helper.layout.append(Layout(
#                 Row(
#                     HTML("<h4>Legacy categories:</h4>"),
#                     HTML(
#                     """ {%load mptt_tags %}
#                         <p>
#                         {% for node in course.categories.all %}
#                         {{ node.get_ancestors|tree_path:" > " }} > {{ node }}<br />
#                         {% endfor %}
#                         </p>
#                     """)
#                 ))
#             )
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 HTML("<a href='http://www.merlot.org/merlot/categories.htm' target='_blank'>Full Merlot Categories list</a>"),
#                 Field('merlot_categories'),
#             )
#         ))
# 
#         self.helper.layout.append(Layout(
#             Row(
#                 Field('image_url'),
#                 Div(Field('audience'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons_commercial'),
#                     css_class="small-4 columns"),
#                 Div(Field('creative_commons_derivatives'),
#                     css_class="small-8 columns left"),
#             )
#         ))
# 
#         self.helper.layout.append(
#             Row(
#                 Submit('Save', 'save'),
#             css_class="buttons")
#         )
# 
#         super(CourseModelForm, self).__init__(*args, **kwargs)
# 
#     class Meta:
#         model = Course
#         fields = ('title', 'linkurl', 'source', 'description', 'language', 'author', 'merlot_categories',
#                   'tags', 'content_medium', 'merlot_ignore',
#                   'image_url', 'audience',
#                   'creative_commons', 'creative_commons_commercial', 'creative_commons_derivatives',)


# Path: search/web/views/dashboard.py
import HTMLParser
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from vanilla import ListView, DetailView, FormView
from data.models import Course, Source
from ..forms import CourseModelForm


        return context

    def get_form(self, data=None, files=None, **kwargs):
        kwargs['source'] = self.source
        return self.form_class(data, files, **kwargs)

    def form_valid(self, form):
        cleaned_data = form.cleaned_data

        cleaned_data['source'] = self.source
        cleaned_data['provider'] = self.source.provider

        # categories = cleaned_data.pop('categories')
        merlot_categories = cleaned_data.pop('merlot_categories')

        course = Course(**cleaned_data)
        course.save()

        # for cat in categories:
        #     course.categories.add(cat)

        for cat in merlot_categories:
            course.merlot_categories.add(cat)

        return redirect(self.source.get_absolute_url())


class CourseFormEditView(CourseActionMixin, FormView):
    model = Course

    form_class = CourseModelForm