Exemplo n.º 1
0
 class Meta:
     exclude = ()
     widgets = {
         'description': SummernoteWidget(editor_conf='responsibility'),
         'about': SummernoteWidget(editor_conf='responsibility'),
         'where_get': SummernoteWidget(editor_conf='responsibility'),
         'more_info': SummernoteWidget(editor_conf='responsibility'),
     }
Exemplo n.º 2
0
class CommentReplyForm(IdeiaForm):
    description = forms.CharField(widget=SummernoteWidget(editor_conf='reply'), max_length=settings.ANSWER_TEXT_LIMIT if hasattr(settings, "ANSWER_TEXT_LIMIT") else 10000, required=True)

    def __init__(self, instance=None, user=None, *args, **kargs):
        super(CommentReplyForm, self).__init__(instance, *args, **kargs)
        self.instance = instance
        self.user = user
        self.question = instance["question_id"]
        self.description = instance["description"]

    def is_valid(self):
        is_valid = super(CommentReplyForm, self).is_valid()

        description = self.cleaned_data.get('description', '').strip()

        if description == '' or description is None:
            self.add_error('description', ValidationError(_('Your answer can not be blank'), code='description'))
            is_valid = False

        return is_valid

    def clean_description(self):
        description = self.cleaned_data.get('description', '').strip()
        return description

    def __process__(self):
        return Business.comment_reply(self.cleaned_data, self.user, self.question)
Exemplo n.º 3
0
class CreateCommentForm(IdeiaForm):
    content = forms.CharField(
        max_length=settings.COMMENT_TEXT_LIMIT if hasattr(
            settings, "COMMENT_TEXT_LIMIT") else 10000,
        required=True,
        widget=SummernoteWidget(editor_conf='comment', load_init=False))
    content_type = forms.CharField(max_length=20, required=True)
    content_object_id = forms.IntegerField(required=True)

    def __init__(self, user=None, *args, **kwargs):
        self.user = user
        super(CreateCommentForm, self).__init__(*args, **kwargs)

    def __process__(self):
        return Business.create_comment(self.user, self.cleaned_data)

    def clean(self):
        self.cleaned_data = super(CreateCommentForm, self).clean()
        if 'content' in self.cleaned_data:
            self.cleaned_data['content'] = self.cleaned_data['content'].strip()

        return self.cleaned_data

    def is_valid(self):

        valid = super(CreateCommentForm, self).is_valid()

        if not self.user or not self.user.is_authenticated:
            self.add_error(
                None,
                ValidationError(('User must be authenticated.'),
                                code='is_not_authenticated'))
            valid = False

        if 'content' in self.cleaned_data and self.cleaned_data[
                'content'] == '':
            self.add_error(
                'content',
                ValidationError(
                    ('Is not possible to comment only with white spaces.'),
                    code='white_spaces'))
            valid = False

        try:
            entity_to_comment = settings.ENTITY_TO_COMMENT
        except AttributeError:
            entity_to_comment = False

        if not entity_to_comment or self.cleaned_data[
                'content_type'] not in entity_to_comment:
            self.add_error(
                None,
                ValidationError(('Content Type is not specified.'),
                                code='content_is_not_specified'))
            valid = False

        return valid
Exemplo n.º 4
0
 class Meta:
     excludes = ()
     widgets = {
         'text': SummernoteWidget(editor_conf='article_admin'),
         # 'author': SelectizeSelectSingle(
         #     unique_name='admin_authors',
         #     label_field='username',
         #     search_fields=(
         #         'username__unaccent__icontains',
         #         'first_name__unaccent__icontains',
         #         'last_name__unaccent__icontains',
         #     )
         # ),
     }
Exemplo n.º 5
0
class EditAnswerForm(IdeiaForm):
    description = forms.CharField(widget=SummernoteWidget(editor_conf='reply'), max_length=settings.ANSWER_TEXT_LIMIT if hasattr(settings, "ANSWER_TEXT_LIMIT") else 10000, required=True)

    user = None

    def __init__(self, instance=None, user=None, *args, **kwargs):
        self.instance = instance
        self.user = user

        if self.instance:

            initial = kwargs.get('initial', {})
            initial.update({
                'description': self.instance.description
            })

            kwargs.update({
                'initial': initial
            })

        super(EditAnswerForm, self).__init__(*args, **kwargs)

    def set_author(self, user):
        self.user = user

    def is_valid(self):
        is_valid = super(EditAnswerForm, self).is_valid()

        if not self.user or not self.user.is_authenticated():
            self.add_error(None,
                           ValidationError(('User must be authenticated.'),
                                           code='is_not_authenticated'))
            is_valid = False

        if self.user != self.instance.author:
            self.add_error(None,
            ValidationError(('User dont have access'),
                                               code='is_not_permission'))
            is_valid = False

        return is_valid

    def __process__(self):
        return Business.update_reply(self.cleaned_data, self.instance)
