Пример #1
0
def signup(request):
    if request.method == 'POST':
        form = signupForm(request.POST)
        if form.is_valid():
            newUser = MyUser()

            try:
                _newUser = User.objects.create_user(username=request.POST['username'], password=request.POST['password']
                                                    , first_name=request.POST['name'], email=request.POST['email'])
                newUser.user = _newUser
                newUser.birthday = request.POST['birthday_year'] + '-' + request.POST['birthday_month'] + '-' + \
                                   request.POST['birthday_day']
                newUser.save()

            except:
                form.add_error(None, 'username "' + request.POST['username'] + '" already existed')
                return render(request, 'signup.html', {'signupForm': form})

            return HttpResponseRedirect(reverse('login'))

        else:
            return render(request, 'signup.html', {'signupForm': form})

    else:
        form = signupForm()
        return render(request, 'signup.html', {'signupForm': form})
Пример #2
0
def SingUp(request):
    if request.method == 'POST':

        form = SingUpForm(request.POST)

        if form.is_valid():
            data = form.cleaned_data

            username = data['username']
            name = data['name']
            email = data['email']
            password = data['password']

            try:
                user = User.objects.create_user(username=username,
                                                email=email,
                                                first_name=name,
                                                password=password)

                myuser = MyUser(user=user, active=0)

                myuser.save()

                return redirect('Login')

            except IntegrityError:
                return render(request, 'singUp.html', {
                    'state': 'User not valid',
                    'form': form
                })
    else:
        form = SingUpForm()
        form.field_order

    return render(request, 'singUp.html', {'form': form})
Пример #3
0
def RegisterMessage(request):
    if request.method == 'POST':

        email = request.POST['email']

        exist = User.objects.filter(username=email)

        name = request.POST['name']
        last_name = request.POST['last_name']
        message = request.POST['message']

        try:
            user = User.objects.create_user(username=email,
                                            email=email,
                                            first_name=name,
                                            last_name=last_name,
                                            password=email)

            myuser = MyUser(user=user, active=0)

            myuser.save()

        except IntegrityError:
            user = authenticate(request, username=email, password=email)

        message = Message(user=myuser)
        message.message = message
        message.save()

        return render(request, 'registerMessage.html', {'state': 1})
    else:
        return render(request, 'registerMessage.html', {'state': 0})
Пример #4
0
    def create_request_debit_for_tree(self,
                                      user: MyUser,
                                      request: Request,
                                      action: int = 2):
        transactions = list()
        user_price_field = get_price_field_for_category(request.ts_category)

        for ancestors in user.get_ancestors(
                include_self=True).select_related('parent'):
            if action == 2:
                cost = getattr(ancestors.parent,
                               user_price_field) if ancestors.parent_id else 0
                subtotal = getattr(ancestors, user_price_field)
            else:
                cost = 0
                subtotal = 0
            transactions.append(
                Transaction(user_id=ancestors.pk,
                            status=1,
                            action=2,
                            request=request,
                            cost=cost,
                            subtotal=subtotal,
                            user_parent_id=ancestors.parent_id))
        return self.bulk_create(transactions)
