예제 #1
0
 def page(o):
     """
     Page
         *) /page/<index>/<limit>
         *) /page/<index>/<limit>/<flag_count>
     <index> = Page Index
     :return:
     """
     if not BasicApi.is_allow_permission(o, 'page'):
         raise BasicApi.NotAllowed('Invalid permission')
     l = len(o.path)
     index = 1
     if l > 0:
         index = int(o.path[0])
     limit = BasicDao.DEFAULT_LIMIT
     if l > 1:
         limit = int(o.path[1])
     count = BasicDao.DEFAULT_COUNT
     if l > 2:
         scount = str(o.path[2]).strip().lower()
         count = '1' == scount or 'true' == scount
     return BasicDao.page({
         'model': o.model,
         'filter': o.filter,
         'field': o.field,
         'order': o.order,
         'data': o.data,
         'page': {
             'index': index,
             'limit': limit,
             'count': count
         },
     })
예제 #2
0
 def create(o):
     """
     Create
         *) /create
     :return:
     """
     if not BasicApi.is_allow_permission(o, 'create'):
         raise BasicApi.NotAllowed('Invalid permission')
     return BasicDao.create({'model': o.model, 'data': o.data})
예제 #3
0
 def delete(o):
     """
     Delete
         *) /delete
         *) /delete/<pk>
     :return:
     """
     if not BasicApi.is_allow_permission(o, 'delete'):
         raise BasicApi.NotAllowed('Invalid permission')
     l = len(o.path)
     pk = None
     if l > 0:
         pk = o.path[0]
     else:
         pk = o.data.pk
     return BasicDao.delete({'model': o.model, 'pk': pk})
예제 #4
0
 def get(o):
     """
     Get
         *) /get
         *) /get/<pk>
     :return:
     """
     if not BasicApi.is_allow_permission(o, 'get'):
         raise BasicApi.NotAllowed('Invalid permission')
     l = len(o.path)
     pk = None
     if l > 0:
         pk = o.path[0]
     else:
         pk = o.data.pk
     return BasicDao.get({
         'model': o.model,
         'pk': pk,
         'filter': o.filter,
         'field': o.field
     })
예제 #5
0
 def list(o):
     """
     List
         *) /list
         *) /list/<limit>
     :return:
     """
     if not BasicApi.is_allow_permission(o, 'list'):
         raise BasicApi.NotAllowed('Invalid permission')
     l = len(o.path)
     limit = BasicDao.DEFAULT_LIMIT
     if l > 0:
         limit = int(o.path[0])
     return BasicDao.list({
         'model': o.model,
         'filter': o.filter,
         'field': o.field,
         'order': o.order,
         'data': o.data,
         'limit': limit
     })
예제 #6
0
파일: auth.py 프로젝트: thomson470/public
def login(request):
    view_items = request.view_items
    # rest
    if view_items.is_rest:
        formatter = view_items.formatter
        if view_items.use_session and request.user.is_authenticated:
            data = Result.success("LOGGED_IN")
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        parameters = view_items.parameters
        username = ''
        if 'username' in parameters:
            username = parameters['username'][0]
        password = ''
        if 'password' in parameters:
            password = parameters['password'][0]
        if username == '' or password == '':
            data = Result.error(BasicView.CODE_USER_PASS_REQUIRED, 'username and password are required')
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        authUser = BasicDao.get({'model': AuthUser, 'filter': {'username': username}})
        if authUser is None:
            data = Result.error(BasicView.CODE_USER_NOT_FOUND, 'User is not found')
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        if not authUser.is_active:
            data = Result.error(BasicView.CODE_USER_INACTIVE, 'User is not active')
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        pwd_valid = check_password(password, authUser.password)
        if not pwd_valid:
            data = Result.error(BasicView.CODE_INVALID_PASSWORD, 'Invalid password')
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        authUser.last_login = datetime.now()
        authUser.save()
        if view_items.use_session:
            auth_login(request, authUser)
            data = Result.success()
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
        else:
            key = str(uuid.uuid1())
            user = AccessUser(user=authUser)
            secret = BasicView.create_secret(request)
            access = Access(user=user, key=key, secret=secret)
            saved = Access.create(key, access)
            if not saved:
                data = Result.error(BasicView.CODE_ACCESS_KEY_REG_FAIL, 'Failed to register Access Key')
                return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
            del access.user.password
            del access.secret
            data = Result.success(access)
            return BasicView.quick_return(formatter=formatter, data=data, nullable=False)
    # web
    else:
        settings = view_items.settings[0]
        redirect_to = ''
        if REDIRECT_FIELD_NAME in view_items.parameters:
            redirect_to = view_items.parameters[REDIRECT_FIELD_NAME][0]
        if request.user.is_authenticated:
            if not '' == redirect_to:
                redirect_to = base64.urlsafe_b64decode(redirect_to).decode()
            if redirect_to == settings['path']['login'] or redirect_to == '':
                return HttpResponseRedirect(settings['path']['home'])
            return HttpResponseRedirect(redirect_to)
        if 'POST' == request.method:
            form = AuthenticationForm(request, data=request.POST)
            if form.is_valid():
                auth_login(request, form.get_user())
                return HttpResponseRedirect(settings['path']['login'] + '?' + REDIRECT_FIELD_NAME + '=' + redirect_to)
        else:
            form = AuthenticationForm(request)

        current_site = get_current_site(request)
        context = {
            'form': form,
            REDIRECT_FIELD_NAME: redirect_to,
            'site': current_site,
            'settings': settings,
            'request': request
        }
        template = view_items.template.login
        return BasicView.quick_return(template=template, context=context, is_rest=False)