Exemplo n.º 6
0
class EditCommentForm(IdeiaForm):

    content = forms.CharField(
        max_length=settings.COMMENT_TEXT_LIMIT if hasattr(
            settings, "COMMENT_TEXT_LIMIT") else 10000,
        required=True,
        widget=SummernoteWidget(editor_conf='comment', load_init=False))

    def __init__(self, user=None, instance=None, *args, **kargs):
        self.user = user
        self.instance = instance

        super(EditCommentForm, self).__init__(*args, **kargs)

    def is_valid(self):

        valid = super(EditCommentForm, self).is_valid()

        if 'content' in self.cleaned_data:
            content = strip_tags(self.cleaned_data['content'])
            if content == '':

                self.add_error(
                    'content',
                    ValidationError(
                        ('Is not possible to comment only with white spaces.'),
                        code='white_spaces'))
                valid = False

        if self.user and self.instance.author and self.instance.author != self.user:
            self.add_error(
                None,
                ValidationError(('comment edit permission denied!'),
                                code='is_not_authenticated'))
            valid = False

        return valid

    def __process__(self):
        return Business.edit_comment(self.instance, self.cleaned_data)
Exemplo n.º 7
0
class SummernoteFormField(CharField):

    def __init__(self, editor_conf='default', load_init=True, async=False, *args, **kwargs):

        kwargs.update({'widget': SummernoteWidget(editor_conf=editor_conf, load_init=load_init, async=async)})
Exemplo n.º 8
0
 class Meta:
     excludes = ()
     widgets = {'description': SummernoteWidget(editor_conf='community')}
Exemplo n.º 9
0
class CreateQuestionForm(IdeiaModelForm):

    title = forms.CharField(max_length=settings.QUESTION_TITLE_LIMIT if hasattr(settings, "QUESTION_TITLE_LIMIT") else 100, required=True)
    slug = forms.SlugField(max_length=300, required=False)
    description = forms.CharField(max_length=settings.QUESTION_TEXT_LIMIT if hasattr(settings, "QUESTION_TEXT_LIMIT") else 10000, widget=SummernoteWidget(editor_conf='question'), required=True)

    user = None

    class Meta:
        model = Business.Question
        exclude = ['author', 'question_date', 'slug', 'search_vector', 'like_count', 'dislike_count', 'comment_count']

    def is_valid(self):
        valid = super(CreateQuestionForm, self).is_valid()
        return valid

    def __init__(self, data=None, files=None, user=None, *args, **kargs):
        super(CreateQuestionForm, self).__init__(data, files, *args, **kargs)
        self.set_author(user)

    def set_author(self, user):
        self.user = user

    def clean_description(self):
        regex = re.compile(r'<p>&nbsp;</p>')
        _description = regex.sub('', self.cleaned_data.get('description'))

        return _description

    def clean_slug(self):
        slug = self.cleaned_data.get('slug', None)
        title = self.cleaned_data.get('title')
        slug = slug if bool(slug) else slugify(title)
        return slug

    @transaction.atomic()
    def __process__(self):
        self.instance.author = self.user
        # self.instance = Business.save_question(self.user, self.cleaned_data)

        self.instance = self.save()

        # TODO: [POG] Protect for error in oldest questions without slug
        if not bool(self.instance.slug):
            self.instance.slug = slugify(self.instance.title)
            self.instance.save()

        return self.instance
Exemplo n.º 10
0
    class Meta:

        widgets = {
            'content': SummernoteWidget(editor_conf='article_admin')
        }