Пример #5
0
def register(req):
    # print("django version: %s" % django.get_version())
    if req.method == 'POST':
        message = 'please recheck your information'
        username = req.POST.get("userName", None)
        email = req.POST.get("inputEmail", None)
        passwd = req.POST.get("inputPassword", None)
        passwd2 = req.POST.get("repeatPassword", None)
        if passwd != passwd2:
            message = 'password are different'
            return render(req, 'register/register.html', locals())
        else:
            if not User.objects.all().filter(email=email):
                if not User.objects.all().filter(username=username):
                    # 使用内置User自带create_user方法创建用户,不需要使用save()
                    user = User.objects.create_user(username=username,
                                                    password=passwd,
                                                    email=email)
                    # 如果直接使用objects.create()方法后不需要使用save()
                    user_profile = MyUser(user=user)
                    user_profile.userCreateTime = datetime.date.today()
                    user_profile.userBirthday = datetime.date(2017, 3, 22)
                    user_profile.userDescription = 'i am what i am'
                    # user_profile.userPermission = UserPermission.objects.create(
                    #     userPermissionID_id=3,
                    #     userType='user',
                    #     modifyOtherAccount=False,
                    #     modifyPost=False,
                    # )
                    obj = UserPermission.objects.filter(userPermissionID=3)
                    user_profile.userPermissionID = obj[0]
                    # user_profile.userPermissionID_id = 3
                    user_profile.save()

                    mailname = EmailBackend()
                    user = mailname.authenticate(email=email, password=passwd)
                    print(user)
                    if user and user.is_active:
                        auth.login(req,
                                   user,
                                   backend='users.views.EmailBackend')
                        name = user.username
                        print("user name: %s" % name)

                    # return render_to_response('member.html', {'username': username})
                    return redirect('/')
                return render(req, 'register/register.html',
                              {'message': 'username already exists'})
            return render(req, 'register/register.html',
                          {'message': 'user email already exists'})

    return render(req, "register/register.html")
Пример #6
0
    def get_available_for_user(self, user: MyUser, category: int = None):

        if user.stantion_order_type == 1:
            order = ('requests_created', 'id',)
        else:
            order = ('order', 'id',)

        user_stantion = self.filter(users__id=user.pk, active=True).order_by(*order)
        if user_stantion or user.use_only_self_stantions:
            return [stantion for stantion in user_stantion if stantion.is_allow_create(category)]
        else:
            ancestors = user.get_ancestors(ascending=True).values_list('id', 'use_only_self_stantions',
                                                                       'stantion_order_type')

            use_only_self_stantions = False

            filter_ancestor = []
            for ancestor in ancestors:
                if ancestor[1]:
                    # reset filter_ancestor and set only this user
                    filter_ancestor = [ancestor[0]]
                    use_only_self_stantions = True

                    if ancestor[2] == 1:
                        order = ('requests_created', 'id',)
                    else:
                        order = ('order', 'id',)

                    break
                else:
                    filter_ancestor.append(ancestor[0])
            if filter_ancestor:
                child_stantion = self.filter(users__id__in=filter_ancestor, active=True, is_available_for_child=True)\
                    .order_by(*order)
            if filter_ancestor and child_stantion.count():
                return [stantion for stantion in child_stantion if stantion.is_allow_create(category)]
            elif use_only_self_stantions:
                return []
            else:
                if category:
                    filter_by_category = {
                        CATEGORY_FIELD_AVAILABLE[category]: True
                    }
                else:
                    filter_by_category = {}

                return self.filter(active=True, requests_created__lt=F('daily_limit'),
                                   is_available_for_all_users=True, **filter_by_category)\
                    .filter(Q(freeze_date_end=None) | Q(freeze_date_end__lt=timezone.now()))\
                    .extra(select={'fieldsum': 'daily_limit - requests_created'}).order_by(*order)
Пример #7
0
def oauth_callback(request):
    code = request.GET['code']
    url = 'https://api.weibo.com/oauth2/access_token'
    values = {
        'client_id': '2027959579',
        'client_secret': 'f839c30fc7681ba271dd28d2356b37d4',
        'grant_type': 'authorization_code',
        'redirect_uri': 'themer.chinacloudapp.cn/callback',
        'code': code
    }
    data = urllib.parse.urlencode(values)
    data = data.encode('ascii')
    req = urllib.request.Request(url, data)
    response = urllib.request.urlopen(req)
    r = response.read().decode('utf-8')
    result = json.loads(r)
    print(result)
    access_token = result['access_token']
    uid = result['uid']
    expire_time = result['expires_in']

    d = get_info(access_token, uid)



    try:
        user = User.objects.get(username=uid)
    except:
        user = User.objects.create_user(uid, '[email protected]', '********')
        user.save()
    user = authenticate(username=uid, password='******')
    login(request, user)

    with urllib.request.urlopen(d['avatar']) as response:
        data = response.read()
        avatar = Avatar(user=user, primary=True)
        image_name = str(uuid.uuid4()) + ".jpg"
        avatar.avatar = ContentFile(data, image_name)
        avatar.save()

    try:
        ouser = MyUser.objects.get(user=user)
    except:
        ouser = MyUser(user=user)
    ouser.type = 1
    ouser.access_token = access_token
    ouser.expire_time = int(expire_time)
    ouser.username = d['name']
    ouser.save()
    return redirect('/timeline')
