class BaseCreateView(PermissionMixin, generic.CreateView):
    """Base content create view."""
    template_name = 'base_content_edit.jinja'

    def get_context_data(self, **kwargs):
        context = super(BaseCreateView, self).get_context_data(**kwargs)
        context['verbose_name'] = self.model._meta.verbose_name
        context['creating'] = True
        return context

    def form_valid(self, form):
        content = self.model._meta.verbose_name.capitalize()
        messages.success(self.request, '%s succesfully created.' % content)
        return super(BaseCreateView, self).form_valid(form) class BaseListView(PermissionMixin, generic.ListView):
    """Base content list view."""
    template_name = 'base_content_list.jinja'
    create_object_url = None

    def get_context_data(self, **kwargs):
        context = super(BaseListView, self).get_context_data(**kwargs)
        context['verbose_name'] = self.model._meta.verbose_name
        context['verbose_name_plural'] = self.model._meta.verbose_name_plural
        context['create_object_url'] = self.create_object_url
        return context class BaseUpdateView(PermissionMixin, generic.UpdateView):
    """Base content edit view."""
    template_name = 'base_content_edit.jinja'

    def get_context_data(self, **kwargs):
        context = super(BaseUpdateView, self).get_context_data(**kwargs)
        context['verbose_name'] = self.model._meta.verbose_name
        context['creating'] = False
        return context

    def form_valid(self, form):
        content = self.model._meta.verbose_name.capitalize()
        messages.success(self.request, '%s succesfully updated.' % content)
        return super(BaseUpdateView, self).form_valid(form)

    def get(self, request, *args, **kwargs):
        if getattr(self.get_object(), 'is_editable', True):
            return super(BaseUpdateView, self).get(request, *args, **kwargs)
        messages.error(self.request, 'Object cannot be updated.')
        return redirect(self.success_url)

    def post(self, request, *args, **kwargs):
        if getattr(self.get_object(), 'is_editable', True):
            return super(BaseUpdateView, self).post(request, *args, **kwargs)
        messages.error(self.request, 'Object cannot be updated.')
        return redirect(self.success_url) class EventMetric(models.Model):
    """New generation event metrics."""
    name = models.CharField(max_length=100)
    active = models.BooleanField(default=True)

    objects = models.Manager()
    active_objects = GenericActiveManager()

    class Meta:
        ordering = ['name']

    def __str__(self):
        return self.name

    def get_absolute_edit_url(self):
        return reverse('edit_metric', kwargs={'pk': self.id}) class EventMetricForm(happyforms.ModelForm):
    """Form for EventMetric Model."""

    class Meta:
        model = EventMetric
        fields = ['name', 'active'] class FunctionalAreaForm(happyforms.ModelForm):
    """Form of functional areas."""

    class Meta:
        model = FunctionalArea
        fields = ['name', 'active'] class MobilisingSkillForm(happyforms.ModelForm):
    """Form of mobilising skill."""

    class Meta:
        model = MobilisingSkill
        fields = ['name', 'active'] class MobilisingInterestForm(happyforms.ModelForm):
    """Form of mobilising interest."""

    class Meta:
        model = MobilisingInterest
        fields = ['name', 'active'] class ActivityForm(happyforms.ModelForm):
    """Form of activity type."""

    class Meta:
        model = Activity
        fields = ['name', 'active'] class CampaignForm(happyforms.ModelForm):
    """Form of campaign type."""

    class Meta:
        model = Campaign
        fields = ['name', 'active'] class Activity(models.Model):
    name = models.CharField(max_length=100)
    active = models.BooleanField(default=True)

    objects = models.Manager()
    active_objects = GenericActiveManager()

    class Meta:
        ordering = ['name']
        verbose_name = 'activity'
        verbose_name_plural = 'activities'

    def __str__(self):
        return self.name

    def get_absolute_edit_url(self):
        return reverse('edit_activity', kwargs={'pk': self.id})

    @property
    def is_editable(self):
        """Check if activity is editable."""
        return self.name not in READONLY_ACTIVITIES

    @property
    def is_verifiable(self):
        """Check if the activity is verifiable."""
        if self.name in VERIFIABLE_ACTIVITIES:
            return True
        return False class Campaign(models.Model):
    """Campaign/Initiatives model.

    As of March 2015 this serves as the Initiative model used both
    in events and reports app.
    """
    name = models.CharField(max_length=100)
    active = models.BooleanField(default=True)

    objects = models.Manager()
    active_objects = GenericActiveManager()

    class Meta:
        ordering = ['name']
        verbose_name = 'initiative'
        verbose_name_plural = 'initiatives'

    def __str__(self):
        return self.name

    def get_absolute_edit_url(self):
        return reverse('edit_campaign', kwargs={'pk': self.id})
