def view(request, name):
    rankings = Ranking.objects.all()

    use = User.objects.get(username=name)
    today = datetime.date.today()
    yearLoop = range(1900, today.year - 7)
    birthdate = use.participant.datenaissance
    formatedBirthdate = birthdate.strftime('%d/%m/%Y')
    terrain = Court.objects.filter(user=use)
    tournoi1 = Pair.objects.filter(user1=use, confirm=True)
    tournoi2 = Pair.objects.filter(user2=use, confirm=True)
    tournoi = list(chain(tournoi1, tournoi2))

    user_logs = LogActivity.objects.filter(section="Utilisateur", target=use.username).order_by('-date')[:10]

    if request.method == "POST":
        email = request.POST['email']
        firstname = request.POST['firstname']
        lastname = request.POST['lastname']
        gsm = request.POST['gsm']
        tel = request.POST['tel']
        fax = request.POST['fax']
        title = request.POST['title']
        boite = request.POST['boite']
        street = request.POST['street']
        number = request.POST['number']
        locality = request.POST['locality']
        postalcode = request.POST['postalcode']
        birthdate = request.POST['birthdate']
        classement = request.POST['classement']
        lat = request.POST['lat']
        lng = request.POST['lng']

        # check champs
        if firstname == "" or lastname == "" or (tel == "" and gsm == "") or street == "" or number == "" or locality == "" or postalcode == "" or birthdate == "":
            errorEdit = "Veuillez remplir tous les champs obligatoires !"
            return render(request, 'profil.html', locals())

        # check format date
        if re.match(r"^[0-3][0-9]/[0-1][0-9]/[1-2][0-9]{3}$", birthdate) is None:
            errorEdit = "La date de naissance n'a pas le bon format"
            return render(request, 'profil.html', locals())

        # On formate la date
        birthdate2 = birthdate.split("/")
        datenaissance = datetime.datetime(
            int(birthdate2[2]), int(birthdate2[1]), int(birthdate2[0]))

        use.email = email
        use.save()

        formatedBirthdate = birthdate
        participant = use.participant
        participant.titre = title
        participant.nom = lastname
        participant.prenom = firstname
        participant.rue = street
        participant.numero = number
        participant.boite = boite
        participant.codepostal = postalcode
        participant.localite = locality
        participant.telephone = tel
        participant.fax = fax
        participant.gsm = gsm
        participant.datenaissance = datenaissance
        participant.classement = Ranking.objects.get(nom=classement)
        participant.latitude = lat
        participant.longitude = lng
        participant.save()

        # Validate classement
        validate_classement_thread(participant)
        LogActivity(user=request.user, section="Utilisateur",
                target=""+use.username, details=u"Profil modifié").save()
        successEdit = "Le profil a bien été changé"

    use = User.objects.get(username=name)
    today = datetime.date.today()
    yearLoop = range(1900, today.year - 7)
    birthdate = use.participant.datenaissance
    formatedBirthdate = birthdate.strftime('%d/%m/%Y')
    terrain = Court.objects.filter(user=use)
    tournoi1 = Pair.objects.filter(user1=use, confirm=True)
    tournoi2 = Pair.objects.filter(user2=use, confirm=True)
    tournoi = list(chain(tournoi1, tournoi2))

    if request.user.is_authenticated():
        return render(request, 'viewUser.html', locals())
    return redirect(reverse(home))
