Ejemplo n.º 1
0
def resgister(request):
    if request.method=="GET":
        return render(request,'users/register.html', {'errmsg': ''})

    if request.method=="POST":
        username = request.POST.get('user_name')
        password = request.POST.get('pwd')
        cpassword = request.POST.get('cpwd')
        email = request.POST.get('email')
        if not all([username, password, email]):
            # 有数据为空
            return render(request, 'users/register.html', {'errmsg': '参数不能为空!'})

        if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email):
            # 邮箱不合法
            return render(request, 'users/register.html', {'errmsg': '邮箱不合法!'})
        if password!=cpassword:
            return render(request, 'users/register.html', {'errmsg': '两次密码不一致!'})
        try:
            new_data = Users.objects.order_by('-id')[:1]
            if len(new_data)>=1:
                newuid=new_data[0].userid+1
                passport = Users(username=username, password=password, email=email,userid=newuid)
            else:
                passport = Users(username=username, password=password, email=email)
            passport.save()
        except Exception as e:
            print("e: ", e)  # 把异常打印出来
            return render(request, 'users/register.html', {'errmsg': '用户名已存在!'})
        return render(request, 'users/login.html')
Ejemplo n.º 2
0
def join(request):
    user = Users()
    user.name = request.POST['name']
    user.email = request.POST['email']
    user.password = request.POST['password']
    user.gender = request.POST['gender']
    user.save()

    return HttpResponseRedirect('/user/joinsuccess')
Ejemplo n.º 3
0
 def create(self, validated_data):
     u = Users(
         first_name=validated_data['first_name'],
         last_name=validated_data['last_name'],
         # birthday = validated_data['birthday'],
         # number_of_friends = validated_data['number_of_frineds']
     )
     u.save()
     return u
Ejemplo n.º 4
0
def register(request):
    user_name = request.GET['user_name']
    password = request.GET['password']
    try:
        user = Users(user_name=user_name, password=password)
        user.save()
        status = 200  # 返回注册成功的编号
    except:
        status = 100  # 返回注册失败的编号
    return HttpResponse(json.dumps({'status': status}))
Ejemplo n.º 5
0
    def _create_user(self, username: str, password: str):
        salt = 'blahfffffj349feiblah123'
        signer = Signer(salt=salt)

        user = Users()
        user.user_id = 324
        user.first_name = 'Billy'
        user.last_name = 'Bobtest'
        user.user_name = username
        user.last_login_date = timezone.now()
        user.password_hash = signer.signature(password)
        user.salt_hash = salt
        user.save()

        return user
Ejemplo n.º 6
0
 def create(self, data):
     # hash_password = hashlib.md5(data['password'].encode()).hexdigest()
     # changed_data['password'] = hash_password
     # changed_data = data
     # del changed_data['groups']
     # del changed_data['user_permissions']
     u = Users(
         first_name=data['first_name'],
         last_name=data['last_name'],
         username=data['username'],
         password=data['password'],
         email=data['email'],
     )
     u.set_password(data['password'])
     u.save()
     return u
Ejemplo n.º 7
0
    def _create_user(self, username: str, password: str, userid: int):
        salt = 'blahfffff{}j349'.format(password)
        signer = Signer(salt=salt)

        user = Users()
        user.user_id = userid
        user.first_name = 'Billy'
        user.last_name = 'Bobtest'
        user.user_name = username
        user.email = '{}@gmail.com'.format(username)
        user.about = "This is about me and the things I like"
        user.last_login_date = timezone.now()
        user.password_hash = signer.signature(password)
        user.salt_hash = salt
        user.save()

        return user
Ejemplo n.º 8
0
 def post(self, request):  # 注册页面视图
     # 接收参数
     data = request.POST
     # 表单验证合法
     form = RegisterModelForm(data)
     if form.is_valid():
         # 操作数据库
         clened_data = form.cleaned_data
         # 创建一个用户
         user = Users()
         user.phone = clened_data.get('phone')
         user.password = set_password(clened_data.get('password'))
         user.save()
         return redirect('user:登录')
     # 错误
     else:
         return render(request, 'user/reg.html', context={'form': form})
Ejemplo n.º 9
0
 def post(self, request):
     # 完成用户信息的注册
     # 接收参数
     data = request.POST
     # 验证参数合法性 表单验证
     form = RegisterModelForm(data)
     if form.is_valid():
         # 获取清洗后的数据
         cleaned_data = form.cleaned_data
         # 保存数据库
         user = Users()
         user.phone = cleaned_data.get('phone')
         user.password = set_password(cleaned_data.get('password'))
         user.save()
         return redirect('user:登录')
     else:
         return render(request, 'user/reg.html', context={'form': form})
Ejemplo n.º 10
0
    def setUpClass(cls):
        super().setUpClass()
        salt = 'blahfffffj349feiblah123'
        signer = Signer(salt=salt)

        user = Users()
        user.user_id = 324
        user.first_name = 'Billy'
        user.last_name = 'Bobtest'
        user.user_name = 'myUsername'
        user.last_login_date = timezone.now()
        user.password_hash = signer.signature('password123')
        user.salt_hash = salt
        user.save()

        cls.user = user
        cls.client = Client()