from django.conf.urls import patterns, url
from django.core.urlresolvers import reverse_lazy
from remo.base.views import BaseCreateView, BaseListView, BaseUpdateView
from remo.events.models import EventMetric
from remo.events.forms import EventMetricForm
from remo.profiles.forms import FunctionalAreaForm, MobilisingSkillForm, MobilisingInterestForm
from remo.profiles.models import FunctionalArea, MobilisingSkill, MobilisingInterest
from remo.reports.forms import ActivityForm, CampaignForm
from remo.reports.models import Activity, Campaign



urlpatterns = patterns(
    '',
    url('^activities/$',
        BaseListView.as_view(
            groups=['Admin', 'Council'],
            model=Activity, create_object_url=reverse_lazy('create_activity')),
        name='list_activities'),
    url('^activities/new/$',
        BaseCreateView.as_view(
            groups=['Admin', 'Council'],
            model=Activity, form_class=ActivityForm,
            success_url=reverse_lazy('list_activities')),
        name='create_activity'),
    url(r'^activities/(?P<pk>\d+)/edit/$',
        BaseUpdateView.as_view(
            groups=['Admin', 'Council'],
            model=Activity, form_class=ActivityForm,
            success_url=reverse_lazy('list_activities')),
        name='edit_activity'),
    url('^initiatives/$',
        BaseListView.as_view(
            groups=['Admin', 'Council'],
            model=Campaign, create_object_url=reverse_lazy('create_campaign')),
        name='list_campaigns'),
    url('^initiatives/new/$',
        BaseCreateView.as_view(
            groups=['Admin', 'Council'],
            model=Campaign, form_class=CampaignForm,
            success_url=reverse_lazy('list_campaigns')),
        name='create_campaign'),
    url(r'^initiatives/(?P<pk>\d+)/edit/$',
        BaseUpdateView.as_view(
            groups=['Admin', 'Council'],
            model=Campaign, form_class=CampaignForm,
            success_url=reverse_lazy('list_campaigns')),
        name='edit_campaign'),
    url('^functional_areas/$',
        BaseListView.as_view(
            model=FunctionalArea,
            create_object_url=reverse_lazy('create_functional_area')),
        name='list_functional_areas'),
    url('^functional_areas/new/$',
        BaseCreateView.as_view(
            model=FunctionalArea, form_class=FunctionalAreaForm,
            success_url=reverse_lazy('list_functional_areas')),
        name='create_functional_area'),
    url(r'^functional_areas/(?P<pk>\d+)/edit/$',
        BaseUpdateView.as_view(
            model=FunctionalArea, form_class=FunctionalAreaForm,
            success_url=reverse_lazy('list_functional_areas')),
        name='edit_functional_area'),
    url('^mobilising_skills/$',
        BaseListView.as_view(
            groups=['Admin', 'Council'],
            model=MobilisingSkill,
            create_object_url=reverse_lazy('create_mobilising_skills')),
        name='list_mobilising_skills'),
    url('^mobilising_skills/new/$',
        BaseCreateView.as_view(
            groups=['Admin', 'Council'],
            model=MobilisingSkill, form_class=MobilisingSkillForm,
            success_url=reverse_lazy('list_mobilising_skills')),
        name='create_mobilising_skills'),
    url(r'^mobilising_skills/(?P<pk>\d+)/edit/$',
        BaseUpdateView.as_view(
            groups=['Admin', 'Council'],

            model=MobilisingSkill, form_class=MobilisingSkillForm,