Skip to content

Commit

Permalink
01-June-2023: Employee Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
DPrakhin committed Jun 1, 2023
1 parent a3e4cae commit de24fad
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 37 deletions.
Binary file modified db.sqlite3
Binary file not shown.
99 changes: 75 additions & 24 deletions employees/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ def emp_about(request):
except:
employee_info = None

devices_count = 0
if employee_info:
if Devices.objects.filter(user_id=employee_info.id):
devices_count = len(Devices.objects.filter(user_id=employee_info.id))

context['employee_info'] = employee_info
context['employee_data'] = employee_info
context['devices_count'] = devices_count
context['user_admin'] = user_admin
return render(request, 'employees/about.html', context=context)

Expand Down Expand Up @@ -309,38 +315,83 @@ def emp_delete(request, emp_id):
context['user_admin'] = user_admin
# ---

# Важливі додаткові дані:
# 1. Кількість обладнання за співробітником:

# 2. Чи є матеріально-відповідальна особа - product_owner:
if len(Company.objects.all()) > 0:
company_info = Company.objects.all()
# 1. Базова інформація про співробітника:
employee_info = Employees.objects.get(id=emp_id)

for company in company_info:
product_owner = company.product_owner
product_owner_id = company.product_owner.id
# 2. Кількість обладнання у співробітника:
if Devices.objects.filter(user_id=emp_id):
devices_count = len(Devices.objects.filter(user_id=emp_id))
else:
product_owner = None
devices_count = 0

print('Product owner = ', product_owner)
print('Product owner ID = ', product_owner_id)
# 3. Перевіряємо, чи є product_owner
# визначаємо його ID:
product_owner = -1
product_owner_email = None

# 3. Перевіряємо, чи є користува з доступом до системи:
# Ищем не так, Искать нужно по эл.почте => Переписать
# NewUser.objects.get(email=employee_email)
try:
user_account = NewUser.objects.get(id=emp_id)
except:
user_account = None
if Company.objects.all():
for company in Company.objects.all():
if company.product_owner:
if company.product_owner.id == int(emp_id):
product_owner = company.product_owner.id
product_owner_email = Employees.objects.get(id=product_owner).email
product_owner = -10
else:
product_owner = company.product_owner.id
product_owner_email = Employees.objects.get(id=product_owner).email

# 4. Шукаємо користувача пов'язаного зі співробітником:
user_employee_id = None
if NewUser.objects.filter(email=employee_info.email):
user_employee_id = NewUser.objects.get(email=employee_info.email).id

# 5. Робимо перевірку, чи може бути співробітник видалений із бази:
if request.user.email == employee_info.email:
# Користувач не може видалити сам себе:
can_delete = -1
elif product_owner == -1 and devices_count == 0:
can_delete = 1
elif product_owner >= 0:
can_delete = 10
else:
user_account_id = user_account.id

print('User Account = ', user_account)
print('User Account ID =', user_account_id)
can_delete = -1

# Подальша обробка:

if request.method == 'POST':
# Видалення - простий випадок:
if can_delete == 1:
employee_info.delete()
user_account = NewUser.objects.get(id=user_employee_id)
user_account.is_employee = False
user_account.is_staff = False
user_account.is_active = False
user_account.save()

# Видалення, більш складний випадок:
elif can_delete == 10:
if devices_count > 0:
# Потрібно переписати усі пристрої на матеріально-відповідальну особу:
all_devices = Devices.objects.filter(user_id=emp_id)
for device in all_devices:
device.user_id = Employees.objects.get(email=product_owner_email)
device.save()

employee_info.delete()
user_account = NewUser.objects.get(id=user_employee_id)
user_account.is_employee = False
user_account.is_staff = False
user_account.is_active = False
user_account.save()

return redirect('/employees/list')

#context['product_owner'] = product_owner
context['employee_id'] = emp_id
context['employee_info'] = employee_info
context['devices_count'] = devices_count
context['product_owner'] = product_owner
context['product_owner_email'] = product_owner_email
context['can_delete'] = can_delete
return render(request, 'employees/emp_delete.html', context=context)


Expand Down
4 changes: 2 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def get_devices_data(self):
# НЕ ЗМІНЮВАТИ
@login_required
def main_page_users(request):
if not request.user.groups.filter(name='Users').exists():
return redirect("/")
if request.user.is_employee:
return redirect("/main/main_page/employees")

else:
user_data = {'u_id': request.user.id, 'u_email': request.user.email}
Expand Down
2 changes: 1 addition & 1 deletion templates/employees/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h3 class="headli-1">Інформація про спіробітника:</h3>
<div class='px-3 py-3 d-flex justify-content-between'>
<h3 class='card-title'>Кількість обладнання:</h3>
<div class="card-right d-flex align-items-center">
<p>{{ count }}</p>
<p>{{ devices_count }}</p>
</div>
</div>
<div class="chart-wrapper">
Expand Down
34 changes: 24 additions & 10 deletions templates/employees/emp_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,33 @@ <h4 class="card-title">Ви збираєтесь видалити відомос
<div class="card">
<div class="card-content" style="margin: 0 auto;">
<div class="card-body" >
<h5 class="card-title">Ім'я: {{ city_info.city }}</h5>
<h4 class="card-title">Прізвище: {{ emp_count }}</h4>
<h3 class="card-title">Електронна пошта: {{ company_count }}</h3>
<h3 class="card-title">Номер співробітника: {{ company_count }}</h3>
<h3 class="card-title">Кількість обладнання: {{ company_count }}</h3>
<h3 class="card-title">Матеріально відповідальна особа: {{ product_owner }}</h3>
<h5 class="card-title">Ім'я: {{ employee_info.first_name }}</h5>
<h4 class="card-title">Прізвище: {{ employee_info.last_name }}</h4>
<h3 class="card-title">Електронна пошта: {{ employee_info.email }}</h3>
<h3 class="card-title">Номер співробітника: {{ employee_info.eid }}</h3>
{% if devices_count == 0 %}
<h3 class="card-title" style="color: green; text-decoration: bold;">Кількість обладнання: {{ devices_count }}</h3>
{% else %}
<h3 class="card-title" style="color: red; text-decoration: bold;">Кількість обладнання: {{ devices_count }}</h3>
{% endif %}
{% if product_owner == -1 %}
<h3 class="card-title" style="color: red">Матеріально Відповідальна Особа (МВО) в компанії не вказана!</h3>
{% elif product_owner == -10 %}
<h3 class="card-title" style="color: red; text-decoration: bold;">Цей співробітник - Матеріально Відповідальна Особа (МВО)!</h3>
{% else %}
<h3 class="card-title" style="color: green; text-decoration: bold;">Матеріально Відповідальна Особа (МВО): {{ product_owner_email }}</h3>
{% endif %}
</div>
</div>
<div style="padding: 10px;">
<form action="/employees/delete/{{ city_info.id }}" method="POST">
{% csrf_token %}
<input type="submit" value="Видалити співробітника" class="btn btn-danger my-btn">
</form>
{% if can_delete == -1 %}
<h3 class="card-title" style="color: red">Нажаль співробітник не може бути видалений із бази!</h3>
{% else %}
<form action="/employees/delete/{{ employee_id }}" method="POST">
{% csrf_token %}
<input type="submit" value="Видалити співробітника" class="btn btn-danger my-btn">
</form>
{% endif %}
</div>
</div>
{% else %}
Expand Down

0 comments on commit de24fad

Please sign in to comment.