def add_users(self, request, farm_object): added_users = request.POST.getlist('added_user') removed_added_users = request.POST.getlist('removed_added_user') for u in added_users: if u in removed_added_users: continue # Can't remember why the need to strip here, but if really needed, # then probably needed for removed_added_users as well. u = u.strip() try: user = User.objects.get(email = u) farm_object.users.add(user) farm_object.save() except: # User does not exist try: invited_user = InvitedUser.objects.get(email = u) except: # New user. Must send email send_invitation_email(u, request.user, farm_object) invited_user = InvitedUser(email = u) invited_user.save() invited_user.farms.add(farm_object) invited_user.save()
def add_users(farm, added_users): for u in added_users: email = extract_email(u) try: user = User.objects.get(email = email) farm.users.add(user) except ObjectDoesNotExist: try: invited_user = InvitedUser.objects.get(email=email) except ObjectDoesNotExist: invited_user = InvitedUser(email=email) ## TODO Add invitation invited_user.save() invited_user.farms.add(farm) invited_user.save()
'muser': user, } ) (farm2, created) = Farm.objects.get_or_create(name="test_farm_2", farmer = user, defaults = { 'cuser': user, 'muser': user, } ) # Create an invited user email = "*****@*****.**" cleanup(email) invited_user = InvitedUser(email=email, cuser_id = user.id, muser_id = user.id) invited_user.save() # Add both farms to this invited user invited_user.farms.add(farm1) invited_user.farms.add(farm2) # Create a user record from invited_user new_user = User(email=email) new_user.save() contact_info = Contact_Info(user=new_user, cuser = new_user, muser = new_user) contact_info.save()