コード例 #1
0
ファイル: news_category.py プロジェクト: phuclc/dienthoai
def delete(news_category_id):
    try:
        b = News_Category.objects.get(id=news_category_id)
        b.delete()
        return render.template('admin/news_category/detail.html', news_category=b)
    except Exception:
        abort(404)
コード例 #2
0
ファイル: category.py プロジェクト: phuclc/dienthoai
def update(category_id):
    try:
        data = request.form.to_dict()
        temp_data = data
        ancestorpath = []
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip()) > 0)
        category = Category.objects.get(id=str(category_id))  # cha
        if category is None:
            return abort(400)
        update_map = dict([("set__" + key, value) for key, value in data.items()])
        if update_map.has_key("set__parent"):
            if update_map["set__parent"] == category_id:
                return abort(400)
            parent = Category.objects(id=str(update_map["set__parent"])).first()  # con
            if parent is None:
                return abort(400)
            ancestorpath = parent.ancestors
            ancestorpath.append(parent)
            """
            Check category in child's ancestors when update a category with its parent is its descendant
            If category in child's ancestors return True
            else return False
            """

            def ancestors(parent, child):
                if parent.id == child.id:
                    return True
                if child.parent is None:
                    return False
                return ancestors(parent, child.parent)

            if ancestors(category, parent):
                return abort(400)

        if temp_data.has_key("parent"):
            if len(str(temp_data["parent"]).strip()) == 0:
                update_map["set__parent"] = None
        update_map["set__ancestors"] = ancestorpath
        category.update(**update_map)
        category.reload

        # get all child
        children = Category.objects(ancestors__contains=category.id)

        def getpath(path, child):
            if child.parent is None:
                return path
            path.insert(0, child.parent)
            return getpath(path, child.parent)

        # update ancestors in child
        for child in children:
            child_path = getpath([], child)
            child.ancestors = child_path
            child.save()
        rebuilt_category_product()
        return render.template("admin/category/detail.html", category=category.reload()), 200
    except Exception as e:
        print e
        abort(400)
コード例 #3
0
ファイル: user.py プロジェクト: phuclc/dienthoai
def delete(user_id):
    try:
        u = User.objects.get(uid=user_id)
        u.delete()
        return render.template('admin/user/detail.html', user=u)
    except DoesNotExist:
        abort(404)
コード例 #4
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def create():
    try:
        data = request.form.to_dict()
        brand = Brand.objects.create(**data)
        return render.template("admin/brand/detail.html", brand=brand), 201
    except ValidationError as ve:
        abort(400, "Validation Error")
コード例 #5
0
ファイル: sticky.py プロジェクト: phuclc/dienthoai
def delete(sticky_id):
    try:
        b = Sticky.objects.get(id=sticky_id)
        b.delete()
        return render.template('admin/sticky/detail.html', sticky=b)
    except Exception:
        abort(404)
コード例 #6
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def delete(question_category_id):
    try:
        b = Question_Category.objects.get(id=question_category_id)
        b.delete()
        return render.template('admin/question_category/detail.html', question_category=b)
    except Exception:
        abort(404)
コード例 #7
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def delete(brand_id):
    try:
        b = Brand.objects.get(uid=brand_id)
        b.delete()
        return render.template("admin/brand/detail.html", brand=b)
    except DoesNotExist:
        abort(404)
コード例 #8
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def create():
    try:
        data = request.form.to_dict()
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip())>0)
        question_category = Question_Category.objects.create(**data)
        return render.template('admin/question_category/detail.html', question_category=question_category), 201
    except ValidationError as ve:
        abort(400, "Validation Error")
コード例 #9
0
ファイル: order.py プロジェクト: phuclc/dienthoai
def index():
    data = request.args.to_dict()
    filtering_state = data.get('state__icontains', '')

    criteria = {'state__icontains': str(filtering_state)}
     
    orders = Order.objects(**criteria)
    return render.template('admin/order/index.html', orders=orders)
コード例 #10
0
ファイル: category.py プロジェクト: phuclc/dienthoai
def detail(category_id):
    try:
        category = Category.objects(id=str(category_id)).first()
        if category is None:
            return abort(404)
        return render.template("admin/category/detail.html", category=category)
    except Exception, e:
        abort(404)
コード例 #11
0
ファイル: question.py プロジェクト: phuclc/dienthoai
def create():
    try:
        data = request.form.to_dict()
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip())>0)
        data['user'] = json.loads(session.get('user')).get('_id').get('$oid')
        question = Question.objects.create(**data)
        return render.template('admin/question/detail.html', question=question), 201
    except ValidationError as ve:
        abort(400, "Validation Error")
コード例 #12
0
ファイル: question.py プロジェクト: phuclc/dienthoai
def edit(question_id):
    try:
        b = Question.objects.get(id=question_id)
        categories = Question_Category.objects()
        username=json.loads(session.get('user')).get('username')
        if str(b.user.id) != str(json.loads(session.get('user')).get('_id').get('$oid')) or json.loads(session.get('user')).get('permission') < 3:
            return abort(403)
        return render.template('admin/question/edit.html', question=b,categories=categories,username=username)
    except DoesNotExist:
        abort(404, "404 does not exist")
