def add_edit_useraccount(request, username=None, useraccount_id=None): username_error = False if username is None: # Edit user_account = get_object_or_404(UserAccount, pk=useraccount_id) user = user_account.user else: #Add user = get_object_or_404(Person, user__username=username) user_account = False if request.method == 'POST': form = UserAccountForm(request.POST) if form.is_valid(): data = form.cleaned_data if user_account: # Edit user_account.machine_category = data['machine_category'] user_account.default_project = data['default_project'] user_account.save() messages.success(request, "User account for '%s' changed succesfully" % user_account.user) return HttpResponseRedirect(user.get_absolute_url()) else: #add try: project = data['default_project'] except: project = None machine_category = data['machine_category'] try: UserAccount.objects.get( username__exact=user.username, machine_category=machine_category, date_deleted__isnull=True) except UserAccount.DoesNotExist: user_account = create_account(user, project, machine_category) messages.success(request, "User account for '%s' created succesfully" % user_account.user) return HttpResponseRedirect(user.get_absolute_url()) username_error = True else: form = UserAccountForm() from django import forms form.initial['username'] = user.username if user_account: # Fill form with initial form.fields['default_project'] = forms.ModelChoiceField(queryset=user.project_set.all()) form.initial = user_account.__dict__ form.initial['default_project'] = form.initial['default_project_id'] form.initial['machine_category'] = form.initial['machine_category_id'] return render_to_response( 'machines/useraccount_form.html', {'form': form, 'user_account': user_account, 'username_error': username_error}, context_instance=RequestContext(request))
def add_user_to_project(person, project): project.users.add(person) project.save() for mc in project.machine_categories.all(): if not person.has_account(mc): create_account(person, project, mc)