Upload files to "base/manage/commands"
Signed-off-by: nestict <developer@nestict.com>
This commit is contained in:
75
base/manage/commands/createhorillauser.py
Normal file
75
base/manage/commands/createhorillauser.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
Horilla management command to create a new user and associated employee.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from employee.models import Employee
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""
|
||||
Horilla management command to create a new user and associated employee.
|
||||
"""
|
||||
|
||||
help = "Creates a new user"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("--first_name", type=str, help="First name of the new user")
|
||||
parser.add_argument("--last_name", type=str, help="Last name of the new user")
|
||||
parser.add_argument("--username", type=str, help="Username of the new user")
|
||||
parser.add_argument("--password", type=str, help="Password for the new user")
|
||||
parser.add_argument("--email", type=str, help="Email of the new user")
|
||||
parser.add_argument("--phone", type=str, help="Phone number of the new user")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if not options["first_name"]:
|
||||
first_name = input("Enter first name: ")
|
||||
last_name = input("Enter last name: ")
|
||||
username = input("Enter username: ")
|
||||
password = input("Enter password: ")
|
||||
email = input("Enter email: ")
|
||||
phone = input("Enter phone number: ")
|
||||
else:
|
||||
first_name = options["first_name"]
|
||||
last_name = options["last_name"]
|
||||
username = options["username"]
|
||||
password = options["password"]
|
||||
email = options["email"]
|
||||
phone = options["phone"]
|
||||
|
||||
if User.objects.filter(username=username).exists():
|
||||
self.stdout.write(
|
||||
self.style.WARNING(f'User with username "{username}" already exists')
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
user = User.objects.create_superuser(
|
||||
username=username, email=email, password=password
|
||||
)
|
||||
employee = Employee()
|
||||
employee.employee_user_id = user
|
||||
employee.employee_first_name = first_name
|
||||
employee.employee_last_name = last_name
|
||||
employee.email = email
|
||||
employee.phone = phone
|
||||
employee.save()
|
||||
|
||||
bot = User.objects.filter(username="Horilla Bot").first()
|
||||
if bot is None:
|
||||
User.objects.create_user(
|
||||
username="Horilla Bot",
|
||||
password=str(uuid.uuid4()),
|
||||
)
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'Employee "{employee}" created successfully')
|
||||
)
|
||||
except Exception as e:
|
||||
if "user" in locals():
|
||||
user.delete()
|
||||
raise CommandError(f'Error creating user "{username}": {e}') from e
|
||||
55
base/manage/commands/horilladumpdata.py
Normal file
55
base/manage/commands/horilladumpdata.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import os
|
||||
|
||||
from django.apps import apps as django_apps
|
||||
from django.conf import settings
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Dump all relevant data to JSON files."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
|
||||
folder_name = input("Enter the name of the folder to save JSON files: ").strip()
|
||||
if not folder_name:
|
||||
self.stderr.write(self.style.ERROR("Folder name cannot be empty."))
|
||||
return
|
||||
|
||||
dump_dir = os.path.join(settings.BASE_DIR, folder_name)
|
||||
os.makedirs(dump_dir, exist_ok=True)
|
||||
|
||||
data_files = [
|
||||
("auth.User", "user_data.json"),
|
||||
("employee.Employee", "employee_info_data.json"),
|
||||
("base", "base_data.json"),
|
||||
("employee.employeeworkinformation", "work_info_data.json"),
|
||||
("employee.employeebankdetails", "bank_info_data.json"),
|
||||
]
|
||||
|
||||
optional_apps = [
|
||||
("attendance", "attendance_data.json"),
|
||||
("leave", "leave_data.json"),
|
||||
("asset", "asset_data.json"),
|
||||
("recruitment", "recruitment_data.json"),
|
||||
("onboarding", "onboarding_data.json"),
|
||||
("offboarding", "offboarding_data.json"),
|
||||
("pms", "pms_data.json"),
|
||||
("payroll", "payroll_data.json"),
|
||||
("payroll", "payroll_loanaccount_data.json"),
|
||||
("project", "project_data.json"),
|
||||
]
|
||||
|
||||
data_files += [
|
||||
(app, file) for app, file in optional_apps if django_apps.is_installed(app)
|
||||
]
|
||||
|
||||
for data in data_files:
|
||||
output_path = os.path.join(dump_dir, data[1])
|
||||
|
||||
try:
|
||||
with open(output_path, "w") as f:
|
||||
call_command("dumpdata", data[0], stdout=f, indent=4)
|
||||
self.stdout.write(self.style.SUCCESS(f"Dumped: {data[0]}"))
|
||||
except Exception as e:
|
||||
self.stderr.write(self.style.ERROR(f"Error dumping {data[0]}: {e}"))
|
||||
44
base/manage/commands/trigger_save.py
Normal file
44
base/manage/commands/trigger_save.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from django.apps import apps
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Save all instances of the specified model"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
"app_label", type=str, help="App label of the model (e.g., attendance)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"model_name",
|
||||
type=str,
|
||||
help="Name of the model to save instances for (e.g., Attendance)",
|
||||
)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
app_label = kwargs["app_label"]
|
||||
model_name = kwargs["model_name"]
|
||||
|
||||
try:
|
||||
model = apps.get_model(app_label, model_name)
|
||||
except LookupError:
|
||||
raise CommandError(
|
||||
f"Model '{model_name}' not found in the app '{app_label}'."
|
||||
)
|
||||
|
||||
try:
|
||||
instances = model.objects.all()
|
||||
print(
|
||||
f"Saving {len(instances)} instances of '{model_name}' in app '{app_label}'. Please wait....."
|
||||
)
|
||||
for instance in instances:
|
||||
instance.save()
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"All instances of '{model_name}' in app '{app_label}' have been saved."
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
raise CommandError(
|
||||
f"An error occurred while saving instances of '{model_name}' in app '{app_label}': {e}"
|
||||
)
|
||||
Reference in New Issue
Block a user