コード例 #13
0
ファイル: user.py プロジェクト: phuclc/dienthoai
def create():
    try:
        data = request.form.to_dict()
        data['password'] = generate_encrypt_password(data.get('password',''))
        user = User.objects.create(**data)
        return render.template('admin/user/detail.html', user=user), 201
    except NotUniqueError as e:
        abort(400, "Duplicated, username is existed") # Duplicate
    except ValidationError as ve:
        abort(400, "Validation Error")
コード例 #14
0
ファイル: news.py プロジェクト: phuclc/dienthoai
def edit(news_id):
    try:
        b = News.objects.get(id=news_id)
        categories = News_Category.objects()
        username=json.loads(session.get('user')).get('username')
        if str(b.author.id) != str(json.loads(session.get('user')).get('_id').get('$oid')) or json.loads(session.get('user')).get('permission') < 3:
            return abort(403)
        return render.template('admin/news/edit.html', news=b,categories=categories,username=username)
    except DoesNotExist:
        abort(404, "404 does not exist")
コード例 #15
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def update(question_category_id):
    try:
        data = request.form.to_dict()
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip())>0)
        update_map = dict([('set__' + key, value) for key, value in data.items()])
        b = Question_Category.objects.get(id=question_category_id)
        b.update(**update_map)
        b.reload()
        return render.template('admin/question_category/detail.html', question_category=b)
    except Exception:
        abort(404)
コード例 #16
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def update(brand_id):
    try:
        data = request.form.to_dict()
        update_map = dict([("set__" + key, value) for key, value in data.items()])
        b = Brand.objects.get(uid=brand_id)
        b.update(**update_map)
        b.reload()
        return render.template("admin/brand/detail.html", brand=b)
    except DoesNotExist:
        abort(404)
    except ValidationError as ve:
        abort(400, "Validation error")
コード例 #17
0
ファイル: user.py プロジェクト: phuclc/dienthoai
def update(user_id):
    try:
        data = request.form.to_dict()
        # Hash the password if exists
        if 'password' in data:
            data['password'] = generate_encrypt_password(data.get('password', ''))
        update_map = dict([('set__' + key, value) for key, value in data.items()])
        u = User.objects.get(uid=user_id)
        u.update(**update_map)
        u.reload()
        return render.template('admin/user/detail.html', user=u)
    except DoesNotExist:
        abort(404)
    except NotUniqueError:
        abort(400, "Duplicated, username is existed")
    except ValidationError as ve:
        abort(400, "Validation error")
コード例 #18
0
ファイル: question.py プロジェクト: phuclc/dienthoai
def update(question_id):
    try:
        data = request.form.to_dict()
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip())>0)
        if data['user']:
            del data['user']
        update_map = dict([('set__' + key, value) for key, value in data.items()])
        b = Question.objects.get(id=question_id)
        b.update(**update_map)
        b.reload()
        if data.get('show_in_index') is None:
            b.show_in_index = False
            b.save()
            b.reload()
        return render.template('admin/question/detail.html', question=b)
    except Exception as e:
        abort(404)
コード例 #19
0
ファイル: home.py プロジェクト: phuclc/dienthoai
def do_login():
    data = request.form.to_dict()
    username = data.get('username', None)
    password = data.get('password', None)
    if identity.authenticate(username, password):
        resp = make_response(redirect(request.args.get('next', '/')))
        if json.loads(session['user']).get('permission', 1) > 2:
            userId = json.loads(session.get('user')).get('_id').get('$oid')
            sid = str(uuid.uuid4())
            user = User.objects(id=userId).get()
            user.sid = sid
            user.save()
            user.reload()
            session['user'] = user.to_json()
            resp.set_cookie('userId', userId)
            resp.set_cookie('sid', sid)
        return resp
    else:
        return render.template('home/login.html')
コード例 #20
0
ファイル: category.py プロジェクト: phuclc/dienthoai
def delete(category_id):
    try:
        category = Category.objects.get(id=category_id)
        children = Category.objects(ancestors__contains=category.id)
        num = category.delete()

        def getpath(path, child):
            if child.parent is None:
                return path
            path.insert(0, child.parent)
            return getpath(path, child.parent)

        # update ancestors in child
        for child in children:
            child_path = getpath([], child)
            child.ancestors = child_path
            child.save()
        return render.template("admin/category/detail.html", category=category)
    except Exception:
        abort(404)
コード例 #21
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def index():
    # Slice of to pagination

    # List of filter by get args:
    # Example: /admin/brand/?page=1&name_icontains=apple
    data = request.args.to_dict()

    # Type of filter
    engine_filter = {"name__icontains": str}

    # Prepare filter
    criteria = {}
    for k in data:
        if k in engine_filter:
            criteria[k] = engine_filter[k](data[k])

    pagination = Paginate("admin.brand.index", count=len(Brand.objects(**criteria)), per_page=10)
    page = pagination.get_page()
    brands = Brand.objects(**criteria)[(page - 1) * 10 : page * 10]
    return render.template("admin/brand/index.html", brands=brands, pagination=pagination)
