def confirm_group_request(request, group_request_id): if not request.user.is_authenticated(): return HttpResponseRedirect("/welcome/") if not request.user.is_superuser: messages.error(request, _(u"Недостаточно прав")) return HttpResponseRedirect("/") if request.method == "POST": group_request = get_object_or_404(RequestNewGroup, id=group_request_id) if group_request.Complete == 0: group = Groups.objects.filter(Title=group_request.Title, Local_Admin=group_request.User) if not group: group = Groups(Title=group_request.Title, Local_Admin=group_request.User) group.save() u2g = User2Group(Group=group, Member=group_request.User) u2g.save() list_usernames = re.split("[\,]*\s*", group_request.Members) print(group_request.Members, list_usernames) for username in list_usernames: if username != "": user = User.objects.filter(username=username) if user: user = user[0] u2g = User2Group.objects.get_or_create(Group=group, Member=user) group_request.Complete = RequestNewGroup.COMPLETE else: group_request.Comment_Admin += _(u"Пользователь " + username + u" не найден \n") else: group_request.Comment_Admin += _(u"Группа " + group[0].Title + u" уже существует\n") group_request.Complete = RequestNewGroup.COMPLETE_WITH_ERROR group_request.save() return HttpResponseRedirect("/groups/new_requests/")
def new_group(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/welcome/") if not request.user.is_superuser: messages.error(request, _(u"Недостаточно прав")) return HttpResponseRedirect("/") users = User.objects.all() data_user = [u.username for u in users] # if u.visible == 0 data_user = str(data_user).replace("'", '"').replace('u"', '"') if request.method == "POST": form = NewGroup(request.POST, users=data_user) if form.is_valid(): title = form.cleaned_data.get("title", None) local_admin = get_object_or_404(User, username=form.cleaned_data.get("local_admin")) group = Groups.objects.filter(Title=title, Local_Admin=local_admin) if not group: group = Groups(Title=title, Local_Admin=local_admin) group.save() u2g = User2Group(Group=group, Member=local_admin) u2g.save() mess = _(u"Группа " + title.encode("utf-8") + " успешно создана") messages.success(request, mess) return HttpResponseRedirect("/groups/" + str(group.id) + "/add_members") else: messages.warning(request, _(u"Группа " + title.encode("utf-8") + " уже существует")) return HttpResponseRedirect(request.META["HTTP_REFERER"]) else: form = NewGroup(users=data_user) template = get_template("new_group.html") context = RequestContext( request, { "form": form, #'groups': groups, }, ) return HttpResponse(template.render(context))
def registr(request): if request.user.is_authenticated(): return HttpResponseRedirect("/welcome/") errors_registr = [] form = RegForm(request.POST or None) context = {"form": form, "errors": errors_registr, "user": request.user} if request.method == "POST" and form.is_valid(): username = form.cleaned_data.get("username", None) password = form.cleaned_data.get("password", None) check_password = form.cleaned_data.get("check_password", None) email = form.cleaned_data.get("email", None) visible = 1 # form.cleaned_data.get('visible', 0) if password != check_password: errors_registr.append(_(u"Пароли не совпали")) login = User.objects.filter(username=username.lower()) if len(login) > 0: errors_registr.append(_(u"Имя занято")) if errors_registr: return render(request, "registr.html", context) user = User.objects.create_user( username=username.lower(), email=email.lower(), password=password, visible=visible ) user.save() group = Groups(Title=_(u"Локальная"), Local_Admin=user) group.save() u2g = User2Group(Group=group, Member=user) u2g.save() return HttpResponseRedirect("/login/") return render(request, "registr.html", context)