mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-26 07:22:27 +00:00
add asset add
This commit is contained in:
61
apps/common/README.md
Normal file
61
apps/common/README.md
Normal file
@@ -0,0 +1,61 @@
|
||||
## Celery usage
|
||||
|
||||
|
||||
Jumpserver use celery to run task async. Using redis as the broker, so
|
||||
you should run a redis instance
|
||||
|
||||
#### Run redis
|
||||
|
||||
$ yum -y install redis
|
||||
|
||||
or
|
||||
|
||||
$ docker run -name jumpserver-redis -d -p 6379:6379 redis redis-server
|
||||
|
||||
|
||||
#### Write tasks in app_name/tasks.py
|
||||
|
||||
ops/tasks.py
|
||||
|
||||
```
|
||||
from __future__ import absolute_import
|
||||
|
||||
import time
|
||||
from celery import shared_task
|
||||
from common import celery_app
|
||||
|
||||
|
||||
@shared_task
|
||||
def longtime_add(x, y):
|
||||
print 'long time task begins'
|
||||
# sleep 5 seconds
|
||||
time.sleep(5)
|
||||
print 'long time task finished'
|
||||
return x + y
|
||||
|
||||
|
||||
@celery_app.task(name='hello-world')
|
||||
def hello():
|
||||
print 'hello world!'
|
||||
|
||||
```
|
||||
|
||||
#### Run celery in development
|
||||
|
||||
```
|
||||
$ cd apps
|
||||
$ celery -A common worker -l info
|
||||
```
|
||||
|
||||
#### Test using task
|
||||
|
||||
```
|
||||
$ ./manage.py shell
|
||||
>>> from ops.tasks import longtime_add
|
||||
>>> res = longtime_add.delay(1, 2)
|
||||
>>> res.get()
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
6
apps/common/__init__.py
Normal file
6
apps/common/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
# This will make sure the app is always imported when
|
||||
# Django starts so that shared_task will use this app.
|
||||
from .celery import app as celery_app
|
||||
|
7
apps/common/apps.py
Normal file
7
apps/common/apps.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CommonConfig(AppConfig):
|
||||
name = 'common'
|
21
apps/common/celery.py
Normal file
21
apps/common/celery.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import os
|
||||
|
||||
from celery import Celery
|
||||
|
||||
# set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
app = Celery('jumpserver')
|
||||
|
||||
# Using a string here means the worker will not have to
|
||||
# pickle the object when using Windows.
|
||||
app.config_from_object('django.conf:settings')
|
||||
|
||||
app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS])
|
||||
|
5
apps/common/models.py
Normal file
5
apps/common/models.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
34
apps/common/tasks.py
Normal file
34
apps/common/tasks.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from celery import shared_task
|
||||
from django.core.mail import send_mail
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
@shared_task(name='send_mail_async')
|
||||
def send_mail_async(*args, **kwargs):
|
||||
""" Using celery to send email async
|
||||
|
||||
You can use it as django send_mail function
|
||||
|
||||
Example:
|
||||
send_mail_sync.delay(subject, message, from_mail, recipient_list, fail_silently=False, html_message=None)
|
||||
|
||||
Also you can ignore the from_mail, unlike django send_mail, from_email is not a require args:
|
||||
|
||||
Example:
|
||||
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
|
||||
"""
|
||||
if len(args) == 3:
|
||||
args = list(args)
|
||||
args[0] = settings.EMAIL_SUBJECT_PREFIX + args[0]
|
||||
args.insert(2, settings.EMAIL_HOST_USER)
|
||||
args = tuple(args)
|
||||
|
||||
send_mail(*args, **kwargs)
|
||||
|
||||
|
||||
# def send_mail_async(subject, message, from_mail, recipient_list, fail_silently=False, html_message=None):
|
||||
# if settings.CONFIG.MAIL_SUBJECT_PREFIX:
|
||||
# subject += settings.CONFIG.MAIL_SUBJECT_PREFIX
|
||||
# send_mail(subject, message, from_mail, recipient_list, fail_silently=fail_silently, html_message=html_message)
|
60
apps/common/templates/common/flash_message_standalone.html
Normal file
60
apps/common/templates/common/flash_message_standalone.html
Normal file
@@ -0,0 +1,60 @@
|
||||
{% load static %}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<title>{{ title }}</title>
|
||||
|
||||
{% include '_head_css_js.html' %}
|
||||
<link href="{% static "css/jumpserver.css" %}" rel="stylesheet">
|
||||
<script src="{% static "js/jumpserver.js" %}"></script>
|
||||
|
||||
</head>
|
||||
|
||||
<body class="gray-bg">
|
||||
<div class="passwordBox2 animated fadeInDown">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="ibox-content">
|
||||
<div>
|
||||
<img src="{% static 'img/logo.png' %}" style="margin: auto" width="82" height="82">
|
||||
<h2 style="display: inline">Jumpserver</h2>
|
||||
</div>
|
||||
{% if errors %}
|
||||
<p>
|
||||
<div class="alert alert-danger">
|
||||
{{ errors }}
|
||||
</div>
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if messages %}
|
||||
<p>
|
||||
<div class="alert alert-success">
|
||||
{{ messages }}
|
||||
</div>
|
||||
</p>
|
||||
{% endif %}
|
||||
<div class="row">
|
||||
<div class="col-lg-3">
|
||||
<a href="{{ redirect_url }}" class="btn btn-primary block full-width m-b">返回</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr/>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
Copyright Jumpserver.org
|
||||
</div>
|
||||
<div class="col-md-6 text-right">
|
||||
<small>© 2014-2016</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
0
apps/common/templatetags/__init__.py
Normal file
0
apps/common/templatetags/__init__.py
Normal file
39
apps/common/templatetags/common_tags.py
Normal file
39
apps/common/templatetags/common_tags.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
|
||||
from django import template
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter
|
||||
def join_queryset_attr(queryset, attr, delimiter=', '):
|
||||
return delimiter.join([getattr(obj, attr, '') for obj in queryset])
|
||||
|
||||
|
||||
@register.filter
|
||||
def pagination_range(total_page, current_num=1, display=5):
|
||||
"""Return Page range
|
||||
|
||||
:param total_page: Total numbers of paginator
|
||||
:param current_num: current display page num
|
||||
:param display: Display as many as [:display:] page
|
||||
|
||||
In order to display many page num on web like:
|
||||
< 1 2 3 4 5 >
|
||||
"""
|
||||
try:
|
||||
current_num = int(current_num)
|
||||
except ValueError:
|
||||
current_num = 1
|
||||
|
||||
start = current_num - display/2 if current_num > display/2 else 1
|
||||
end = start + display if start + display <= total_page else total_page + 1
|
||||
|
||||
return range(start, end)
|
||||
|
||||
|
||||
|
||||
|
3
apps/common/tests.py
Normal file
3
apps/common/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
24
apps/common/utils.py
Normal file
24
apps/common/utils.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.shortcuts import reverse as dj_reverse
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None, external=False):
|
||||
url = dj_reverse(viewname, urlconf=urlconf, args=args, kwargs=kwargs, current_app=current_app)
|
||||
|
||||
if external:
|
||||
url = settings.SITE_URL.strip('/') + url
|
||||
return url
|
||||
|
||||
|
||||
def get_object_or_none(model, **kwargs):
|
||||
try:
|
||||
obj = model.objects.get(**kwargs)
|
||||
except model.DoesNotExist:
|
||||
obj = None
|
||||
return obj
|
||||
|
7
apps/common/views.py
Normal file
7
apps/common/views.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user