# Path: dash/categories/fields.py
# class CategoryChoiceField(forms.ModelChoiceField):
#     def label_from_instance(self, obj):
#         return obj.get_label_from_instance()
# 
# Path: dash/orgs/views.py
# class OrgObjPermsMixin(OrgPermsMixin):
#     def get_object_org(self):
#         return self.get_object().org
# 
#     def has_org_perm(self, codename):
#         has_org_perm = super(OrgObjPermsMixin, self).has_org_perm(codename)
# 
#         if has_org_perm:
#             user = self.get_user()
# 
#             if user.is_anonymous:
#                 return True
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         has_perm = super(OrgObjPermsMixin, self).has_permission(request, *args, **kwargs)
# 
#         if has_perm:
#             user = self.get_user()
# 
#             # user has global permission
#             if user.has_perm(self.permission):
#                 return True
# 
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
# class OrgPermsMixin(object):
#     """
#     Get the organisation and the user within the inheriting view so that it be
#     come easy to decide whether this user has a certain permission for that
#     particular organization to perform the view's actions
#     """
# 
#     def get_user(self):
#         return self.request.user
# 
#     def derive_org(self):
#         return self.request.org
# 
#     def pre_process(self, request, *args, **kwargs):
#         user = self.get_user()
#         org = self.derive_org()
# 
#         if user.is_superuser:
#             return None
# 
#         if not org:
#             return HttpResponseRedirect(reverse("orgs.org_choose"))
# 
#         return None
# 
#     def has_org_perm(self, permission):
#         (app_label, codename) = permission.split(".")
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().is_anonymous:
#             return False
# 
#         if self.org:
#             org_group = self.get_user().get_org_group()
#             if org_group:
#                 if org_group.permissions.filter(content_type__app_label=app_label, codename=codename).exists():
#                     return True
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         """
#         Figures out if the current user has permissions for this view.
#         """
#         self.kwargs = kwargs
#         self.args = args
#         self.request = request
#         self.org = self.derive_org()
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().has_perm(self.permission):
#             return True
# 
#         return self.has_org_perm(self.permission)
# 
# Path: dash/stories/models.py
# def validate_file_extension(value):
#     def format_audio_link(cls, link):
#     def space_tags(cls, tags):
#     def teaser(self, field, length):
#     def long_teaser(self):
#     def short_teaser(self):
#     def get_written_by(self):
#     def get_featured_images(self):
#     def get_category_image(self):
#     def get_image(self):
# class Story(SmartModel):
#     class Meta:
# class StoryImage(SmartModel):


from smartmin.views import SmartCreateView, SmartCRUDL, SmartListView, SmartUpdateView
from django import forms
from django.core.validators import validate_image_file_extension
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from dash.categories.fields import CategoryChoiceField
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from .models import Category, Story, StoryImage

            while idx <= 3:
                self.form.fields["image_%d" % idx] = forms.ImageField(
                    required=False,
                    label=_("Image %d") % idx,
                    help_text=_("Image to display on story page and in previews (optional)"),
                    validators=[validate_image_file_extension],
                )
                idx += 1

            return form

        def post_save(self, obj):
            obj = super(StoryCRUDL.Images, self).post_save(obj)

            # remove our existing images
            self.object.images.all().delete()

            # overwrite our new ones
            # TODO: this could probably be done more elegantly
            for idx in range(1, 4):
                image = self.form.cleaned_data.get("image_%d" % idx, None)

                if image:
                    StoryImage.objects.create(
                        story=self.object, image=image, created_by=self.request.user, modified_by=self.request.user
                    )

            return obj


# Path: dash/categories/fields.py
# class CategoryChoiceField(forms.ModelChoiceField):
#     def label_from_instance(self, obj):
#         return obj.get_label_from_instance()
# 
# Path: dash/orgs/views.py
# class OrgObjPermsMixin(OrgPermsMixin):
#     def get_object_org(self):
#         return self.get_object().org
# 
#     def has_org_perm(self, codename):
#         has_org_perm = super(OrgObjPermsMixin, self).has_org_perm(codename)
# 
#         if has_org_perm:
#             user = self.get_user()
# 
#             if user.is_anonymous:
#                 return True
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         has_perm = super(OrgObjPermsMixin, self).has_permission(request, *args, **kwargs)
# 
#         if has_perm:
#             user = self.get_user()
# 
#             # user has global permission
#             if user.has_perm(self.permission):
#                 return True
# 
#             return user.get_org() == self.get_object_org()
# 
#         return False
# 
# class OrgPermsMixin(object):
#     """
#     Get the organisation and the user within the inheriting view so that it be
#     come easy to decide whether this user has a certain permission for that
#     particular organization to perform the view's actions
#     """
# 
#     def get_user(self):
#         return self.request.user
# 
#     def derive_org(self):
#         return self.request.org
# 
#     def pre_process(self, request, *args, **kwargs):
#         user = self.get_user()
#         org = self.derive_org()
# 
#         if user.is_superuser:
#             return None
# 
#         if not org:
#             return HttpResponseRedirect(reverse("orgs.org_choose"))
# 
#         return None
# 
#     def has_org_perm(self, permission):
#         (app_label, codename) = permission.split(".")
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().is_anonymous:
#             return False
# 
#         if self.org:
#             org_group = self.get_user().get_org_group()
#             if org_group:
#                 if org_group.permissions.filter(content_type__app_label=app_label, codename=codename).exists():
#                     return True
# 
#         return False
# 
#     def has_permission(self, request, *args, **kwargs):
#         """
#         Figures out if the current user has permissions for this view.
#         """
#         self.kwargs = kwargs
#         self.args = args
#         self.request = request
#         self.org = self.derive_org()
# 
#         if self.get_user().is_superuser:
#             return True
# 
#         if self.get_user().has_perm(self.permission):
#             return True
# 
#         return self.has_org_perm(self.permission)
# 
# Path: dash/stories/models.py
# def validate_file_extension(value):
#     def format_audio_link(cls, link):
#     def space_tags(cls, tags):
#     def teaser(self, field, length):
#     def long_teaser(self):
#     def short_teaser(self):
#     def get_written_by(self):
#     def get_featured_images(self):
#     def get_category_image(self):
#     def get_image(self):
# class Story(SmartModel):
#     class Meta:
# class StoryImage(SmartModel):


# Path: dash/stories/views.py
from smartmin.views import SmartCreateView, SmartCRUDL, SmartListView, SmartUpdateView
from django import forms
from django.core.validators import validate_image_file_extension
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from dash.categories.fields import CategoryChoiceField
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from .models import Category, Story, StoryImage


            while idx <= 3:
                self.form.fields["image_%d" % idx] = forms.ImageField(
                    required=False,
                    label=_("Image %d") % idx,
                    help_text=_("Image to display on story page and in previews (optional)"),
                    validators=[validate_image_file_extension],
                )
                idx += 1

            return form

        def post_save(self, obj):
            obj = super(StoryCRUDL.Images, self).post_save(obj)

            # remove our existing images
            self.object.images.all().delete()

            # overwrite our new ones
            # TODO: this could probably be done more elegantly
            for idx in range(1, 4):
                image = self.form.cleaned_data.get("image_%d" % idx, None)

                if image:
                    StoryImage.objects.create(
                        story=self.object, image=image, created_by=self.request.user, modified_by=self.request.user
                    )

            return obj


    class Create(OrgPermsMixin, SmartCreateView):