コード例 #22
0
ファイル: user.py プロジェクト: phuclc/dienthoai
def index():
    # Slice of to pagination

    # List of filter by get args:
    # Example: /admin/user/?page=1&username_icontains=duythinht&permission=3
    data = request.args.to_dict()

    # Type of filter
    engine_filter = {'username__icontains': str, 'email__icontains': str, 'permission': int}

    # Prepare filter
    criteria = {}
    for k in data:
        if k in engine_filter:
            criteria[k] = engine_filter[k](data[k])

    pagination = Paginate('admin.user.index', count=len(User.objects(**criteria)), per_page=10)
    page = pagination.get_page()
    users = User.objects(**criteria)[(page-1) * 10:page * 10]
    return render.template('admin/user/index.html', users=users, pagination=pagination)
コード例 #23
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def index():
    # Slice of to pagination

    # List of filter by get args:
    # Example: /admin/question_category/?page=1&name_icontains=apple
    data = request.args.to_dict()

    # Type of filter
    engine_filter = {'name__icontains': str}

    # Prepare filter
    criteria = {}
    for k in data:
        if k in engine_filter:
            criteria[k] = engine_filter[k](data[k])

    pagination = Paginate('admin.question_category.index', count=len(Question_Category.objects(**criteria)), per_page=10)
    page = pagination.get_page()
    question_categorys = Question_Category.objects(**criteria)[(page-1) * 10:page * 10]
    return render.template('admin/question_category/index.html', question_categorys=question_categorys, pagination=pagination)
コード例 #24
0
ファイル: category.py プロジェクト: phuclc/dienthoai
def create():
    try:
        data = request.form.to_dict()
        data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip()) > 0)
        ancestorpath = []
        if "parent" in data:
            if data["parent"] == "":
                del data["parent"]
            parent = Category.objects.get(id=data["parent"])
            ancestorpath = parent.ancestors
            ancestorpath.append(parent)
        data["ancestors"] = ancestorpath
        category = Category.objects.create(**data)
        return render.template("admin/category/detail.html", category=category), 201
    except NotUniqueError as e:
        abort(400, "Duplicated, category is existed")  # Duplicate
    except ValidationError as ve:
        abort(400, "Validation Error")
    except ValueError:
        abort(400, "ValueError Error")
    except DoesNotExist as dne:
        abort(400, "DoesNotExist Error")
コード例 #25
0
ファイル: category.py プロジェクト: phuclc/dienthoai
def edit(category_id):
    try:
        category = Category.objects.get(id=category_id)
        categories = Category.objects()

        def is_can_be_parent(can_be_parent, can_be_child):
            def get_ancestors(list, child):
                if child is None:
                    return list
                if child.parent is not None:
                    list.append(child.parent.id)
                return get_ancestors(list, child.parent)

            """
            to check can_be_parent is child of can_be_child much get all child of can_be_child
            to get all child of can_be_child much check all category with each category as c has ancestors, 
            which has can_be_child => c is child of can_be_child
            if can_be_parent in list child of can_be_child return false
            """
            list_child_of_child = []
            for c in categories:
                list = []
                list = get_ancestors(list, c)
                if can_be_child.id in list:
                    list_child_of_child.append(c.id)
            if can_be_parent.id in list_child_of_child:
                return False
            if can_be_child.parent is not None:
                if can_be_child.parent.id == can_be_parent.id:
                    return True
            if can_be_child.id == can_be_parent.id:
                return False
            return True

        categories = filter(lambda can_be_parent: is_can_be_parent(can_be_parent, category), categories)
        return render.template("admin/category/edit.html", category=category, categories=categories)
    except Exception:
        abort(404, "404 does not exist")
コード例 #26
0
ファイル: sticky.py プロジェクト: phuclc/dienthoai
def edit(sticky_id):
    try:
        b = Sticky.objects.get(id=sticky_id)
        return render.template('admin/sticky/edit.html', sticky=b)
    except DoesNotExist:
        abort(404, "404 does not exist")
コード例 #27
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def edit(brand_id):
    try:
        b = Brand.objects.get(uid=brand_id)
        return render.template("admin/brand/edit.html", brand=b)
    except DoesNotExist:
        abort(404, "404 does not exist")
コード例 #28
0
ファイル: brand.py プロジェクト: phuclc/dienthoai
def add():
    return render.template("admin/brand/create.html")
コード例 #29
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def edit(question_category_id):
    try:
        b = Question_Category.objects.get(id=question_category_id)
        return render.template('admin/question_category/edit.html', question_category=b)
    except DoesNotExist:
        abort(404, "404 does not exist")
コード例 #30
0
ファイル: question_category.py プロジェクト: phuclc/dienthoai
def add():
    return render.template('admin/question_category/create.html')