2018-03-25 17:16:05 +00:00
|
|
|
# ⁻*- mode: python; coding: utf-8 -*-
|
2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2018-03-25 17:16:05 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Lev-Arcady Sellem
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2018-04-14 19:29:16 +00:00
|
|
|
"""re2o.script_utils
|
|
|
|
A set of utility scripts that can be used as standalone to interact easily
|
|
|
|
with Re2o throught the CLI
|
|
|
|
"""
|
2018-03-25 17:16:05 +00:00
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
import os
|
|
|
|
import pwd
|
2021-02-10 10:06:09 +00:00
|
|
|
import sys
|
2018-04-14 19:29:16 +00:00
|
|
|
from getpass import getpass
|
2021-02-10 10:06:09 +00:00
|
|
|
from os.path import dirname
|
2018-03-25 17:16:05 +00:00
|
|
|
|
|
|
|
from django.core.management.base import CommandError
|
2021-02-10 10:06:09 +00:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2018-03-25 17:19:50 +00:00
|
|
|
from django.db import transaction
|
2018-04-14 19:29:16 +00:00
|
|
|
from django.utils.html import strip_tags
|
2021-02-10 10:06:09 +00:00
|
|
|
from reversion import revisions as reversion
|
2018-04-14 19:29:16 +00:00
|
|
|
|
|
|
|
from users.models import User
|
2018-03-25 17:19:50 +00:00
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
proj_path = dirname(dirname(__file__))
|
2018-04-13 23:42:22 +00:00
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "re2o.settings")
|
|
|
|
sys.path.append(proj_path)
|
|
|
|
os.chdir(proj_path)
|
|
|
|
|
|
|
|
application = get_wsgi_application()
|
|
|
|
|
2018-03-25 17:19:50 +00:00
|
|
|
|
2018-03-25 17:16:05 +00:00
|
|
|
def get_user(pseudo):
|
2020-04-25 01:36:27 +00:00
|
|
|
"""Find a user from its pseudo
|
2021-02-10 10:06:09 +00:00
|
|
|
|
2020-04-25 01:36:27 +00:00
|
|
|
Parameters:
|
|
|
|
pseudo (string): pseudo of this user
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
user instance:Instance of user
|
2021-02-10 10:06:09 +00:00
|
|
|
|
2020-04-25 01:36:27 +00:00
|
|
|
"""
|
2018-03-25 17:16:05 +00:00
|
|
|
user = User.objects.filter(pseudo=pseudo)
|
2018-04-13 23:42:22 +00:00
|
|
|
if len(user) == 0:
|
2018-06-23 17:53:10 +00:00
|
|
|
raise CommandError("Invalid user.")
|
2018-04-13 23:42:22 +00:00
|
|
|
if len(user) > 1:
|
2021-02-10 10:06:09 +00:00
|
|
|
raise CommandError("Several users match this username. This SHOULD NOT happen.")
|
2018-03-25 17:16:05 +00:00
|
|
|
return user[0]
|
|
|
|
|
2018-03-25 17:18:40 +00:00
|
|
|
|
|
|
|
def get_system_user():
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Find the system user login who used the command"""
|
2018-03-25 17:18:40 +00:00
|
|
|
return pwd.getpwuid(int(os.getenv("SUDO_UID") or os.getuid())).pw_name
|
2018-03-25 17:19:50 +00:00
|
|
|
|
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
def form_cli(Form, user, action, *args, **kwargs):
|
2018-03-25 17:19:50 +00:00
|
|
|
"""
|
2020-04-25 01:36:27 +00:00
|
|
|
Fill-in a django form from cli
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Form : a django class form to fill-in
|
|
|
|
user : a re2o user doign the modification
|
|
|
|
action: the action done with that form, for logs purpose
|
2021-02-10 10:06:09 +00:00
|
|
|
|
2018-03-25 17:19:50 +00:00
|
|
|
"""
|
2018-04-13 23:42:22 +00:00
|
|
|
data = {}
|
|
|
|
dumb_form = Form(user=user, *args, **kwargs)
|
2018-03-25 17:19:50 +00:00
|
|
|
for key in dumb_form.fields:
|
2019-11-04 16:55:03 +00:00
|
|
|
if not dumb_form.fields[key].widget.input_type == "hidden":
|
|
|
|
if dumb_form.fields[key].widget.input_type == "password":
|
2018-04-13 23:42:22 +00:00
|
|
|
data[key] = getpass("%s : " % dumb_form.fields[key].label)
|
2018-03-25 17:19:50 +00:00
|
|
|
else:
|
2018-04-13 23:42:22 +00:00
|
|
|
data[key] = input("%s : " % dumb_form.fields[key].label)
|
2018-03-25 17:19:50 +00:00
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
form = Form(data, user=user, *args, **kwargs)
|
2018-03-25 17:19:50 +00:00
|
|
|
if not form.is_valid():
|
2018-06-23 17:53:10 +00:00
|
|
|
sys.stderr.write("Errors: \n")
|
2018-03-25 17:19:50 +00:00
|
|
|
for err in form.errors:
|
2018-04-13 23:42:22 +00:00
|
|
|
# Oui, oui, on gère du HTML là où d'autres ont eu la
|
|
|
|
# lumineuse idée de le mettre
|
2019-11-04 16:55:03 +00:00
|
|
|
sys.stderr.write("\t%s : %s\n" % (err, strip_tags(form.errors[err])))
|
2018-06-23 17:53:10 +00:00
|
|
|
raise CommandError("Invalid form.")
|
2018-03-25 17:19:50 +00:00
|
|
|
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2018-04-13 23:42:22 +00:00
|
|
|
form.save()
|
|
|
|
reversion.set_user(user)
|
|
|
|
reversion.set_comment(action)
|
2018-03-25 17:19:50 +00:00
|
|
|
|
2021-02-10 10:06:09 +00:00
|
|
|
sys.stdout.write("%s: done. The edit may take several minutes to apply.\n" % action)
|