コード例 #1
0
ファイル: community.py プロジェクト: tainaml/p_gs
class CoreCommunityFollowersForm(IdeiaForm):

    community = forms.ModelChoiceField(queryset=Community.objects.all(), required=True)
    criteria = forms.CharField(required=False)
    state = forms.ModelChoiceField(queryset=State.objects.filter(country=1), required=False)
    city = forms.ModelChoiceField(queryset=City.objects.all(), required=False)
    page = forms.IntegerField(required=False)

    def __init__(self, startswith=False, items_per_page=None, *args, **kwargs):
        self.startswith = startswith
        self.items_per_page = items_per_page

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

        if self.data and 'state' in self.data and self.data.get('state'):
            self.fields['city'].queryset = City.objects.filter(state=self.data['state'])

    def clean(self):
        cleaned_data = super(CoreCommunityFollowersForm, self).clean()
        cleaned_data['page'] = cleaned_data['page'] if 'page' in cleaned_data and cleaned_data['page'] else 1

        return cleaned_data

    def __process__(self):
        return Business.get_followers(
            self.cleaned_data,
            self.items_per_page,
            self.cleaned_data.get('page'),
            self.startswith
        )
コード例 #2
0
class CoreUserProfileFullEditForm(EditProfileForm):

    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    responsibility = forms.ModelMultipleChoiceField(queryset=Responsibility.objects.all().order_by('name'))
    state = forms.ModelChoiceField(queryset=State.objects.filter(country=1))
    state_hometown = forms.ModelChoiceField(queryset=State.objects.filter(country=1))
    city_hometown = forms.ModelChoiceField(queryset='')

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

        if self.data and 'state_hometown' in self.data:
            self.fields['city_hometown'].queryset = City.objects.filter(state=self.data['state_hometown'])

    def is_valid(self):
        is_valid = super(CoreUserProfileFullEditForm, self).is_valid()
        return is_valid

    @transaction.atomic()
    def __process__(self):
        process_profile = super(CoreUserProfileFullEditForm, self).__process__()
        process_user = BusinessUserProfile.update_user(self.user, data={
            'first_name': self.cleaned_data['first_name'],
            'last_name': self.cleaned_data['last_name']
        })
        process_occupation = BusinessUserProfile.update_or_create_occupation(profile=process_profile, responsibilities=self.cleaned_data.get('responsibility'))

        return process_profile if (process_profile and process_occupation and process_user) else False
コード例 #3
0
class CoreUserProfileEditForm(EditProfileForm):

    responsibility = forms.ModelChoiceField(queryset=Responsibility.objects.all())
    state = forms.ModelChoiceField(queryset=State.objects.filter(country=1))

    @transaction.atomic()
    def __process__(self):
        process_profile = super(CoreUserProfileEditForm, self).__process__()
        process_occupation = BusinessUserProfile.update_or_create_occupation(
            process_profile,
            responsibilities=[self.cleaned_data.get('responsibility')])

        return process_profile if (process_profile and process_occupation) else False
コード例 #4
0
class EditProfileForm(IdeiaForm):
    birth = forms.DateField(input_formats=['%d/%m/%Y'])
    gender = forms.CharField(max_length=1, required=True)
    city = forms.ModelChoiceField(queryset='', required=False)
    city_hometown = forms.ModelChoiceField(queryset='')
    profile_picture = forms.ImageField(required=False)
    description = forms.CharField(required=False, widget=forms.Textarea, max_length=255)

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

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

        self.fields['city'].queryset = City.objects.all()

        if self.data and 'state' in self.data:
            self.fields['city_hometown'].queryset = City.objects.filter(state=self.data['state'])


    def is_valid(self):

        is_valid = super(EditProfileForm, self).is_valid()
        image = self.cleaned_data.get('profile_picture', False)
        birth = self.cleaned_data.get('birth', None)

        if birth and birth.year > timezone.now().year:
            is_valid = False
            self.add_error('birth', ValidationError(_('Birth invalid.'), code='birth'))

        if 'gender' in self.cleaned_data:
            if self.cleaned_data['gender'].upper() not in ['M', 'F']:
                is_valid = False
                self.add_error('gender',
                               ValidationError(_('Gênero não permitido.'), code='gender'))

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

            if image._size > 1024 * 1024:
                self.add_error('profile_picture',
                               ValidationError(_('Image size more than 1mb.'), code='profile_picture'))
                is_valid = False
        return is_valid

    def __process__(self):
        self.instance = Business.edit_profile(self.user, self.cleaned_data)
        return self.instance