Exemplo n.º 11
0
class ArticleForm(IdeiaModelForm):

    title = forms.CharField(required=True,
                            max_length=settings.ARTICLE_TITLE_LIMIT if hasattr(
                                settings, "ARTICLE_TITLE_LIMIT") else 100)
    slug = forms.SlugField(max_length=150, required=False)
    text = forms.CharField(required=True,
                           widget=SummernoteWidget(editor_conf='article'),
                           max_length=settings.ARTICLE_TEXT_LIMIT if hasattr(
                               settings, "ARTICLE_TEXT_LIMIT") else 10000)
    # image = forms.ImageField(required=False)
    publishin = forms.DateTimeField(required=False)
    # status = forms.ChoiceField(required=False, choices=Business.Article.STATUS_CHOICES)
    # author = forms.IntegerField(required=False)

    # Actions: save, publish, schedule
    ACTION_SAVE = 1
    ACTION_PUBLISH = 2
    ACTION_SCHEDULE = 3

    action = ACTION_SAVE

    _author = None

    class Meta:
        model = Business.Article
        exclude = [
            'first_slug', 'slug', 'status', 'search_vector', 'author',
            'comment_count', 'like_count', 'dislike_count'
        ]

    def __init__(self, data=None, files=None, author=False, *args, **kwargs):

        super(ArticleForm, self).__init__(data, files, *args, **kwargs)

        # if author:
        #     self.set_author(author)

    def clean_text(self):
        regex = re.compile(r'<p>&nbsp;</p>')
        _text = regex.sub('', self.cleaned_data.get('text'))

        return _text

    def clean_publishin(self):
        _date = self.cleaned_data.get('publishin')
        _instance_date = self.instance.publishin
        _now = timezone.now()

        if not _date and _instance_date:
            _date = _instance_date

        if self.action == self.ACTION_SCHEDULE:
            if not _date:
                self.add_error('publishin',
                               ValidationError(_('Publish date has invalid')))
            elif _date < _now:
                self.add_error('publishin',
                               ValidationError(_('Publish date has invalid')))
            else:
                _date = _date if _date else _now

        elif self.action == self.ACTION_PUBLISH:
            _date = _now

        return _date

    def clean_slug(self):
        slug = self.cleaned_data.get('slug', None)
        title = self.cleaned_data.get('title')
        slug = slug if bool(slug) else Business.get_valid_slug(
            self.instance, title)
        return slug

    # def clean_author(self):
    #     _author = self.cleaned_data.get('author')
    #     return _author if _author else self._author

    def is_valid(self):
        valid = True

        old_status_is_published = False

        if not super(ArticleForm, self).is_valid():
            valid = False

        if self.instance.status == Business.Article.STATUS_PUBLISH:
            old_status_is_published = Business.Article.STATUS_PUBLISH

        slug = self.cleaned_data.get('slug', None)
        if bool(slug) and self.is_author():
            title = self.cleaned_data.get('title')
            slug = slug if bool(slug) else Business.get_valid_slug(
                self.instance, title)
            self.instance.slug = slug

        if bool(self.instance.slug) and not self.is_author():
            self.cleaned_data.pop('slug')

        image = self.cleaned_data.get('image', False)

        if image and 'image' in self.changed_data:
            if image.content_type not in settings.IMAGES_ALLOWED:
                self.add_error(
                    'image',
                    ValidationError(_('Image format is not allowed.'),
                                    code='image_format_incorrect'))
                valid = False

        if image and 'image' in self.changed_data:
            if image.size > 2621440:  #2,5 MB
                self.add_error(
                    'image',
                    ValidationError(_('Image size is too long.'),
                                    code='image_size_soo_long'))
                valid = False

        if 'submit-publish' in self.data:
            '''
            Publishing action
            '''
            self.action = self.ACTION_PUBLISH
            self.instance.do_publish()

        elif 'submit-save' in self.data:
            '''
            Save action
            '''
            self.instance.action = self.ACTION_SAVE

            if self.instance.status == Business.Article.STATUS_TEMP:
                self.instance.status = Business.Article.STATUS_DRAFT

            self.instance.do_save()

        # self.cleaned_data.update({
        #     'status': self.instance.status
        # })

        if old_status_is_published and 'publishin' in self.cleaned_data:
            del self.cleaned_data['publishin']

        return valid

    def is_author(self):
        return bool(self.instance.author == self._author)

    def set_author(self, author):
        self._author = author

    def __process__(self):
        if not self.instance.author:
            self.instance.author = self._author

        self.instance = self.save()
        return Business.save_article(self.instance, self.cleaned_data)
Exemplo n.º 12
0
 class Meta:
     excludes = ()
     widgets = {'text': SummernoteWidget(editor_conf='article_admin')}
Exemplo n.º 13
0
 class Meta:
     model = UserAlert
     exclude = ()
     widgets = {'content': SummernoteWidget(editor_conf='article')}