Example #1
0
def index(request):
    # установка проекта(БД)
    setProject(request=request)

    # INDEX
    if request.method == "GET":
        json = []

        tags = Tag.objects.all().order_by("id")

        for tag in tags:
            json.append(tag.data())

        json = {"success": True, "message": "Loaded", "data": json}

        return HttpResponseJSON(json)

    # CREATE
    if request.method == "POST":
        post = simplejson.loads(request.POST["data"])

        name = post["name"]
        description = post["description"]

        tag = Tag.objects.create(name=name, description=description)

        json = {
            "success": True,
            "message": "Создан tag %s" % tag.id,
            "data": {"id": tag.id, "name": tag.name, "description": tag.description},
        }

        return HttpResponseJSON(json)
Example #2
0
def update(request, tag):
    # установка проекта(БД)
    setProject(request=request)

    tag = int(tag)

    # find Tag
    tag = Tag.objects.get(id=tag)

    # UPDATE
    if request.method == "PUT":
        # print request.raw_post_data

        raw_data = request.raw_post_data

        if "&" in raw_data:
            params = urllib.unquote(raw_data).split("&")
            for param in params:
                if param[0:5] == "data=":
                    post = simplejson.loads(param.split("=")[1])
        else:
            post = simplejson.loads((urllib.unquote(raw_data).split("=")[1]))

        # update tag
        if "name" in post:
            tag.name = post["name"]

        if "description" in post:
            tag.description = post["description"]

        # сохраняем Tag
        tag.save()

        json = {
            "success": True,
            "message": "Обновлён tag %s" % tag.id,
            "data": {"id": tag.id, "name": tag.name, "description": tag.description},
        }

    # DELETE
    if request.method == "DELETE":
        id = tag.id
        tag.delete()

        json = {"success": True, "message": "Удалён tag %s" % id}

    return HttpResponseJSON(json)
Example #3
0
def report(request):

    # установка проекта(БД)
    setProject(request=request)

    # INDEX
    if request.method == "GET":
        json = []

        tags = Tag.objects.all().order_by("id")

        for tag in tags:
            json.append(tag.data())

        json = {"success": True, "message": "Loaded", "data": json}

        return HttpResponseJSON(json)