Commit 3a4c0685 authored by Dexter Sapugay's avatar Dexter Sapugay

50% done with csv upload

parent cfdbe91e
from django.contrib import admin from django.contrib import admin
from .models import Employee, Productivity, JobOrder, Position, History, Dashboard, User from .models import Employee, Productivity, Position, History, Dashboard, User
admin.site.register(User) admin.site.register(User)
admin.site.register(Employee) admin.site.register(Employee)
admin.site.register(Productivity) admin.site.register(Productivity)
admin.site.register(JobOrder)
admin.site.register(Position) admin.site.register(Position)
admin.site.register(History) admin.site.register(History)
admin.site.register(Dashboard) admin.site.register(Dashboard)
......
# Generated by Django 4.0.4 on 2023-02-13 16:35 # Generated by Django 4.0.4 on 2023-04-02 13:16
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
...@@ -34,6 +34,18 @@ class Migration(migrations.Migration): ...@@ -34,6 +34,18 @@ class Migration(migrations.Migration):
('position_name', models.CharField(max_length=255)), ('position_name', models.CharField(max_length=255)),
], ],
), ),
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=300, unique=True)),
('password', models.CharField(max_length=300)),
('first_name', models.CharField(max_length=300)),
('last_name', models.CharField(max_length=300)),
('birthday', models.DateField()),
('sex', models.CharField(max_length=50)),
],
),
migrations.CreateModel( migrations.CreateModel(
name='Productivity', name='Productivity',
fields=[ fields=[
...@@ -42,16 +54,10 @@ class Migration(migrations.Migration): ...@@ -42,16 +54,10 @@ class Migration(migrations.Migration):
('workinghours', models.DurationField()), ('workinghours', models.DurationField()),
('remarks', models.TextField(blank=True, max_length=2000)), ('remarks', models.TextField(blank=True, max_length=2000)),
('prod_score', models.FloatField(null=True)), ('prod_score', models.FloatField(null=True)),
('employee', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='EmployeeProdDB.employee')), ('joborder_no', models.CharField(max_length=10)),
],
),
migrations.CreateModel(
name='JobOrder',
fields=[
('joborder_no', models.CharField(max_length=10, primary_key=True, serialize=False)),
('process', models.CharField(max_length=255)), ('process', models.CharField(max_length=255)),
('status', models.CharField(max_length=255)), ('status', models.CharField(max_length=255)),
('report_no', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='EmployeeProdDB.productivity')), ('employee', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='EmployeeProdDB.employee')),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
......
# Generated by Django 4.0.4 on 2023-04-02 14:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('EmployeeProdDB', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='productivity',
name='employee',
),
migrations.AlterField(
model_name='productivity',
name='prod_date',
field=models.DateField(null=True),
),
migrations.AlterField(
model_name='productivity',
name='report_no',
field=models.AutoField(max_length=10, primary_key=True, serialize=False),
),
]
# Generated by Django 4.0.4 on 2023-02-14 17:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('EmployeeProdDB', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=300, unique=True)),
('password', models.CharField(max_length=300)),
('first_name', models.CharField(max_length=300)),
('last_name', models.CharField(max_length=300)),
('birthday', models.DateField()),
('sex', models.CharField(max_length=50)),
],
),
]
...@@ -22,12 +22,15 @@ class Employee(models.Model): ...@@ -22,12 +22,15 @@ class Employee(models.Model):
# db_table = 'employee' # db_table = 'employee'
class Productivity(models.Model): class Productivity(models.Model):
report_no = models.CharField(max_length=10, primary_key=True) report_no = models.AutoField(max_length=10, primary_key=True)
employee = models.ForeignKey('Employee', on_delete=models.CASCADE) #employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
prod_date = models.DateField() prod_date = models.DateField(null=True)
workinghours = models.DurationField() workinghours = models.DurationField()
remarks = models.TextField(max_length=2000, blank=True) remarks = models.TextField(max_length=2000, blank=True)
prod_score = models.FloatField(null=True) prod_score = models.FloatField(null=True)
joborder_no = models.CharField(max_length=10)
process = models.CharField(max_length=255)
status = models.CharField(max_length=255)
# class Meta: # class Meta:
# constraints = [ # constraints = [
...@@ -38,11 +41,11 @@ class Productivity(models.Model): ...@@ -38,11 +41,11 @@ class Productivity(models.Model):
# ) # )
# ] # ]
class JobOrder(models.Model): #class JobOrder(models.Model):
joborder_no = models.CharField(max_length=10, primary_key=True) #joborder_no = models.CharField(max_length=10, primary_key=True)
report_no = models.ForeignKey('Productivity', on_delete=models.CASCADE) #report_no = models.ForeignKey('Productivity', on_delete=models.CASCADE)
process = models.CharField(max_length=255) #process = models.CharField(max_length=255)
status = models.CharField(max_length=255) #status = models.CharField(max_length=255)
class Position(models.Model): class Position(models.Model):
......
import io
import csv import csv
from datetime import datetime from datetime import datetime, timedelta
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from .models import Productivity, User from .models import Productivity, User, Employee
from django.contrib import messages from django.contrib import messages
from django.utils.dateparse import parse_date, parse_duration from django.utils.dateparse import parse_date, parse_duration
#from dateutil.parser import parse as parse_date from dateutil.parser import parse as parse_date
def home(request): def home(request):
return render(request, 'EmployeeProdDB/home.html') return render(request, 'EmployeeProdDB/home.html')
def loginpage(request): def loginpage(request):
if(request.method == "POST"): if(request.method == "POST"):
uname = request.POST.get('username') uname = request.POST.get('username')
...@@ -46,33 +48,74 @@ def upload_csv(request): ...@@ -46,33 +48,74 @@ def upload_csv(request):
quotechar='"' quotechar='"'
) )
# create and save model instances for each row of data encountered_empty_row = False
for row in csv_data:
report_no = row[0] # Loop through data rows
employee_id = row[1] for i, row in enumerate(csv_data):
prod_date_str = row[2] # Check if this row is the start of a new report
#prod_date = row[2] if row[0] == 'PRODUCTIVITY REPORT':
prod_date = None # Reset variables for the new report
if prod_date_str: report_no = row[1]
prod_date = parse_date(prod_date_str) employee = None
workinghours_str = row[3] prod_date = None
workinghours = None workinghours = None
if workinghours_str: remarks = None
workinghours = parse_duration(workinghours_str) prod_score = None
remarks = row[4] joborder_no = None
prod_score = row[5] process = None
status = None
Productivity.objects.create(
report_no=report_no, # Skip 8 rows
employee_id=employee_id, count = 0
prod_date=prod_date, while count < 8:
workinghours=workinghours, next(csv_data)
remarks=remarks, count += 1
prod_score=prod_score,
# add more fields as needed # Reset the flag for encountered empty row
) encountered_empty_row = False
return redirect('show_csv_data') # Read headers from 9th row
headers = next(csv_data)
jo_no_index = headers.index('JO NO')
status_index = headers.index('Status')
process_index = headers.index('Process')
duration_index = headers.index('Duration')
remarks_index = headers.index('Remarks')
elif all(cell == '' for cell in row):
# Check if an empty row has been encountered before
if encountered_empty_row:
# Stop processing data as we have encountered two consecutive empty rows
break
else:
# Set the flag to indicate that an empty row has been encountered
encountered_empty_row = True
else:
# Read data from the row
joborder_no = row[jo_no_index]
status = row[status_index]
process = row[process_index]
workinghours = row[duration_index]
remarks = row[remarks_index]
# Convert working hours to a Duration object
if workinghours:
hours, minutes = map(int, workinghours.split(':'))
workinghours = timedelta(hours=hours, minutes=minutes)
# Create a new productivity object
Productivity.objects.create(
#report_no=report_no,
#employee=employee,
#prod_date=prod_date,
workinghours=workinghours,
remarks=remarks,
#prod_score=prod_score,
joborder_no=joborder_no,
process=process,
status=status
)
return render(request, 'EmployeeProdDB/upload_csv.html', {'message': 'Data imported successfully!'})
return render(request, 'EmployeeProdDB/upload_csv.html') return render(request, 'EmployeeProdDB/upload_csv.html')
...@@ -81,7 +124,6 @@ def show_csv_data(request): ...@@ -81,7 +124,6 @@ def show_csv_data(request):
csv_data = Productivity.objects.all() csv_data = Productivity.objects.all()
return render(request, 'EmployeeProdDB/show_csv_data.html', {'csv_data': csv_data}) return render(request, 'EmployeeProdDB/show_csv_data.html', {'csv_data': csv_data})
#def handle_file_upload(request): #def handle_file_upload(request):
# if request.method == 'POST': # if request.method == 'POST':
# file = request.FILES.get('file-input') # file = request.FILES.get('file-input')
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment