A api update

pull/22/head
雷二猛 2019-12-19 23:49:40 +08:00
parent 5e34148217
commit e326b6ce34
18 changed files with 45 additions and 45 deletions

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from django.contrib.auth.hashers import make_password, check_password from django.contrib.auth.hashers import make_password, check_password
@ -13,7 +13,7 @@ class User(models.Model, ModelMixin):
token_expired = models.IntegerField(null=True) token_expired = models.IntegerField(null=True)
last_login = models.CharField(max_length=20) last_login = models.CharField(max_length=20)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True) created_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True)
deleted_at = models.CharField(max_length=20, null=True) deleted_at = models.CharField(max_length=20, null=True)
deleted_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True) deleted_by = models.ForeignKey('User', models.PROTECT, related_name='+', null=True)

View File

@ -1,6 +1,6 @@
from django.core.cache import cache from django.core.cache import cache
from django.views.generic import View from django.views.generic import View
from libs import JsonParser, Argument, human_time, json_response from libs import JsonParser, Argument, human_datetime, json_response
from .models import User from .models import User
import time import time
import uuid import uuid
@ -43,7 +43,7 @@ class UserView(View):
).parse(request.GET) ).parse(request.GET)
if error is None: if error is None:
User.objects.filter(pk=form.id).update( User.objects.filter(pk=form.id).update(
deleted_at=human_time(), deleted_at=human_datetime(),
deleted_by=request.user deleted_by=request.user
) )
return json_response(error=error) return json_response(error=error)
@ -64,7 +64,7 @@ def login(request):
token_isvalid = user.access_token and len(user.access_token) == 32 and user.token_expired >= time.time() token_isvalid = user.access_token and len(user.access_token) == 32 and user.token_expired >= time.time()
user.access_token = user.access_token if token_isvalid else uuid.uuid4().hex user.access_token = user.access_token if token_isvalid else uuid.uuid4().hex
user.token_expired = time.time() + 8 * 60 * 60 user.token_expired = time.time() + 8 * 60 * 60
user.last_login = human_time() user.last_login = human_datetime()
user.save() user.save()
return json_response({'access_token': user.access_token, 'nickname': user.nickname}) return json_response({'access_token': user.access_token, 'nickname': user.nickname})

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
import json import json
@ -21,7 +21,7 @@ class Alarm(models.Model, ModelMixin):
notify_grp = models.CharField(max_length=255) notify_grp = models.CharField(max_length=255)
status = models.CharField(max_length=2, choices=STATUS) status = models.CharField(max_length=2, choices=STATUS)
duration = models.CharField(max_length=50) duration = models.CharField(max_length=50)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
def to_dict(self, *args, **kwargs): def to_dict(self, *args, **kwargs):
tmp = super().to_dict(*args, **kwargs) tmp = super().to_dict(*args, **kwargs)
@ -42,7 +42,7 @@ class Group(models.Model, ModelMixin):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
desc = models.CharField(max_length=255, null=True) desc = models.CharField(max_length=255, null=True)
contacts = models.TextField(null=True) contacts = models.TextField(null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
def to_dict(self, *args, **kwargs): def to_dict(self, *args, **kwargs):
@ -65,7 +65,7 @@ class Contact(models.Model, ModelMixin):
ding = models.CharField(max_length=255, null=True) ding = models.CharField(max_length=255, null=True)
wx_token = models.CharField(max_length=255, null=True) wx_token = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
def __repr__(self): def __repr__(self):

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
from apps.config.models import Environment from apps.config.models import Environment
import json import json
@ -16,7 +16,7 @@ class App(models.Model, ModelMixin):
extend = models.CharField(max_length=2, choices=EXTENDS) extend = models.CharField(max_length=2, choices=EXTENDS)
is_audit = models.BooleanField() is_audit = models.BooleanField()
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
updated_at = models.CharField(max_length=20, null=True) updated_at = models.CharField(max_length=20, null=True)
updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
@ -7,7 +7,7 @@ class Environment(models.Model, ModelMixin):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
key = models.CharField(max_length=50) key = models.CharField(max_length=50)
desc = models.CharField(max_length=255, null=True) desc = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, on_delete=models.PROTECT) created_by = models.ForeignKey(User, on_delete=models.PROTECT)
def __repr__(self): def __repr__(self):
@ -22,7 +22,7 @@ class Service(models.Model, ModelMixin):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
key = models.CharField(max_length=50) key = models.CharField(max_length=50)
desc = models.CharField(max_length=255, null=True) desc = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, on_delete=models.PROTECT) created_by = models.ForeignKey(User, on_delete=models.PROTECT)
def __repr__(self): def __repr__(self):

View File

@ -99,7 +99,7 @@ class ConfigView(View):
).parse(request.body) ).parse(request.body)
if error is None: if error is None:
form.value = form.value.strip() form.value = form.value.strip()
form.updated_at = human_time() form.updated_at = human_datetime()
form.updated_by = request.user form.updated_by = request.user
envs = form.pop('envs') envs = form.pop('envs')
for env_id in envs: for env_id in envs:
@ -122,7 +122,7 @@ class ConfigView(View):
old_value = config.value old_value = config.value
config.value = form.value config.value = form.value
config.desc = form.desc config.desc = form.desc
config.updated_at = human_time() config.updated_at = human_datetime()
config.updated_by = request.user config.updated_by = request.user
config.save() config.save()
ConfigHistory.objects.create( ConfigHistory.objects.create(
@ -145,7 +145,7 @@ class ConfigView(View):
action='3', action='3',
old_value=config.value, old_value=config.value,
value='', value='',
updated_at=human_time(), updated_at=human_datetime(),
updated_by=request.user, updated_by=request.user,
**config.to_dict(excludes=('id', 'value', 'updated_at', 'updated_by_id')) **config.to_dict(excludes=('id', 'value', 'updated_at', 'updated_by_id'))
) )
@ -226,7 +226,7 @@ def _parse(request, query, data):
if item.value != value: if item.value != value:
old_value = item.value old_value = item.value
item.value = value item.value = value
item.updated_at = human_time() item.updated_at = human_datetime()
item.updated_by = request.user item.updated_by = request.user
item.save() item.save()
ConfigHistory.objects.create( ConfigHistory.objects.create(
@ -238,7 +238,7 @@ def _parse(request, query, data):
action='3', action='3',
old_value=item.value, old_value=item.value,
value='', value='',
updated_at=human_time(), updated_at=human_datetime(),
updated_by=request.user, updated_by=request.user,
**item.to_dict(excludes=('id', 'value', 'updated_at', 'updated_by_id')) **item.to_dict(excludes=('id', 'value', 'updated_at', 'updated_by_id'))
) )
@ -247,7 +247,7 @@ def _parse(request, query, data):
query.key = key query.key = key
query.is_public = True query.is_public = True
query.value = _filter_value(value) query.value = _filter_value(value)
query.updated_at = human_time() query.updated_at = human_datetime()
query.updated_by = request.user query.updated_by = request.user
Config.objects.create(**query) Config.objects.create(**query)
ConfigHistory.objects.create(action='1', **query) ConfigHistory.objects.create(action='1', **query)

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
from apps.app.models import App from apps.app.models import App
@ -20,7 +20,7 @@ class DeployRequest(models.Model, ModelMixin):
status = models.CharField(max_length=2, choices=STATUS) status = models.CharField(max_length=2, choices=STATUS)
reason = models.CharField(max_length=255, null=True) reason = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
approve_at = models.CharField(max_length=20, null=True) approve_at = models.CharField(max_length=20, null=True)
approve_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) approve_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
@ -9,7 +9,7 @@ class ExecTemplate(models.Model, ModelMixin):
body = models.TextField() body = models.TextField()
desc = models.CharField(max_length=255, null=True) desc = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
updated_at = models.CharField(max_length=20, null=True) updated_at = models.CharField(max_length=20, null=True)
updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -1,5 +1,5 @@
from django.views.generic import View from django.views.generic import View
from libs import json_response, JsonParser, Argument, human_time from libs import json_response, JsonParser, Argument, human_datetime
from libs.channel import Channel from libs.channel import Channel
from apps.exec.models import ExecTemplate from apps.exec.models import ExecTemplate
from apps.host.models import Host from apps.host.models import Host
@ -21,7 +21,7 @@ class TemplateView(View):
).parse(request.body) ).parse(request.body)
if error is None: if error is None:
if form.id: if form.id:
form.updated_at = human_time() form.updated_at = human_datetime()
form.updated_by = request.user form.updated_by = request.user
ExecTemplate.objects.filter(pk=form.pop('id')).update(**form) ExecTemplate.objects.filter(pk=form.pop('id')).update(**form)
else: else:

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
from apps.setting.utils import AppSetting from apps.setting.utils import AppSetting
from libs.ssh import SSH from libs.ssh import SSH
@ -13,7 +13,7 @@ class Host(models.Model, ModelMixin):
username = models.CharField(max_length=50) username = models.CharField(max_length=50)
desc = models.CharField(max_length=255, null=True) desc = models.CharField(max_length=255, null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
deleted_at = models.CharField(max_length=20, null=True) deleted_at = models.CharField(max_length=20, null=True)
deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -5,7 +5,7 @@ from libs import json_response, JsonParser, Argument
from apps.setting.utils import AppSetting from apps.setting.utils import AppSetting
from apps.host.models import Host from apps.host.models import Host
from libs.ssh import SSH, AuthenticationException from libs.ssh import SSH, AuthenticationException
from libs import human_time from libs import human_datetime
class HostView(View): class HostView(View):
@ -42,7 +42,7 @@ class HostView(View):
).parse(request.GET) ).parse(request.GET)
if error is None: if error is None:
Host.objects.filter(pk=form.id).update( Host.objects.filter(pk=form.id).update(
deleted_at=human_time(), deleted_at=human_datetime(),
deleted_by=request.user, deleted_by=request.user,
) )
return json_response(error=error) return json_response(error=error)

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
import json import json
@ -32,7 +32,7 @@ class Detection(models.Model, ModelMixin):
latest_fault_time = models.IntegerField(null=True) latest_fault_time = models.IntegerField(null=True)
latest_notify_time = models.IntegerField(default=0) latest_notify_time = models.IntegerField(default=0)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
updated_at = models.CharField(max_length=20, null=True) updated_at = models.CharField(max_length=20, null=True)
updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -7,7 +7,7 @@ from apps.alarm.models import Alarm
from apps.monitor.executors import dispatch from apps.monitor.executors import dispatch
from apps.monitor.utils import seconds_to_human from apps.monitor.utils import seconds_to_human
from django.conf import settings from django.conf import settings
from libs import AttrDict, human_time from libs import AttrDict, human_datetime
import logging import logging
import json import json
import time import time
@ -36,14 +36,14 @@ class Scheduler:
if obj.latest_status == 0: if obj.latest_status == 0:
if old_status == 1: if old_status == 1:
self._record_alarm(obj, '2') self._record_alarm(obj, '2')
logger.info(f'{human_time()} recover job_id: {obj.id}') logger.info(f'{human_datetime()} recover job_id: {obj.id}')
else: else:
if obj.fault_times >= obj.threshold: if obj.fault_times >= obj.threshold:
if time.time() - obj.latest_notify_time >= obj.quiet * 60: if time.time() - obj.latest_notify_time >= obj.quiet * 60:
obj.latest_notify_time = int(time.time()) obj.latest_notify_time = int(time.time())
obj.save() obj.save()
self._record_alarm(obj, '1') self._record_alarm(obj, '1')
logger.info(f'{human_time()} notify job_id: {obj.id}') logger.info(f'{human_datetime()} notify job_id: {obj.id}')
def _handle_event(self, event): def _handle_event(self, event):
# TODO: notify to user # TODO: notify to user
@ -59,7 +59,7 @@ class Scheduler:
obj = Detection.objects.filter(pk=event.job_id).first() obj = Detection.objects.filter(pk=event.job_id).first()
old_status = obj.latest_status old_status = obj.latest_status
obj.latest_status = 0 if event.retval else 1 obj.latest_status = 0 if event.retval else 1
obj.latest_run_time = human_time(event.scheduled_run_time) obj.latest_run_time = human_datetime(event.scheduled_run_time)
if old_status in [0, None] and event.retval is False: if old_status in [0, None] and event.retval is False:
obj.latest_fault_time = int(time.time()) obj.latest_fault_time = int(time.time())
if obj.latest_status == 0: if obj.latest_status == 0:

