Beispiel #1
0
    def on_list(self, request):
        res = {'error': 1}
        if request.method == 'GET':
            keyword = request.args.get('keyword')
            user_id = request.args.get('user_id')
            page = cvtools.num(request.args.get('page'))
            per_page = cvtools.num(request.args.get('per_page'))
            images, count = cvtools.get_list(keyword, user_id, None, "i.created_at DESC", page, per_page)
            result = []
            for img in images:
                temp = {}
                temp['ar_id'] = img['id']
                temp['src_img'] = cvtools.get_abs_url(conf.UPLOAD_DIR_SRC[1:], img['src_name'])
                temp['target_type'] = img['target_type']
                if temp['target_type'] == conf.TAR_TYPE_IMAGE:
                    temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_IMG[1:], img['target'])
                elif temp['target_type'] == conf.TAR_TYPE_VIDEO:
                    temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_VIDEO[1:], img['target'])
                else:
                    temp['target'] = img['target']
                temp['title'] = img['title']
                temp['user'] = img['username']
                temp['created_at'] = img['created_at'].isoformat()
                result.append(temp)

            res['error'] = 0
            res['result'] = result
            res['total'] = count
        else:
            res['error_msg'] = 'request is not GET'

        response_str = json.dumps([res])
        return Response(response_str, mimetype='application/json')
Beispiel #2
0
    def on_remove_ar(self, request):
        res = {'error': 1}
        if request.method == 'POST':
            user_id = request.form.get('user_id')
            session_id = request.form.get('session_id')
            ar_id = request.form.get('ar_id')

            if ar_id is not None:
                if cvtools.user_logged_in(user_id, session_id):
                    image, count = cvtools.get_list(None, None, ar_id)
                    if len(image) > 0:
                        image = image[0]
                        if image['user_id'] != int(user_id):
                            res['error_msg'] = 'this ar is not yours'
                        else:
                            if image['target_type'] == conf.TAR_TYPE_IMAGE:
                                tar = conf.UPLOAD_DIR_TAR_IMG + '/' + image['target']
                            elif image['target_type'] == conf.TAR_TYPE_VIDEO:
                                tar = conf.UPLOAD_DIR_TAR_VIDEO + '/' + image['target']
                            else:
                                tar = None

                            if cvtools.remove_image(ar_id, conf.UPLOAD_DIR_SRC + '/' + image['src_name'], tar):
                                res['error'] = 0
                            else:
                                res['error_msg'] = 'delete unsuccessful'
                    else:
                        res['error_msg'] = 'image with id (' + ar_id + ') not found'
                else:
                    res['error'] = 2
                    res['error_msg'] = 'please login'
            else:
                res['error_msg'] = 'ar_id missing'
        response_str = json.dumps([res])
        return Response(response_str, mimetype='application/json')
Beispiel #3
0
    def on_admin_remove_multiple_image(self, request):
        if request.cookies.get('cookie_name') is None:
            return redirect('/')
        checked = request.args.getlist('checked[]')
        error = 0
        for img_id in checked:
            if img_id is not None:
                image, count = cvtools.get_list(None, None, img_id)
                if len(image) > 0:
                    image = image[0]
                    if image['target_type'] == conf.TAR_TYPE_IMAGE:
                        tar = conf.UPLOAD_DIR_TAR_IMG + '/' + image['target']
                    elif image['target_type'] == conf.TAR_TYPE_VIDEO:
                        tar = conf.UPLOAD_DIR_TAR_VIDEO + '/' + image['target']
                    else:
                        tar = None

                    if not cvtools.remove_image(img_id, conf.UPLOAD_DIR_SRC + '/' + image['src_name'], tar):
                        error = 1
                    
        return redirect(request.referrer)
Beispiel #4
0
    def on_admin_multiple_image_action(self, request):
        if request.cookies.get('cookie_name') is None:
            return redirect('/')
        
        action = request.args.get('action')
        checked = request.args.getlist('checked[]')
        
        error = 0
        if action == "pin":
            for img_id in checked:
                if img_id is not None:
                    if cvtools.is_pinned(img_id) is False:
                        cvtools.toggle_pinned(img_id)
                    else:
                        continue
        elif action == "unpin":
            for img_id in checked:
                if img_id is not None:
                    if cvtools.is_pinned(img_id) is True:
                        cvtools.toggle_pinned(img_id)
                    else:
                        continue
        elif action == "remove":
            for img_id in checked:
                if img_id is not None:
                    image, count = cvtools.get_list(None, None, img_id)
                    if len(image) > 0:
                        image = image[0]
                        if image['target_type'] == conf.TAR_TYPE_IMAGE:
                            tar = conf.UPLOAD_DIR_TAR_IMG + '/' + image['target']
                        elif image['target_type'] == conf.TAR_TYPE_VIDEO:
                            tar = conf.UPLOAD_DIR_TAR_VIDEO + '/' + image['target']
                        else:
                            tar = None

                        if not cvtools.remove_image(img_id, conf.UPLOAD_DIR_SRC + '/' + image['src_name'], tar):
                            error = 1
        
        return redirect(request.referrer)
Beispiel #5
0
    def on_admin_list(self, request, page):
        if request.cookies.get('cookie_name') is None or request.cookies.get('cookie_name') == '':
            return redirect('/')
        
        sid = request.cookies.get('cookie_name')
        keyword = request.args.get('keyword')
        
        # My changes start here
        user = request.cookies.get('cookie_user')
        if keyword is None:
            keyword = ""
        
        option = request.args.get('option')
        action = request.args.get('action')
        search_page = request.args.get('page')
        
        types = ['', '', '', '', '', '']
        
        if request.args.get('type-image') is not None:
            types[1] = int(request.args.get('type-image'))
        else:
            types[1] = conf.TAR_TYPE_NOTYPE
            
        if request.args.get('type-video') is not None:
            types[2] = int(request.args.get('type-video'))
        else:
            types[2] = conf.TAR_TYPE_NOTYPE
        
        if request.args.get('type-url') is not None:
            types[3] = int(request.args.get('type-url'))
        else:
            types[3] = conf.TAR_TYPE_NOTYPE
            
        if request.args.get('type-youtube') is not None:
            types[4] = int(request.args.get('type-youtube'))
        else:
            types[4] = conf.TAR_TYPE_NOTYPE
            
        if request.args.get('type-model') is not None:
            types[5] = int(request.args.get('type-model'))
        else:
            types[5] = conf.TAR_TYPE_NOTYPE

        if action == "Signout":
            response = redirect('/')
            response.set_cookie('cookie_name', '')
            return response
        
        search = request.args.get('search')
        # My changes end here
        
        if search is not None:
            if search_page is not None:
                page = int(search_page)
            else:
                page = cvtools.num(page)
        else:
            page = cvtools.num(page)
        
        per_page = 10
        
        # My changes start here
        if option == "type":
            images, count = cvtools.get_list_type(keyword, None, None, 'i.created_at DESC, i.pinned DESC', page, per_page, types)
        elif option == "user":
            images, count = cvtools.get_list_user(keyword, None, None, 'i.created_at DESC, i.pinned DESC', page, per_page, types)
        else:
            images, count = cvtools.get_list(keyword, None, None, 'i.created_at DESC, i.pinned DESC', page, per_page, types)
        # My changes end here
            
        if not images and page != 1:
            return self.error_404()
        pagination = pg.Pagination(page, per_page, count)

        return self.render_template('admin_list.html', page="admin_list", error=None, images=images, pagination=pagination,
                                    keyword=keyword, count=count, option=option, selected='true', types=types, search=search, sid=sid, user=user, abs_url=cvtools.get_abs_url, url_for=url_for, conf=conf)