Exemplo n.º 2
0
def view(request):
    rankings = Ranking.objects.all()
    today = datetime.date.today()
    yearLoop = range(1900, today.year - 7)
    try:
        request.user.participant
    except ObjectDoesNotExist:
        Participant(user=request.user, titre = "Mr", nom="", prenom="", rue="",
        numero="", boite="", codepostal="", localite="", telephone="", fax="",
        gsm="", classement=rankings.first(), oldparticipant=False,
        datenaissance=today, latitude=0.0, isClassementVerified=False,
         longitude=0.0).save()
    birthdate = request.user.participant.datenaissance
    formatedBirthdate = birthdate.strftime('%d/%m/%Y')
    if request.method == "POST":
        if request.POST['action'] == 'sendMailConfirmationMail':
            # Send email with code to finish registration and validate account
            participant = Participant.objects.get(user=request.user)
            activationObject = UserInWaitOfActivation.objects.get(
                participant=participant)
            activationObject.dayOfRegistration = datetime.datetime.now()
            activationObject.save()
            link = "http://" + request.get_host() + "/tennis/emailValidation/"
            if send_register_confirmation_email(activationObject, participant, link):
                successSendMail = u"Un email vous a été renvoyé sur votre adresse courante. En cas de non-réception, veuillez revérifier l'adresse enregistrée ci-dessous."
            else:
                echecSendMail = u"Une erreur s'est produite lors de l'envois de votre message,\nle problème a été signaler au staff et sera résolu dans les plus bref délais.\nDésole de l'inconvénience, réessayer dans quelques heures"
            return render(request, 'profil.html', locals())
        if request.POST['action'] == 'updatePassword':

            password1 = request.POST['password1']
            password2 = request.POST['password2']

            # On vérifie que les password sont les memes
            if password1 != password2:
                errorMDP = "Les mots de passes sont différents !"
                return render(request, 'profil.html', locals())

            # On vérifie la longeur du password
            if(len(password1) < 2):
                errorMDP = "Votre mot de passe doit contenir au moins 3 caractères"
                return render(request, 'profil.html', locals())

            request.user.set_password(password1)
            request.user.save()
            successMDP = "Le mot de passe a bien été changé"
            user2 = authenticate(username=request.user, password=password1)
            login(request, user2)
            return render(request, 'profil.html', locals())

        if request.POST['action'] == 'editProfil':

            firstname = request.POST['firstname']
            lastname = request.POST['lastname']
            gsm = request.POST['gsm']
            tel = request.POST['tel']
            fax = request.POST['fax']
            title = request.POST['title']
            boite = request.POST['boite']
            street = request.POST['street']
            number = request.POST['number']
            locality = request.POST['locality']
            postalcode = request.POST['postalcode']
            birthdate = request.POST['birthdate']
            classement = request.POST['classement']
            lat = request.POST['lat']
            lng = request.POST['lng']

            if request.POST.__contains__("participated"):
                oldparticipant = True
            else:
                oldparticipant = False

            # check champs
            if (firstname == "" or lastname == "" or (tel == ""
                                                      and gsm == "") or street == "" or number == "" or locality == "" or postalcode == "" or birthdate == ""):
                errorEdit = "Veuillez remplir tous les champs obligatoires !"
                return render(request, 'profil.html', locals())

            # check format date
            if re.match(r"^[0-3][0-9]/[0-1][0-9]/[1-2][0-9]{3}$", birthdate) is None:
                errorEdit = "La date de naissance n'a pas le bon format"
                return render(request, 'profil.html', locals())

            # On formate la date
            birthdate2 = birthdate.split("/")
            datenaissance = datetime.datetime(
                int(birthdate2[2]), int(birthdate2[1]), int(birthdate2[0]))

            formatedBirthdate = birthdate
            participant = request.user.participant
            participant.titre = title
            participant.nom = lastname
            participant.prenom = firstname
            participant.rue = street
            participant.numero = number
            participant.boite = boite
            participant.codepostal = postalcode
            participant.localite = locality
            participant.telephone = tel
            participant.fax = fax
            participant.gsm = gsm
            participant.datenaissance = datenaissance
            participant.classement = Ranking.objects.get(nom=classement)
            participant.oldparticipant = oldparticipant
            participant.latitude = lat
            participant.longitude = lng
            participant.save()

            # Validate classement
            validate_classement_thread(participant)

            successEdit = "Le profil a bien été changé"
            return render(request, 'profil.html', locals())

    if request.user.is_authenticated():

        return render(request, 'profil.html', locals())
    return redirect(reverse(home))