View File

@ -1,5 +1,5 @@
from django.views.generic import View from django.views.generic import View
from libs import json_response, JsonParser, Argument, human_time from libs import json_response, JsonParser, Argument, human_datetime
from apps.monitor.models import Detection from apps.monitor.models import Detection
from django_redis import get_redis_connection from django_redis import get_redis_connection
from django.conf import settings from django.conf import settings
@ -30,7 +30,7 @@ class DetectionView(View):
form.notify_mode = json.dumps(form.notify_mode) form.notify_mode = json.dumps(form.notify_mode)
if form.id: if form.id:
Detection.objects.filter(pk=form.id).update( Detection.objects.filter(pk=form.id).update(
updated_at=human_time(), updated_at=human_datetime(),
updated_by=request.user, updated_by=request.user,
**form) **form)
task = Detection.objects.filter(pk=form.id).first() task = Detection.objects.filter(pk=form.id).first()

View File

@ -1,5 +1,5 @@
from django.db import models from django.db import models
from libs import ModelMixin, human_time from libs import ModelMixin, human_datetime
from apps.account.models import User from apps.account.models import User
import json import json
@ -28,7 +28,7 @@ class Task(models.Model, ModelMixin):
latest_run_time = models.CharField(max_length=20, null=True) latest_run_time = models.CharField(max_length=20, null=True)
latest_output = models.TextField(null=True) latest_output = models.TextField(null=True)
created_at = models.CharField(max_length=20, default=human_time) created_at = models.CharField(max_length=20, default=human_datetime)
created_by = models.ForeignKey(User, models.PROTECT, related_name='+') created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
updated_at = models.CharField(max_length=20, null=True) updated_at = models.CharField(max_length=20, null=True)
updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True) updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

