dodan user

This commit is contained in:
2024-01-18 12:03:37 +01:00
parent 75c00b0e91
commit 298219ab30
32 changed files with 912 additions and 2 deletions

View File

@@ -0,0 +1,42 @@
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from user.models import User
class Command(BaseCommand):
help = """
Programatically set application permissions and groups\n
usage: python manage.py create_groups_and_permissions.py
"""
def handle(self, *args, **options):
# get or create groups
admin_group, created = Group.objects.get_or_create(name='Admin')
operater_group, created = Group.objects.get_or_create(name='Operater')
viewer_group, created = Group.objects.get_or_create(name='Viewer')
# define content types
ct_user = ContentType.objects.get_for_model(User)
# define permissions
application_permissions = [
# (codename, name, content_type, list_of_groups_to_assign_perm)
('add_user', 'Can add new users', ct_user, [admin_group]),
('change_user', 'Can change existing user', ct_user, [admin_group]),
('delete_user', 'Can delete existing user', ct_user, [admin_group]),
('add_data', 'Can add new data', ct_user, [admin_group, operater_group]),
]
# get or create permissions and add them to appropriate groups
for permission_tuple in application_permissions:
permission_obj, created = Permission.objects.get_or_create(
codename=permission_tuple[0],
name=permission_tuple[1],
content_type=permission_tuple[2]
)
for group in permission_tuple[3]:
group.permissions.add(permission_obj)

View File

@@ -0,0 +1,71 @@
import csv
import os
import sys
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand
from user.models import Organization, User
class Command(BaseCommand):
help = """
Programatically create entries for model User
"""
def handle(self, *args, **options):
csv_fpath = os.path.join(settings.RESOURCES_DIR, 'users.csv')
csv.field_size_limit(sys.maxsize)
created_entries = 0
existing_entries = 0
admin_group, created = Group.objects.get_or_create(name='Admin')
operater_group, created = Group.objects.get_or_create(name='Operater')
with open(csv_fpath) as f:
reader = csv.DictReader(f)
default_organization = Organization.objects.get_or_create(name='Državna geodetska uprava')[0]
for row in reader:
username = row['usrname']
first_name = row['ime']
last_name = row['prezime']
email = row['email']
email_confirmed = True
if not email:
print("User {} {} doesn't have email but it's required. Setting fake email...".format(
first_name, last_name
))
# set fake email
email = '{}.{}@example.com'.format(first_name, last_name)
email_confirmed = False
obj, created = User.objects.get_or_create(
username=username,
first_name=first_name,
last_name=last_name,
email=email,
email_confirmed=email_confirmed,
organization=default_organization
)
if row['rola'] == 'operater':
obj.groups.add(operater_group)
elif row['rola'] == 'admin':
obj.groups.add(admin_group)
else:
obj.groups.clear()
if created:
created_entries += 1
print("Kreiran user {} {}".format(obj.first_name, obj.last_name))
else:
existing_entries += 1
print("Created: {}".format(created_entries))
print("Existing: {}".format(existing_entries))