سؤال

I would like to be able to add users in Django to a user-created group through a view and a template form. Creating the groups is easy, I just am having trouble creating a way to add bulk users (like this email1@email.com, email2@email.com, email203920492@email.com etc) to the group. If the user exists in the system, they are sent a message to join, if they don't exist, they get a message inviting them to join the website and the group.

هل كانت مفيدة؟

المحلول

If I understand what you're asking...

If you have a list of the emails, then simply loop through the list of emails and get the user assigned to each email:

for email in emails:
    try:
        user = User.objects.get(email=email)
    except User.DoesNotExist:
        # A user doesn't exist with that email address, so send the invitation email

    # The user exists, so send them an email with a link to a view that lets them join    the group

In your view, you would add the current logged in user to the group when they visit the link, with something like:

request.user.groups.add(group)

نصائح أخرى

Here is a partial answer -- this returns or creates the users from an email form in the temaplate (form model is below too). I still have to figure out how to email them an invite link OR an "accept" to group link. Suggestions would be appreciated.

@login_required
def community(request):
    places = Community.objects.filter(manager=request.user).order_by('id')
    form = EmailAddForm(request.POST or None)

    if form.is_valid():
        emails = form.cleaned_data['emails'].split(',') # this allows you to enter multiple email addresses in the form and separated by comma
        for email in emails:
            try:
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                user = User.objects.get_or_create(email=email, username=email)
        print user #to check that it's working in console


    return render_to_response('community.html', locals(), context_instance=RequestContext(request))

#forms.py
class EmailAddForm(forms.Form):
    emails = forms.CharField(widget=forms.Textarea)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top