def view(request):
    today = datetime.date.today()
    yearLoop = range(1900, today.year - 7)
    rankings = Ranking.objects.all()
    if request.method == "POST":
        # Recuperation des donnees
        username = request.POST['username']
        password = request.POST['password']
        firstname = request.POST['firstname']
        lastname = request.POST['lastname']
        email = request.POST['email']
        gsm = request.POST['gsm']
        tel = request.POST['tel']
        fax = request.POST['fax']
        title = request.POST['title']
        boite = request.POST['boite']
        street = request.POST['street']
        number = request.POST['number']
        locality = request.POST['locality']
        postalcode = request.POST['postalcode']
        birthdate = request.POST['birthdate']
        classement = request.POST['classement']
        lat = request.POST['lat']
        lng = request.POST['lng']

        if request.POST.__contains__("participated"):
            oldparticipant = True
        else:
            oldparticipant = False

        # check champs
        if (username == "" or password == "" or firstname == "" or lastname == "" or email == "" or (tel == ""
                                                                                                     and gsm == "") or street == "" or number == "" or locality == "" or postalcode == "" or birthdate == ""):
            error = "Veuillez remplir tous les champs obligatoires !"
            return render(request, 'register.html', locals())

        # Check username et email already taken
        if username_present(username):
            error = "Ce nom d'utilisateur n'est plus disponible !"
            return render(request, 'register.html', locals())

        # On vérifie si l'email est deja dans la db
        if(email_present(email)):
            error = "Un compte avec cette addresse email existe déjà !"
            return render(request, 'register.html', locals())

        # check username length
        if len(username) < 2:
            error = "Votre nom d'utilisateur doit contenir au moins 3 caractères"
            return render(request, 'register.html', locals())

        # check password length
        if len(password) < 2:
            error = "Votre mot de passe doit contenir au moins 3 caractères"
            return render(request, 'register.html', locals())

        # check format date
        if re.match(r"^[0-3][0-9]/[0-1][0-9]/[1-2][0-9]{3}$", birthdate) is None:
            error = "La date de naissance n'a pas le bon format"
            return render(request, 'register.html', locals())

        # On format la date
        birthdate2 = birthdate.split("/")
        datenaissance = datetime.datetime(
            int(birthdate2[2]), int(birthdate2[1]), int(birthdate2[0]))

        # Account creation & redirect
        user = User.objects.create_user(username, email, password)
        user.save()
        # TODO verfied = FALSE
        participant = Participant(user=user, titre=title, nom=lastname, prenom=firstname, rue=street, numero=number, boite=boite, codepostal=postalcode, localite=locality, telephone=tel, fax=fax, gsm=gsm,
                                  classement=Ranking.objects.get(nom=classement), oldparticipant=oldparticipant, datenaissance=datenaissance, isClassementVerified=True, isAccountActivated=False, latitude=lat, longitude=lng)
        participant.save()

        # Create UserInWaitOfActivation object to keep track of the activation
        today = datetime.datetime.now()
        key = get_random_string(20, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
        while len(UserInWaitOfActivation.objects.filter(confirmation_key=key)) > 0:
            # Key already in user, generate new one
            key = get_random_string(20, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
        activationObject = UserInWaitOfActivation(participant=participant, dayOfRegistration=today, confirmation_key=key)
        activationObject.save()
        link = "http://" + request.get_host() + "/tennis/emailValidation/"

        # Verify user classement
        validate_classement_thread(participant)

        # Send email with code to finish registration and validate account
        send_register_confirmation_email(activationObject, participant, link)

        # On connecte l'utilisateur
        user2 = authenticate(username=username, password=password)
        login(request, user2)
        return redirect(reverse(tournoi))

    if request.user.is_authenticated():
        return redirect(reverse(tournoi))
    return render(request, 'register.html', locals())