Ejemplo n.º 11
0
def register(request):
    if request.method == "POST":
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            username = form.cleaned_data.get('username')
            login(request, user)
            current_user = request.user
            profile = Users(user_id=current_user.id,
                            bio=form.cleaned_data.get('Bio'))
            profile.save()
            messages.info(request,
                          "You are now logged in as {}".format(username))
            return HttpResponseRedirect('/home')
        else:
            for msg in form.error_messages:
                messages.error(request, "{msg}: {form.error_messages[msg]}")
    else:
        form = SignUpForm()
    return (render(request, "register.html", context={"form": form}))
Ejemplo n.º 12
0
def user_manage(request, aid=None, action=None):
    if request.user.has_perms(['asset.view_user', 'asset.edit_user']):
        page_name = ''
        if aid:
            user = get_object_or_404(Users, pk=aid)
            if action == 'edit':
                page_name = '编辑用户'
            elif action == 'delete':
                if user == request.user:
                    Message.objects.create(type=u'用户管理',
                                           user=request.user,
                                           action=u'删除用户',
                                           action_ip=UserIP(request),
                                           content=u'不能删除当前登录用户')
                else:
                    user.delete()
                    Message.objects.create(
                        type=u'用户管理',
                        user=request.user,
                        action=u'删除用户',
                        action_ip=UserIP(request),
                        content=u'删除用户 %s%s,用户名 %s' %
                        (user.last_name, user.first_name, user.username))
                return redirect('user_list')
            elif action == 'view':
                user_detail = Users.objects.filter(id=aid)
                return render(request, 'user/user_profile.html',
                              {'user_detail': user_detail})

        else:
            user = Users()
            action = 'add'
            page_name = '新增用户'

        if request.method == 'POST':
            form = UserForm(request.POST, instance=user)
            if form.is_valid():
                password1 = request.POST.get('password1')
                password2 = request.POST.get('password2')
                perm_select = request.POST.getlist('perm_sel')
                perm_delete = request.POST.getlist('perm_del')
                if action == 'add' or action == 'edit':
                    form.save()
                    if password1 and password1 == password2:
                        user.set_password(password1)
                    user.save()
                    # 授予用户权限
                    user.user_permissions.add(*perm_select)
                    user.user_permissions.remove(*perm_delete)
                    Message.objects.create(type=u'用户管理',
                                           user=request.user,
                                           action=page_name,
                                           action_ip=UserIP(request),
                                           content=u'%s %s%s,用户名 %s' %
                                           (page_name, user.last_name,
                                            user.first_name, user.username))
                    return redirect('user_list')
        else:
            form = UserForm(instance=user)
        data = {
            'form': form,
            'page_name': page_name,
            'action': action,
            'aid': aid
        }

        return render(request, 'user/user_manage.html', data)
    else:
        raise Http404
Ejemplo n.º 13
0
    def post(self, request: HttpRequest):
        try:
            request_json = json.loads(request.body.decode('UTF-8'))
        except json.JSONDecodeError:
            return JSONResponse.new(
                code=400,
                message='request decode error, bad data sent to the server')

        # these are required keys
        _user_name = request_json.get('username')
        _first_name = request_json.get('firstname')
        _last_name = request_json.get('lastname')
        _password = request_json.get('password')

        try:
            self._check_required_inputs(
                [_user_name, _first_name, _last_name, _password])
        except ValueError as err:
            return JSONResponse.new(code=400, message='{}'.format(err.args[0]))

        try:
            Users.objects.get(user_name__exact=_user_name)
        except ObjectDoesNotExist:
            # GOOD, lets create a new user
            new_user = Users()
            salt = token_hex(16)
            signer = Signer(salt=salt)

            new_user.user_id = uuid4().time_mid
            new_user.first_name = _first_name
            new_user.last_name = _last_name
            new_user.user_name = _user_name
            new_user.salt_hash = salt
            new_user.password_hash = signer.signature(_password)
            new_user.email = request_json.get(
                'email', '{}@noemail.set'.format(_user_name))
            new_user.about = request_json.get('about', '')
            new_user.last_login_date = timezone.now()
            new_user.is_active = True

            if request_json.get('profilepic') is not None:
                aws = AWS('snap-life')
                key_name = '{}.png'.format(request_json.get('profilepic'))
                url = aws.upload_profile_image(new_user.user_name, key_name)

                new_user.profile_url = url
            else:
                new_user.profile_url = 'static/assets/usericon.png'

            try:
                new_user.save()
                request.session['{}'.format(new_user.user_id)] = True
            except IntegrityError as err:
                # if this is because we have a collision with our random numbers
                # hash, userID etc. re-create them
                del request.session['{}'.format(new_user.user_id)]
                return JSONResponse.new(
                    code=500, message='username and email need to be unique')

        else:
            return JSONResponse.new(
                code=400,
                message='username {} is already taken'.format(_user_name))

        return JSONResponse.new(code=200,
                                message='success',
                                userid=new_user.user_id)
Ejemplo n.º 14
0
def index(request):
    user = Users(u_name="zc")    # 读者
    user.save()
    author = Authors(name="施耐庵")     # 作者
    author.save()
    return HttpResponse("success")