Exemple #1
0
Fichier : te.py Projet : fasia/MUTA
 def post(self):
     form = UserCreateForm()
     if not form.validate_on_submit():
         return form.errors, 422
     user, passw= User(form.email.data, form.password.data)
     db.session.add(user)
     db.session.commit()
     return UserSerializer(user).data
    def post(self):
        form = UserCreateForm()
        if not form.validate_on_submit():
            return form.errors, 422

        user = User(form.email.data, form.password.data)
        db.session.add(user)
        db.session.commit()
        return UserSerializer(user).data
Exemple #3
0
    def post(self):
        form = UserCreateForm()
        if not form.validate_on_submit():
            return form.errors, 422

        user = User(form.username.data, form.password.data)
        db.session.add(user)
        db.session.commit()
        return jsonify({'username':user.username})
Exemple #4
0
    def post(self, request):
        session_form_data = request.session.get("form_data")
        if session_form_data is None:
            return redirect("registration_input")

        captcha = self.request.POST.get("g-recaptcha-response")
        if captcha:
            auth_url = (
                "https://www.google.com/recaptcha/api/siteverify?secret={}&response={}"
            )
            auth_url = auth_url.format(settings.RECAPTCHA_SECRET_KEY, captcha)
            response = requests.get(auth_url)
            if not response.json().get("success"):
                request.session["recaptcha"] = True
                return redirect("registration_confirm")
        else:
            request.session["recaptcha"] = True
            return redirect("registration_confirm")

        del request.session["form_data"]

        user_form = UserCreateForm(session_form_data)
        person_form = PersonCreateForm(session_form_data)

        if user_form.is_valid() and person_form.is_valid():

            user = user_form.save(commit=False)
            user.is_active = False
            user.last_name = person_form.cleaned_data.get("last_name")
            user.first_name = person_form.cleaned_data.get("first_name")
            user.save()

            person = person_form.save(commit=False)
            person.user = user
            person.save()

            current_site = get_current_site(self.request)
            domain = current_site.domain
            context = {
                "protocol": self.request.scheme,
                "domain": domain,
                "token": dumps(user.pk),
                "user": user,
            }

            subject = render_to_string("app/mail/registration_subject.txt",
                                       context).strip()
            message = render_to_string("app/mail/registration_message.txt",
                                       context).strip()
            user.email_user(subject, message, settings.EMAIL_HOST_USER)

            request.session["notification"] = Notification.REGISTRATION_CONFIRM

            return redirect("index")

        return redirect("registration_input")
Exemple #5
0
def create_user():
    form = UserCreateForm()
    if form.validate_on_submit():
        user = User()
        return change_user(form, user, 'created')
    context = get_context()
    return _render_template('generic_form.html',
                            title='New User',
                            form=form,
                            **context)
Exemple #6
0
 def post(self, request):
     form = UserCreateForm(request.POST)
     if form.is_valid():
         form.save()
         username = request.POST["username"]
         request.session["temp_user"] = {
             'username': username,
             'new': True,
         }
         return redirect('/accounts/register/done/')
     else:
         return render(request, self.template_name, {'user_create_form': form})
Exemple #7
0
 def post(self, request):
     form = UserCreateForm(request.POST)
     if form.is_valid():
         form.save()
         username = request.POST["username"]
         request.session["temp_user"] = {
             'username': username,
             'new': True,
         }
         return redirect('/accounts/register/done/')
     else:
         return render(request, self.template_name,
                       {'user_create_form': form})
Exemple #8
0
def signup(request):
    user_form = UserCreateForm(data=request.POST)
    if request.method == 'POST':
        if user_form.is_valid():
            username = user_form.cleaned_data['username']
            password = user_form.cleaned_data['password2']
            user_form.save()
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('/')
        else:
            return index(request, user_form=user_form)
    return redirect('/')
Exemple #9
0
def signup():
    form = UserCreateForm()
    if request.method == 'POST' and form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if not user:
            user = User(username=form.username.data,
                        password=generate_password_hash(form.password1.data),
                        email=form.email.data)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('main.index'))
        else:
            flash('이미 존재하는 사용자입니다.')
    return render_template('auth/signup.html', form=form)
Exemple #10
0
    def post(self, request):
        user_form = UserCreateForm(request.POST)
        person_form = PersonCreateForm(request.POST)

        if user_form.is_valid() and person_form.is_valid():
            request.session["form_data"] = request.POST
            return redirect("registration_confirm")

        context = {
            "user_form": user_form,
            "person_form": person_form,
        }

        return render(request, "app/registration/input.html", context)
Exemple #11
0
def index(request, auth_form=None, user_form=None):
    # User is logged in
    if request.user.is_authenticated():
        ribbit_form = RibbitForm()
        user = request.user
        ribbits_self = Ribbit.objects.filter(user=user.id)
        ribbits_buddies = Ribbit.objects.filter(
            user__userprofile__in=user.profile.follows.all())
        ribbits = ribbits_self | ribbits_buddies

        return render(
            request, 'buddies.html', {
                'ribbit_form': ribbit_form,
                'user': user,
                'ribbits': ribbits,
                'next_url': '/',
            })
    else:
        # User is not logged in
        auth_form = auth_form or AuthenticateForm()
        user_form = user_form or UserCreateForm()

        return render(request, 'home.html', {
            'auth_form': auth_form,
            'user_form': user_form,
        })
Exemple #12
0
    def get(self, request):
        session_form_data = request.session.get("form_data")
        if session_form_data is None:
            return redirect("registration_input")

        user_form = UserCreateForm(session_form_data, label_suffix="")
        person_form = PersonCreateForm(session_form_data, label_suffix="")

        if "recaptcha" in request.session:
            notification = Notification.RECAPTCHA_ERROR
        else:
            notification = Notification.RECAPTCHA_CONFIRM

        context = {
            "user_form":
            user_form,
            "person_form":
            person_form,
            # get_xxx_displayを使う方法が見つからない。
            "gender":
            dict(person_form.fields["gender"].choices)[int(
                person_form.data["gender"])],
            "prefecture":
            dict(person_form.fields["prefecture_id"].choices)[int(
                person_form.data["prefecture_id"])],
            "recaptcha_public_key":
            settings.RECAPTCHA_PUBLIC_KEY,
            "notification":
            notification,
        }
        return render(request, "app/registration/confirm.html", context)
Exemple #13
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # return data
        context['page'] = 'users'
        context['form'] = UserCreateForm()
        context['form_profile'] = ProfileCreateForm()

        return context
Exemple #14
0
def create_user():
    form = UserCreateForm()
    if form.validate_on_submit():
        user = User()
        user.username = form.username.data
        user.email = form.email.data
        user.password = form.password.data
        user.is_admin = form.is_admin.data
        user.is_active = form.is_active.data

        role = Role.query.get(form.role.data)
        user.role = role

        db.session.add(user)
        db.session.commit()

        return redirect(url_for('users'))

    return render_template('create_user.html', form=form)
Exemple #15
0
    def post(self, request, **kwargs):
        context = super().get_context_data(**kwargs)

        form = UserCreateForm(request.POST, request.FILES)
        form_profile = ProfileCreateForm(request.POST)

        try:
            if form.is_valid():
                # создать профиль
                profile = Profile.objects.create(
                    department=form_profile.data.get('department'),
                    secure_code=form_profile.data.get('secure_code'),
                    status=form_profile.data.get('status'),
                )

                # создать юзера
                photo = None
                if request.FILES['photo'] is not None:
                    photo = 'photo/' + request.FILES['photo'].name

                User.objects.create(
                    first_name=form.data.get('first_name'),
                    last_name=form.data.get('last_name'),
                    email=form.data.get('email'),
                    profile=profile,
                    photo=photo,
                )

                # сохранить фото
                if photo is not None:
                    fs = FileSystemStorage()
                    fs.save(photo, request.FILES['photo'])

                return redirect(reverse('app:users'))

        except Exception as error:
            context['error'] = str(error)

        context['form'] = form
        context['form_profile'] = form_profile

        return render(request, self.template_name, context)
Exemple #16
0
def confirm_email(token):
    email = None
    try:
        email = confirm_token(token)
    finally:
        if email is None:
            flash('The confirmation link is invalid or has expired.', 'danger')
            return redirect('/')

    form = UserCreateForm()
    form.email.data = email
    if request.method == 'POST' and form.validate_on_submit():
        email = User.query.filter_by(email=form.email.data).first()
        nickname = User.query.filter_by(email=form.nickname.data).first()

        if not email and not nickname:
            user = User(
                email=form.email.data,
                password=generate_password_hash(form.password1.data),
                student_name=form.student_name.data,
                student_number=form.student_number.data,
                nickname=form.nickname.data,
                authority=True,
                authority_type=0,  # User
                registered_on=datetime.datetime.now())
            db.session.add(user)
            db.session.commit()
            flash('Registration finished. Please login.', category='info')
            return redirect('/')
        else:
            if email: flash('The email already exists.', category='danger')
            if nickname:
                flash('The nickname already exists.', category='danger')
            return redirect('/')

    return render_template('/main/auth/register_user.html', form=form)
Exemple #17
0
def signup(request):
    '''
    Signup stuff.
    '''
    if request.method == 'POST':
        form = UserCreateForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            profile = Profile.objects.create(user=user)
            login(request, user)
            return redirect('/')
    else:
        form = UserCreateForm()
    return render(request, 'signup.html', {'form': form})
Exemple #18
0
def register(request):
    if request.method == 'POST':
        user_form = UserCreateForm(data=request.POST)

        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            registered = True
        else:
            print(user_form.errors)
    else:
        user_form = UserCreateForm()
    return render(request, 'app/cadastro.html', {'user_form': user_form})
Exemple #19
0
def register_page(request):
    if request.method == 'POST':
        user_form = UserCreateForm(request.POST)
        candidate_form = SignUpForm(request.POST)
        if user_form.is_valid() and candidate_form.is_valid():
            user = user_form.save()
            candidate = candidate_form.save(commit=False)
            candidate.user = user
            candidate.save()
            return redirect('home_page')
    else:
        user_form = UserCreateForm()
        candidate_form = SignUpForm()
    return render(request, 'register.html', {
        'user_form': user_form,
        'candidate_form': candidate_form
    })
Exemple #20
0
 def get(self, request):
     user_form = UserCreateForm(request.session.get("form_data"))
     person_form = PersonCreateForm(request.session.get("form_data"))
     context = {"user_form": user_form, "person_form": person_form}
     return render(request, "app/registration/input.html", context)
Exemple #21
0
 def get(self, request, *args, **kwargs):
     form = UserCreateForm()
     return render(request, self.template_name, {'user_create_form': form})