def check_all_data(request, repo):
    def errorHandle(error, form):
        form = form
        return render_to_response('check_all_data.html', {
                'error' : error,
                'form' : form,
        })
    if not check_premissions(request,'VC-HD-Access'):
        return HttpResponseRedirect("/request/")
    if request.method == 'POST':

        try:
            tmp = ProjectCreation.objects.get(shortname=repo)
            form = ProjectForm(data = request.POST, instance = tmp)
        except:
            raise Http404

        if form.is_valid():
            if form.cleaned_data['use_existing_account']==True:#if user uses existing librarian
                form.cleaned_data['status']='requested_repository'#we don't need to create another one so we push project to 'create_repository'
                form.save()
                return HttpResponseRedirect("/HD/"+tmp.shortname+"/create_repository/")
            else:
                form.cleaned_data['status']='requested_account'#if not we must first create a librarian account in 'create_librarian'
                form.save()
                return HttpResponseRedirect("/HD/"+tmp.shortname+"/create_librarian/")
        else:
            errorHandle("Form is not valid",form)
    else:
        try:
            tmp = ProjectCreation.objects.get(shortname=repo)
        except:
            raise Http404

        form=ProjectForm(instance=tmp)
        if tmp.status=="requested":
            return render_to_response('check_all_data.html', {'form':form})
        elif tmp.status=="requested_repository":
            return errorHandle("Account created. go to status page!", None)
        elif tmp.status=="awaiting_account":
            return errorHandle("Awaiting for account!", None)
        elif tmp.status=="active":
            return errorHandle("Repository already created and active!", None)
        else:
            return errorHandle("Unknown status of this repository", None)
def index(request):
    def errorHandle(error, form):
        # form = ProjectForm()
        return render_to_response("index.html", {"error": error, "form": form})

    if request.method == "POST":
        form = ProjectForm(request.POST)

        if form.is_valid():  # basic check

            if not form.cleaned_data["status"] == "requested":
                form.cleaned_data["status"] = "requested"

            # more checks
            if not form.ValidateShortname():
                return errorHandle("Shortname can contain only letters and numbers", form)

            if not form.ValidateQuota():
                return errorHandle("quota must be a number between 50 and 10000 MB ", form)

            if not check_user(form.cleaned_data["requestorafs"]):
                return errorHandle("user " + form.cleaned_data["requestorafs"] + " does not exists", form)

            account = form.cleaned_data["requestorafs"]

            if check_user(form.cleaned_data["libuser"]):
                form.cleaned_data["use_existing_account"] = True
            else:
                if form.cleaned_data["personal"] == True:
                    form.cleaned_data["libuser"] = form.cleaned_data["requestorafs"]
                    form.cleaned_data["use_existing_account"] = True
                else:
                    form.cleaned_data["libuser"] = "******" + form.cleaned_data["shortname"].lower()
                    form.cleaned_data["use_existing_account"] = False

            try:
                if Project.objects.get(shortname=form.cleaned_data["shortname"]):
                    return errorHandle("Repository with this shortname already exists", form)

            except Project.DoesNotExist:
                pass

            if not len(form.cleaned_data["useradmin"]) == 0:
                users = form.cleaned_data["useradmin"].split(" ")
                for user in users:
                    if not check_user(user):
                        return errorHandle("user " + user + " does not exist", form)

            if not len(form.cleaned_data["userwrite"]) == 0:
                users = form.cleaned_data["userwrite"].split(" ")
                for user in users:
                    if not check_user(user):
                        return errorHandle("user " + user + " does not exist", form)

            if not len(form.cleaned_data["userread"]) == 0:
                users = form.cleaned_data["userread"].split(" ")
                for user in users:
                    if not check_user(user):
                        return errorHandle("user " + user + " does not exist", form)

            if request.POST.getlist("condition"):

                form.cleaned_data["useradmin"] = form.cleaned_data["useradmin"].strip()
                form.cleaned_data["useradmin"] = replace(form.cleaned_data["useradmin"], " ", ":")

                form.cleaned_data["userread"] = form.cleaned_data["userread"].strip()
                form.cleaned_data["userread"] = replace(form.cleaned_data["userread"], " ", ":")

                form.cleaned_data["userwrite"] = form.cleaned_data["userwrite"].strip()
                form.cleaned_data["userwrite"] = replace(form.cleaned_data["userwrite"], " ", ":")

                form.save()
                # sending emails
                subject = (
                    "SVN: "
                    + form.cleaned_data["shortname"]
                    + " project requested (librarian: "
                    + form.cleaned_data["requestorafs"]
                    + ")"
                )
                body = (
                    "A new SVN repository for "
                    + form.cleaned_data["longname"]
                    + " was requested. In order to create it, go to:\n https://svnweb.cern.ch/admin/admin/create.php?status=requested&id=$id\n\n Once the librarian account is created, go to:\n https://svnweb.cern.ch/admin/admin/create.php?status=readytocreate-newaccount&id=$id\n\n Project creation Status board: https://svnweb.cern.ch/status/status.cgi\n\n"
                )  # stil old address
                creator = account + "@cern.ch"
                response = '<font color="green"><b>Request sent to IT Help Desk.</b></font><p>'
                try:
                    send_mail(subject, body, creator, [admins, helpdesk], fail_silently=False)
                except:
                    response += '<br /> <font color="red">Cannot send email to Help Desk</font>'

                subject = "SVN: new project requested"
                body = (
                    "This message is to confirm that you have requested a creation of a SVN repository for "
                    + form.cleaned_data["longname"]
                    + ". Once the repository is created, you will receive all necessary information by e-mail.\n\nIf you have any questions, send an e-mail to [email protected] or consult the SVN Service web page: http://cern.ch/svn\n\nSVN Administators"
                )
                mail = EmailMessage(subject, body, admins, [creator], headers={"Reply-To": helpdesk})
                try:
                    mail.send()
                except:
                    response += '<br /> <font color="red">Cannot send email to You</font>'

                return HttpResponse(response)
            else:
                return errorHandle("You must agree to conditions", form)
        else:
            return errorHandle("Form is not Valid", form)
    else:
        form = ProjectForm()
        user = ""
        try:
            user = request.META["ADFS_LOGIN"]
        except:
            if settings.DEBUG:
                user = ""
            else:
                return errorHandle("Cannot determine your username!", form)

        form = ProjectForm(initial={"requestorafs": user})
        return render_to_response("index.html", {"form": form})