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.
Asked
Active
Viewed 168 times
1

jmitchel3
- 391
- 3
- 4
- 17
2 Answers
0
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)

sdornan
- 2,805
- 2
- 15
- 7
-
thanks sdornan i appreciate it. i'll try it out once I figure out how to list all users in a group (and their associated objects)! – jmitchel3 Sep 26 '12 at 22:24
-
Getting all the users in a group would be something like: group = Group.objects.get(id=whatever) and then users = group.user_set.all() – sdornan Sep 26 '12 at 23:21
-
oh i created my own groups using a manytomany field. due to the permissions for each type of user (some users can make their own groups while others cannot) – jmitchel3 Sep 26 '12 at 23:30
-
see below for the view I created. Thank you for your help! – jmitchel3 Sep 26 '12 at 23:34
-
@sdoman this is the problem i'm having with getting them all to show [link](http://stackoverflow.com/questions/12611443/django-group-users-list-all-associated-objects) – jmitchel3 Sep 27 '12 at 00:01
-
@sdoman... how do you send the 'invitation email' to both the website AND the group? (website is more important) – jmitchel3 Oct 03 '12 at 08:26
0
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)

jmitchel3
- 391
- 3
- 4
- 17