コード例 #5
0
ファイル: form.py プロジェクト: tainaml/p_gs
class ComplaintForm(IdeiaForm):
    description = forms.CharField(max_length=512, required=False)
    complaint_type = forms.ModelChoiceField(queryset=Business.get_type_complaint(), required=True)
    content_type = forms.CharField(max_length=20, required=True)
    object_id = forms.IntegerField(required=True)
    community_complaint = forms.ModelMultipleChoiceField(queryset=Community.objects.all(), required=False)

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

    def is_valid(self):

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

        try:
            entity_to_complaint = settings.ENTITY_TO_COMPLAINT
        except AttributeError:
            entity_to_complaint = False

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

        return valid

    def __process__(self):
        return Business.create_complaint(parameters=self.cleaned_data, user=self.user)
コード例 #6
0
class CommentDeleteForm(IdeiaForm):

    comment = forms.ModelChoiceField(queryset=Comment.objects.all())
    author = None

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

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

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

        if 'comment' in self.cleaned_data and self.cleaned_data.get(
                'comment').author != self.author:
            is_valid = False
            self.add_error(
                '__all__',
                ValidationError(
                    _('You do not have permission to perform this action.'),
                    code='without-permission'))

        return is_valid

    def __process__(self):
        return Business.delete_comment(self.cleaned_data.get('comment'))
コード例 #7
0
ファイル: community.py プロジェクト: tainaml/p_gs
class CoreCommunitySearchMaterialsForm(IdeiaForm):

    criteria = forms.CharField(required=False)
    page = forms.IntegerField(required=False)
    tags = forms.ModelChoiceField(queryset=Business.get_avaiable_tags(), required=False,
                                  empty_label=_("All"))

    def __init__(self, community=None, items_per_page=10, *args, **kwargs):
        self.community = community
        self.items_per_page = items_per_page

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

    def get_avaiable_tags(self):
        return Business.get_avaiable_tags()

    def clean(self):
        cleaned_data = super(CoreCommunitySearchMaterialsForm, self).clean()

        cleaned_data['page'] = cleaned_data['page'] \
            if 'page' in cleaned_data and cleaned_data['page'] else 1

        return cleaned_data

    def __process__(self):
        return Business.get_articles_with_tags(
            self.community,
            self.cleaned_data.get('criteria'),
            self.items_per_page,
            self.cleaned_data.get('page', 1),
            self.cleaned_data.get('tags')
        )
コード例 #8
0
class CoreSearchCommunitiesForm(IdeiaForm):

    criteria = forms.CharField(required=False)
    __term = None
    try:
        __term = Term.objects.get(description__icontains='categoria')
    except:
        pass
    category = forms.ModelChoiceField(queryset=Taxonomy.objects.filter(term=__term), required=False)
    page = forms.IntegerField(required=False)

    def __init__(self, author, items_per_page, *args, **kwargs):
        self.author = author
        self.items_per_page = items_per_page

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

    def clean(self):
        cleaned_data = super(CoreSearchCommunitiesForm, self).clean()

        cleaned_data['page'] = cleaned_data['page'] \
            if 'page' in cleaned_data and cleaned_data['page'] else 1

        return cleaned_data

    def __process__(self):
        return BusinessSocialActions.get_users_acted_by_author_with_parameters(
            author=self.author,
            action=settings.SOCIAL_FOLLOW,
            content_type='community',
            items_per_page=self.items_per_page,
            page=self.cleaned_data.get('page', 1),
            criteria=self.cleaned_data.get('criteria'),
            category=self.cleaned_data.get('category')
        )
コード例 #9
0
class ContactForm(IdeiaForm):
    subject = forms.ModelChoiceField(queryset=Business.get_contact_subjects(),
                                     required=True)
    message = forms.CharField(max_length=1024, required=True)

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

    def __process__(self):
        self.instance = Business.save(self.cleaned_data, self.user)
        return self.instance
コード例 #10
0
class RemoveAnswerForm(IdeiaForm):

    answer = forms.ModelChoiceField(queryset=Answer.objects.all(), required=True)

    def __init__(self, *args, **kwargs):
        super(RemoveAnswerForm, self).__init__(*args, **kwargs)

    def is_valid(self):
        is_valid = super(RemoveAnswerForm, self).is_valid()
        return is_valid

    def __process__(self):
        return Business.remove_answer(self.cleaned_data.get('answer'))
コード例 #11
0
class OccupationForm(IdeiaForm):
    responsibility = forms.ModelChoiceField(queryset=Responsibility.objects.all())
    company = forms.CharField(max_length=100)

    def __init__(self, data=None, request=None, data_model=None, instance=None, *args, **kwargs):
        self.request = request
        self.instance = instance if instance and isinstance(instance, models.Model) else None

        super(OccupationForm, self).__init__(data, *args, **kwargs)

        if data_model is not None and isinstance(data_model, models.Model):
            self.data = forms.model_to_dict(data_model)

    def is_valid(self):
        is_valid = super(OccupationForm, self).is_valid()
        return is_valid

    def __process__(self):
        if self.instance:
            return Business.update_occupation(self.instance, self.cleaned_data)
        else:
            return Business.create_occupation(self.request.user, self.cleaned_data)