Ejemplo n.º 1
0
 class Meta(object):
     """Meta options."""
     model = Group
     fields = [
         'category', 'description', 'tags', 'state', 'owners',
         'whitelist_users', 'featured', 'private', 'published', 'moderated',
         'member_list_published', 'display_location', 'latitude',
         'longitude', 'radius'
     ]
     widgets = {
         'latitude': forms.HiddenInput(),
         'longitude': forms.HiddenInput(),
         'radius': forms.HiddenInput(),
         'owners': MultipleChoiceWidget('UserAutocomplete'),
     }
Ejemplo n.º 2
0
class ComposeForm(ModelForm):
    """
    A simple default form for private messages.
    """
    recipients = forms.ModelMultipleChoiceField(User.objects.all(),
                                                widget=MultipleChoiceWidget(
                                                    'UserAutocomplete', ))
    subject = forms.CharField(label=_(u"Subject"))

    class Meta:
        model = Message
        fields = ('body', )
        widgets = {
            'body': forms.Textarea(attrs={
                'cols': WRAP_WIDTH,
                'rows': 3
            }),
        }

    def save(self, sender, send=True):
        recipients = self.cleaned_data['recipients']
        subject = self.cleaned_data['subject']
        body = self.cleaned_data['body']

        new_message = Message.objects.create(body=body, sender=sender)
        thread = Thread.objects.create(subject=subject,
                                       latest_msg=new_message,
                                       creator=sender)
        thread.all_msgs.add(new_message)

        for recipient in recipients:
            Participant.objects.get_or_create(thread=thread, user=recipient)

        (sender_part,
         created) = Participant.objects.get_or_create(thread=thread,
                                                      user=sender)
        sender_part.replied_at = sender_part.read_at = now()
        sender_part.save()

        thread.save()  # save this last, since this updates the search index
        message_composed.send(sender=Message,
                              message=new_message,
                              recipients=recipients)
        return (thread, new_message)
Ejemplo n.º 3
0
class GroupForm(SanitizeHTMLMixin, ModelForm):
    """Form for groups.models.Group."""
    category = forms.ModelChoiceField(label='Category',
                                      queryset=Category.objects.all())
    tags = TaggitField(widget=TaggitWidget(
        'TagAutocomplete', attrs={'placeholder': "type tags here"}),
                       required=False)
    display_location = forms.CharField(
        label="Display Location",
        help_text=("Optionally enter the location you'd like displayed on the "
                   "group description page."),
        required=False,
        widget=forms.TextInput(
            attrs={'placeholder': "i.e. West Loop, Chicago, IL"}))
    whitelist_users = forms.ModelMultipleChoiceField(
        widget=MultipleChoiceWidget('UserAutocomplete'),
        help_text=u'These users will always be allowed to send to this group.',
        required=False,
        queryset=get_user_model().objects.all())

    class Meta(object):
        """Meta options."""
        model = Group
        fields = [
            'category', 'description', 'tags', 'state', 'owners',
            'whitelist_users', 'featured', 'private', 'published', 'moderated',
            'member_list_published', 'display_location', 'latitude',
            'longitude', 'radius'
        ]
        widgets = {
            'latitude': forms.HiddenInput(),
            'longitude': forms.HiddenInput(),
            'radius': forms.HiddenInput(),
            'owners': MultipleChoiceWidget('UserAutocomplete'),
        }

    def clean_description(self):
        """Cleans the description field"""
        return self.sanitize_html(self.cleaned_data['description'])

    def clean_tags(self):
        """Clean the tags added into the form"""
        tags = self.cleaned_data['tags']
        invalid_tags = []
        valid_tags = Tag.objects.values_list('name', flat=True)
        for tag in tags:
            if tag not in valid_tags:
                invalid_tags.append(tag)
        if invalid_tags:
            self._errors['tags'] = self.error_class(
                ['These tags are invalid: %s.' % ', '.join(invalid_tags)])
        return tags

    def save(self, *args, **kwargs):
        """Save the form"""
        self.instance.tos_accepted_at = now()
        result = super(GroupForm, self).save(*args, **kwargs)

        # For each group owner, added them to the group
        if self.instance.pk:
            for owner in self.cleaned_data['owners']:
                add_user_to_group.delay(owner.pk, self.instance.pk)

        return result
Ejemplo n.º 4
0
class GroupUserAddForm(forms.Form):
    """Form to quickly add a user to a form"""
    users = forms.MultipleChoiceField(
        widget=MultipleChoiceWidget('UserAutocomplete'))