def get_item(request, post_id):
    try:
        item = Post.objects.get(pk=post_id)
    except Post.DoesNotExist:
        return {'code': 'post/not_found', 'values': [post_id]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_item(request, tag_id):
    try:
        item = Tag.objects.get(pk=tag_id)
    except Tag.DoesNotExist:
        return {"code": "tag/not_found", "values": [tag_id]}, 404, False

    return {"code": "ok", "data": helpers.objects_to_json(request, [item])}, 200, item
def get_item(request, file_id):
    try:
        item = File.objects.get(pk=file_id)
    except File.DoesNotExist:
        return {'code': 'file/not_found', 'values': [file_id]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def update(request, user_app_id):
    """Update record"""

    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = get_application_model().objects.get(pk=user_app_id)
    except get_application_model().DoesNotExist:
        return {'code': 'user_app/not_found', 'values': [user_app_id]}, 404, False

    data['client_type'] = AbstractApplication.CLIENT_CONFIDENTIAL
    data['authorization_grant_type'] = AbstractApplication.GRANT_PASSWORD
    data['skip_authorization'] = True
    data['client_id'] = generate_client_id()
    data['client_secret'] = generate_client_secret()

    helpers.json_to_objects(item, data)
    item.user = user
    item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_search(request, search_text):
    if search_text == "all":
        return get_list(request)
    else:
        items = Tag.objects.filter(helpers.get_searching_all_fields_qs(Tag, search_text)).order_by("created").all()

        return {"code": "ok", "data": helpers.objects_to_json(request, items)}, 200, items
def get_item_by_src(request, public_link_src):
    try:
        item = PublicLink.objects.get(src=public_link_src)
    except PublicLink.DoesNotExist:
        return {'code': 'public_link/not_found', 'values': [public_link_src]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_item_by_name(request, properties_name):
    try:
        item = Properties.objects.get(name=properties_name)
    except Properties.DoesNotExist:
        return {'code': 'properties/not_found', 'values': [properties_name]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_item_by_name(request, meta_tag_name):
    try:
        item = MetaTag.objects.get(name=meta_tag_name)
    except MetaTag.DoesNotExist:
        return {'code': 'meta_tag/not_found', 'values': [meta_tag_name]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_item_by_url(request, html_cache_url):
    try:
        item = HtmlCache.objects.get(url=html_cache_url)
    except HtmlCache.DoesNotExist:
        return {'code': 'html_cache/not_found', 'values': [html_cache_url]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def create(request):
    """Create record"""

    data = request.POST

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    if request.FILES and request.FILES.get('file'):
        if user.is_superuser:
            url = helpers.save_file(False,
                                    request.FILES.get('file'))
        else:
            url = helpers.save_file(str(user.id),
                                    request.FILES.get('file'))
    else:
        url = ''

    item, created = File.objects.get_or_create(src=url)
    if created:
        helpers.json_to_objects(item, data)
        item.created_user = user
        item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_item_by_client_id(request, user_app_client_id):
    from oauth2_provider.models import get_application_model

    try:
        item = get_application_model().objects.get(client_id=user_app_client_id)
    except get_application_model().DoesNotExist:
        return {'code': 'user_app/not_found', 'values': [user_app_client_id]}, 404, False

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_list(request):
    from oauth2_provider.models import get_application_model

    try:
        items = get_application_model().objects.all()
    except:
        items = []

    return {'code': 'ok', 'data': helpers.objects_to_json(request, items)}, 200, items
def get_search(request, search_text):
    if search_text == 'all':
        return get_list(request)
    else:
        items = PublicLink.objects.filter(
            helpers.get_searching_all_fields_qs(PublicLink, search_text)
        ).order_by('-created').all()

        return {'code': 'ok', 'data': helpers.objects_to_json(request, items)}, 200, items
def create(request):
    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    item, created = Post.objects.get_or_create(name=data['name'], type=1, created_user=user)
    reload_source = []
    if created:
        reload_source = update_from_json_data(request, item, data, user)

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item]), 'reload_source': reload_source}, 200, item
def create(request):
    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    item, created = PublicLink.objects.get_or_create(src=data['src'])

    if created:
        helpers.json_to_objects(item, data)
        item.created_user = user
        item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def update(request, file_id):
    """Update record"""

    data = request.DATA

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = File.objects.get(pk=file_id)
    except File.DoesNotExist:
        return {'code': 'file/not_found', 'values': [file_id]}, 404, False

    helpers.json_to_objects(item, data)
    item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def create(request):
    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    item, created = Properties.objects.get_or_create(name=data['name'])

    if created:
        helpers.json_to_objects(item, data)
        item.created_user = user
        item.only_update = 0
        item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def update(request, tag_id):
    """Update record"""

    data = request.DATA

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = Tag.objects.get(pk=tag_id)
    except Tag.DoesNotExist:
        return {"code": "tag/not_found", "values": [tag_id]}, 404, False

    helpers.json_to_objects(item, data)
    item.save()

    return {"code": "ok", "data": helpers.objects_to_json(request, [item])}, 200, item
def create(request):
    """Create record"""

    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    item, created = Tag.objects.get_or_create(text=data["text"])
    if created:
        helpers.json_to_objects(item, data)
        item.created_user = user
        item.save()

    return {"code": "ok", "data": helpers.objects_to_json(request, [item])}, 200, item
def update(request, post_id):
    """Update record"""

    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = Post.objects.get(pk=post_id)
    except Post.DoesNotExist:
        return {'code': 'post/not_found', 'values': [post_id]}, 404, False

    reload_source = update_from_json_data(request, item, data, user)

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item]), 'reload_source': reload_source}, 200, item
def update(request, public_link_id):
    """Update record"""

    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = PublicLink.objects.get(pk=public_link_id)
    except PublicLink.DoesNotExist:
        return {'code': 'public_link/not_found', 'values': [public_link_id]}, 404, False

    helpers.json_to_objects(item, data)
    item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def update(request, html_cache_id):
    """Update record"""

    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    try:
        item = HtmlCache.objects.get(pk=html_cache_id)
    except HtmlCache.DoesNotExist:
        return {'code': 'html_cache/not_found', 'values': [html_cache_id]}, 404, False

    helpers.json_to_objects(item, data)
    item.save()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def create(request):
    data = request.DATA

    user = helpers.get_user(request)

    data = helpers.set_null_values_if_not_exist(data, get_fields())

    data['client_type'] = AbstractApplication.CLIENT_CONFIDENTIAL
    data['authorization_grant_type'] = AbstractApplication.GRANT_PASSWORD
    data['skip_authorization'] = True
    data['client_id'] = generate_client_id()
    data['client_secret'] = generate_client_secret()

    item, created = get_application_model().objects.get_or_create(client_id=data['client_id'],
                                                                  user=user,
                                                                  redirect_uris=data['redirect_uris'],
                                                                  client_type=data['client_type'],
                                                                  authorization_grant_type=data[
                                                                      'authorization_grant_type'],
                                                                  client_secret=data['client_secret'],
                                                                  name=data['name'],
                                                                  skip_authorization=data['skip_authorization'])

    return {'code': 'ok', 'data': helpers.objects_to_json(request, [item])}, 200, item
def get_list_by_tag(request, tag_text):
    items = Post.objects.filter(tags__text=tag_text).order_by('-created').all()
    return {'code': 'ok', 'data': helpers.objects_to_json(request, items)}, 200, items
def get_list(request):
    items = PublicLink.objects.all().order_by('position').all()
    return {'code': 'ok', 'data': helpers.objects_to_json(request, items)}, 200, items
def get_list(request):
    items = Tag.objects.all().order_by("created").all()
    return {"code": "ok", "data": helpers.objects_to_json(request, items)}, 200, items
def get_list(request):
    items = HtmlCache.objects.all().order_by('-created').all()

    return {'code': 'ok', 'data': helpers.objects_to_json(request, items)}, 200, items