View File

@ -7,7 +7,7 @@ from apps.schedule.models import Task
from apps.schedule.executors import dispatch from apps.schedule.executors import dispatch
from apps.alarm.utils import auto_clean_records from apps.alarm.utils import auto_clean_records
from django.conf import settings from django.conf import settings
from libs import AttrDict, human_time from libs import AttrDict, human_datetime
import logging import logging
import json import json
@ -47,7 +47,7 @@ class Scheduler:
score += 1 if item[1] else 0 score += 1 if item[1] else 0
Task.objects.filter(pk=event.job_id).update( Task.objects.filter(pk=event.job_id).update(
latest_status=2 if score == len(event.retval) else 1 if score else 0, latest_status=2 if score == len(event.retval) else 1 if score else 0,
latest_run_time=human_time(event.scheduled_run_time), latest_run_time=human_datetime(event.scheduled_run_time),
latest_output=json.dumps(event.retval) latest_output=json.dumps(event.retval)
) )

View File

@ -3,7 +3,7 @@ from django_redis import get_redis_connection
from apps.schedule.models import Task from apps.schedule.models import Task
from apps.host.models import Host from apps.host.models import Host
from django.conf import settings from django.conf import settings
from libs import json_response, JsonParser, Argument, human_time from libs import json_response, JsonParser, Argument, human_datetime
import json import json
@ -28,7 +28,7 @@ class Schedule(View):
form.targets = json.dumps(form.targets) form.targets = json.dumps(form.targets)
if form.id: if form.id:
Task.objects.filter(pk=form.id).update( Task.objects.filter(pk=form.id).update(
updated_at=human_time(), updated_at=human_datetime(),
updated_by=request.user, updated_by=request.user,
**form **form
) )

View File

@ -8,7 +8,7 @@ import json
# 转换时间格式到字符串 # 转换时间格式到字符串
def human_time(date=None): def human_datetime(date=None):
if date: if date:
assert isinstance(date, datetime) assert isinstance(date, datetime)
else: else: