Skip to content
Snippets Groups Projects
Commit 6c2eee81 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

Merge branch 'interactive_commands' into 'django3_migration'

Interactive commands

See merge request !344
parents 7bdd5a80 17905c57
No related branches found
No related tags found
2 merge requests!344Interactive commands,!342Django 3 migration
Pipeline #42553 passed
Showing
with 353 additions and 467 deletions
......@@ -30,27 +30,16 @@
import datetime
import sys
from django.core.management.base import BaseCommand
from ....common.management.commands.base import InteractiveCommand
from ...models import Profile
from ...models import SupervisionTrack
class Command(BaseCommand):
class Command(InteractiveCommand):
help = "Block rejected users after rejection date with no valid supervisor"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
block = True
if options["interactive"]:
try:
......@@ -64,46 +53,33 @@ class Command(BaseCommand):
if answer != "y":
self.stdout.write("Block users operation canceled")
sys.exit(1)
if block:
rejected_profiles = Profile.objects.filter(status=Profile.REJECTED)
count = 0
for rejected_profile in rejected_profiles:
user = rejected_profile.user
if user.profile.rejection_date < datetime.datetime.now():
count += 1
user.profile.status = Profile.BLOCKED
user.profile.rejection_date = None
user.is_active = False
if user.profile.supervision_key is not None:
supervisiontrack = SupervisionTrack.objects.get(
supervision_key=rejected_profile.supervision_key
)
if (
not supervisiontrack.is_valid
and supervisiontrack.start_date is None
):
user.profile.supervision_key = None
supervisiontrack.delete()
user.profile.save()
user.save()
self.stdout.write(
"{} Rejected user(s) successfully blocked/".format(count)
+ "{} Total user(s) checked".format(rejected_profiles.count())
)
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == "":
raw_value = default
return raw_value
sys.exit(0)
rejected_profiles = Profile.objects.filter(status=Profile.REJECTED)
count = 0
for rejected_profile in rejected_profiles:
user = rejected_profile.user
if user.profile.rejection_date < datetime.datetime.now():
count += 1
user.profile.status = Profile.BLOCKED
user.profile.rejection_date = None
user.is_active = False
if user.profile.supervision_key is not None:
supervisiontrack = SupervisionTrack.objects.get(
supervision_key=rejected_profile.supervision_key
)
if (
not supervisiontrack.is_valid
and supervisiontrack.start_date is None
):
user.profile.supervision_key = None
supervisiontrack.delete()
user.profile.save()
user.save()
self.stdout.write(
"{} Rejected user(s) successfully blocked/".format(count)
+ "{} Total user(s) checked".format(rejected_profiles.count())
)
......@@ -29,26 +29,17 @@
import datetime
import sys
from django.core.management.base import BaseCommand
from ....common.management.commands.base import InteractiveCommand
from ...models import Profile
from ...models import SupervisionTrack
class Command(BaseCommand):
class Command(InteractiveCommand):
help = "Clean blocked users expired requests for supervisor validation"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
block = True
if options["interactive"]:
try:
......@@ -62,49 +53,36 @@ class Command(BaseCommand):
if answer != "y":
self.stdout.write("Clean blocked users operation canceled")
sys.exit(1)
if block:
blocked_profiles = Profile.objects.filter(status=Profile.BLOCKED)
count = 0
for blocked_profile in blocked_profiles:
user = blocked_profile.user
if user.profile.rejection_date is not None:
if user.profile.rejection_date < datetime.datetime.now():
count += 1
user.profile.status = Profile.BLOCKED
user.profile.rejection_date = None
user.is_active = False
if user.profile.supervision_key is not None:
supervisiontrack = SupervisionTrack.objects.get(
supervision_key=blocked_profile.supervision_key
)
if (
not supervisiontrack.is_valid
and supervisiontrack.start_date is None
):
user.profile.supervision_key = None
supervisiontrack.delete()
user.profile.save()
user.save()
self.stdout.write(
"{} Blocked user(s) successfully cleaned from expired supervision/".format(
count
)
+ "{} Total user(s) checked".format(blocked_profiles.count())
sys.exit(0)
blocked_profiles = Profile.objects.filter(status=Profile.BLOCKED)
count = 0
for blocked_profile in blocked_profiles:
user = blocked_profile.user
if user.profile.rejection_date is not None:
if user.profile.rejection_date < datetime.datetime.now():
count += 1
user.profile.status = Profile.BLOCKED
user.profile.rejection_date = None
user.is_active = False
if user.profile.supervision_key is not None:
supervisiontrack = SupervisionTrack.objects.get(
supervision_key=blocked_profile.supervision_key
)
if (
not supervisiontrack.is_valid
and supervisiontrack.start_date is None
):
user.profile.supervision_key = None
supervisiontrack.delete()
user.profile.save()
user.save()
self.stdout.write(
"{} Blocked user(s) successfully cleaned from expired supervision/".format(
count
)
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == "":
raw_value = default
return raw_value
+ "{} Total user(s) checked".format(blocked_profiles.count())
)
......@@ -30,24 +30,14 @@
import datetime
import sys
from django.core.management.base import BaseCommand
from ....common.management.commands.base import InteractiveCommand
from ...models import TemporaryUrl
class Command(BaseCommand):
class Command(InteractiveCommand):
help = "Clean expired temporary urls"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
if options["interactive"]:
......@@ -62,7 +52,7 @@ class Command(BaseCommand):
if answer != "y":
self.stdout.write("Clean expired temporary urls operation canceled")
sys.exit(1)
sys.exit(0)
temporary_urls_count = TemporaryUrl.objects.all().count()
now = datetime.datetime.now()
......@@ -71,15 +61,3 @@ class Command(BaseCommand):
"{} Expired temporary url(s) successfully cleaned/".format(count)
+ "{} Total temporary url(s) checked".format(temporary_urls_count)
)
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == "":
raw_value = default
return raw_value
......@@ -31,26 +31,17 @@ import datetime
import sys
from django.conf import settings
from django.core.management.base import BaseCommand
from ....common.management.commands.base import InteractiveCommand
from ....ui.registration.models import RegistrationProfile
from ...models import Profile
from ...models import SupervisionTrack
class Command(BaseCommand):
class Command(InteractiveCommand):
help = "Cleanup outdated invalid users"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
if options["interactive"]:
......@@ -120,15 +111,3 @@ class Command(BaseCommand):
registration_profile.delete()
return count_updated_users
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == "":
raw_value = default
return raw_value
......@@ -31,26 +31,17 @@ import datetime
import sys
from django.conf import settings
from django.core.management.base import BaseCommand
from ....common.management.commands.base import InteractiveCommand
from ....ui.registration.models import RegistrationProfile
from ...models import Profile
from ...models import SupervisionTrack
class Command(BaseCommand):
class Command(InteractiveCommand):
help = "Postpone user(s) validation process"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
if options["interactive"]:
......@@ -146,15 +137,3 @@ class Command(BaseCommand):
RegistrationProfile.objects.filter(user=invalid_profile.user).delete()
return count_updated_users
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == "":
raw_value = default
return raw_value
......@@ -27,52 +27,39 @@
###############################################################################
from django.core.management.base import BaseCommand, CommandError
import sys
from datetime import date
from ....common.management.commands.base import InteractiveCommand
from ...models import Attestation
import sys
class Command(BaseCommand):
help = 'Cleanup outdated attestations'
def add_arguments(self, parser):
parser.add_argument('--noinput', action='store_false', dest='interactive', default=True,
help=('Tells Django to NOT prompt the user for input of any kind.'))
class Command(InteractiveCommand):
help = "Cleanup outdated attestations"
def handle(self, *args, **options):
clean = True
if options['interactive']:
if options["interactive"]:
try:
answer = self.get_input_data('Delete attestation(s) (Y/n)? ', 'y').lower()
answer = self.get_input_data(
"Delete attestation(s) (Y/n)? ", "y"
).lower()
except KeyboardInterrupt:
self.stderr.write("\nOperation canceled.")
sys.exit(1)
if answer != 'y':
self.stdout.write('Cleanup canceled')
if answer != "y":
self.stdout.write("Cleanup canceled")
sys.exit(1)
if clean:
expired_attestations = Attestation.objects.filter(locked=True, expiration_date__lte=date.today())
expired_attestations = Attestation.objects.filter(
locked=True, expiration_date__lte=date.today()
)
attestion_count = expired_attestations.count()
expired_attestations.delete()
self.stdout.write('{} attestation(s) successfully cleaned'.format(attestion_count))
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = raw_input(message)
if default and raw_value == '':
raw_value = default
return raw_value
self.stdout.write(
"{} attestation(s) successfully cleaned".format(attestion_count)
)
# vim: set fileencoding=utf-8 :
# encoding: utf-8
###############################################################################
# #
# Copyright (c) 2020 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform 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. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.core.management.base import BaseCommand
class InteractiveCommand(BaseCommand):
"""Base class for all interactive commands"""
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=True,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message) # nosec only running on Python 3
if default and raw_value == "":
raw_value = default
return raw_value
......@@ -27,64 +27,51 @@
###############################################################################
from django.core.management.base import BaseCommand, CommandError
import random
import sys
from datetime import date
from ....common.management.commands.base import InteractiveCommand
from ...models import Report
import sys
import random
class Command(BaseCommand):
help = 'Cleanup outdated locked reports'
def add_arguments(self, parser):
parser.add_argument('--noinput', action='store_false', dest='interactive', default=True,
help=('Tells Django to NOT prompt the user for input of any kind.'))
class Command(InteractiveCommand):
help = "Cleanup outdated locked reports"
def handle(self, *args, **options):
clean = True
if options['interactive']:
if options["interactive"]:
try:
answer = self.get_input_data('Make report(s) editable again with new unique id (Y/n)? ', 'y').lower()
answer = self.get_input_data(
"Make report(s) editable again with new unique id (Y/n)? ", "y"
).lower()
except KeyboardInterrupt:
self.stderr.write("\nOperation canceled.")
sys.exit(1)
if answer != 'y':
self.stdout.write('Cleanup canceled')
if answer != "y":
self.stdout.write("Cleanup canceled")
sys.exit(1)
if clean:
expired_reports = Report.objects.filter(status=Report.LOCKED, expiration_date__lte=date.today())
expired_reports = Report.objects.filter(
status=Report.LOCKED, expiration_date__lte=date.today()
)
report_count = expired_reports.count()
for expired_report in expired_reports:
# Generate a unique report number
used_numbers = map(lambda x: x.number, Report.objects.all())
used_numbers = Report.objects.values_list("number", flat=True)
number = 0
while (number == 0) or number in used_numbers:
number = random.randint(100000, 2**31)
number = random.randint(100000, 2 ** 31) # nosec
expired_report.status = Report.EDITABLE
expired_report.number = number
expired_report.expiration_date = None
expired_report.save()
self.stdout.write('{} locked report(s) successfully cleaned'.format(report_count))
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = raw_input(message)
if default and raw_value == '':
raw_value = default
return raw_value
self.stdout.write(
"{} locked report(s) successfully cleaned".format(report_count)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment