Skip to content

Commit

Permalink
Clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdiirh committed May 11, 2022
1 parent f98a27c commit f404561
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@require_POST
@csrf_exempt
@login_required()
@login_required(api_endpoint=True)
def exec_command(request):

data = get_request_data(request)
Expand Down
9 changes: 6 additions & 3 deletions control/static/control/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $('button[name="command"]').on("click", (e) => {
send_command($(e.target).val());
})

window.alertError = function (text, timeout=1000) {
window.alertError = function (text, timeout = 2000) {
let alert = $(".alert-error");
alert.html(text)
alert.fadeIn(500)
Expand All @@ -21,7 +21,7 @@ window.alertError = function (text, timeout=1000) {
}, timeout);
}

window.alertSuccess = function (text, timeout=1000) {
window.alertSuccess = function (text, timeout = 2000) {
let alert = $(".alert-success");
alert.html(text)
alert.fadeIn(500)
Expand Down Expand Up @@ -53,7 +53,10 @@ function send_command(code) {
$("#output").text(response['data']['output']);
}
} else {
alertError('An error occurred')
if (response['meta']['message'])
alertError(response['meta']['message'])
else
alertError(`An error occurred [ ${response['meta']['code']} ]`)
}

$("button[name='command']").prop("disabled", false)
Expand Down
26 changes: 6 additions & 20 deletions control/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from django.shortcuts import render, reverse, redirect
from django.http.response import JsonResponse
import requests

from django.contrib.auth import logout as logout_user
from django.http.response import JsonResponse
from django.shortcuts import render, reverse, redirect
from django.views.decorators.http import require_POST

from config.models import Button, Command

from config.models import Button
from utils.auth.http.decorators import login_required

import json
import requests


@login_required(redirect_login=True, next_redirect='control')
def control(request):

custom_buttons = Button.objects.filter(active=True, command__active=True).all()
context = {
'response': '',
Expand All @@ -26,7 +23,6 @@ def control(request):

@require_POST
def ajax_exec(request):

api_key = None
if request.user.is_staff:
api_key = request.user.profile.api_key
Expand All @@ -42,17 +38,7 @@ def ajax_exec(request):
data={'command': str(command)}
)

try:
response = response.json()
except json.JSONDecodeError:
response = {
'data': None,
'meta': {
'status': 'fail',
'code': response.status_code,
}}

return JsonResponse(response, status=200)
return JsonResponse(response.json(), status=200)


def logout(request):
Expand Down
14 changes: 12 additions & 2 deletions utils/auth/http/decorators/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@

from functools import wraps

from utils.api.tools import user_security_check
from utils.api.tools import user_security_check, api_response
from utils.core import http_status as sc
from utils.core.exceptions import AuthenticationError


def login_required(redirect_login=False, next_redirect=None):
def login_required(redirect_login=False, next_redirect=None, api_endpoint=False):

def decorator(func):
@wraps(func)
def inner(request, *args, **kwargs):
try:
user_security_check(request)
except AuthenticationError:

if api_endpoint:
return api_response(
request,
status_code=sc.HTTP_403_FORBIDDEN,
message='authentication failed',
commit=False
)

if not redirect_login:
raise PermissionDenied

Expand Down

0 comments on commit f404561

Please sign in to comment.