Пример #8
0
    def save(self):
        account = MyUser(
            phone_number=self.validated_data['phone_number'],
            email=self.validated_data['email'],
            first_name=self.validated_data['first_name'],
            last_name=self.validated_data['last_name'],
            username=self.validated_data['username'],
        )
        password = self.validated_data['password']
        password2 = self.validated_data['password2']

        if password and password2 and password != password2:
            raise serializers.ValidationError({'گذرواژه ها باید یکسان باشند'})
        account.set_password(password)
        account.save()
        return account
Пример #9
0
    def save(self):
        user = MyUser(
            email=self.validated_data['email'],
            username=self.validated_data['username'],
        )

        password1 = self.validated_data['password']
        password2 = self.validated_data['password2']

        try:
            password_validation.validate_password(password2, self.instance)
        except forms.ValidationError as error:
            raise serializers.ValidationError(
                {'password': '******'})

        if password1 and password2 and password1 != password2:
            raise serializers.ValidationError(
                {'password': '******'})

        user.set_password(password1)
        user.save()

        return user
Пример #10
0
def signup(request):
    if request.method == 'POST':
        form = signupForm(request.POST)
        if form.is_valid():
            newUser = MyUser()

            try:
                _newUser = User.objects.create_user(
                    username=request.POST['username'],
                    password=request.POST['password'],
                    first_name=request.POST['name'],
                    email=request.POST['email'])
                newUser.user = _newUser
                newUser.birthday = request.POST['birthday_year'] + '-' + request.POST['birthday_month'] + '-' + \
                                   request.POST['birthday_day']
                newUser.save()

            except:
                form.add_error(
                    None, 'username "' + request.POST['username'] +
                    '" already existed')
                return render(request, 'signup.html', {'signupForm': form})

            return render(
                request, 'signin.html', {
                    'form': loginForm(),
                    'msg': 'You have successfully registered,'
                    ' login now'
                })

        else:
            return render(request, 'signup.html', {'signupForm': form})

    else:
        form = signupForm()
        return render(request, 'signup.html', {'signupForm': form})
Пример #11
0
 def build(self):
     return MyUser(email =self.email,password =self.password)
Пример #12
0
    def setUp(self):
        permission = UserPermission.objects.create(userPermissionID=3, userType='user', modifyOtherAccount=0,
                                                   modifyPost=0)
        permission1 = UserPermission.objects.create(userPermissionID=2, userType='editor', modifyOtherAccount=0,
                                                    modifyPost=1)
        permission.save()
        permission1.save()
        user = User.objects.create_user(username='******', password='******', email='*****@*****.**')
        user_profile = MyUser(user=user)
        user_profile.userCreateTime = datetime.date.today()
        user_profile.userBirthday = datetime.date(2018, 1, 1)
        user_profile.userDescription = 'i am user test1'
        obj = UserPermission.objects.filter(userPermissionID=2)
        user_profile.userPermissionID = obj[0]
        user_profile.save()

        user = User.objects.create_user(username='******', password='******', email='*****@*****.**')
        user_profile = MyUser(user=user)
        user_profile.userCreateTime = datetime.date.today()
        user_profile.userBirthday = datetime.date(2018, 10, 10)
        user_profile.userDescription = 'i am user test2'
        obj = UserPermission.objects.filter(userPermissionID=3)
        print('obj:')
        print(obj)
        user_profile.userPermissionID = obj[0]

        user_profile.save()