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

50% done with csv upload

parent cfdbe91e
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(Employee)
admin.site.register(Productivity)
admin.site.register(JobOrder)
admin.site.register(Position)
admin.site.register(History)
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
import django.db.models.deletion
......@@ -34,6 +34,18 @@ class Migration(migrations.Migration):
('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(
name='Productivity',
fields=[
......@@ -42,16 +54,10 @@ class Migration(migrations.Migration):
('workinghours', models.DurationField()),
('remarks', models.TextField(blank=True, max_length=2000)),
('prod_score', models.FloatField(null=True)),
('employee', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='EmployeeProdDB.employee')),
],
),
migrations.CreateModel(
name='JobOrder',
fields=[
('joborder_no', models.CharField(max_length=10, primary_key=True, serialize=False)),
('joborder_no', models.CharField(max_length=10)),
('process', 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(
......
# 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):
# db_table = 'employee'
class Productivity(models.Model):
report_no = models.CharField(max_length=10, primary_key=True)
employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
prod_date = models.DateField()
report_no = models.AutoField(max_length=10, primary_key=True)
#employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
prod_date = models.DateField(null=True)
workinghours = models.DurationField()
remarks = models.TextField(max_length=2000, blank=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:
# constraints = [
......@@ -38,11 +41,11 @@ class Productivity(models.Model):
# )
# ]
class JobOrder(models.Model):
joborder_no = models.CharField(max_length=10, primary_key=True)
report_no = models.ForeignKey('Productivity', on_delete=models.CASCADE)
process = models.CharField(max_length=255)
status = models.CharField(max_length=255)
#class JobOrder(models.Model):
#joborder_no = models.CharField(max_length=10, primary_key=True)
#report_no = models.ForeignKey('Productivity', on_delete=models.CASCADE)
#process = models.CharField(max_length=255)
#status = models.CharField(max_length=255)
class Position(models.Model):
......
import io
import csv
from datetime import datetime
from datetime import datetime, timedelta
from django.shortcuts import render, redirect
from .models import Productivity, User
from .models import Productivity, User, Employee
from django.contrib import messages
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):
return render(request, 'EmployeeProdDB/home.html')
def loginpage(request):
if(request.method == "POST"):
uname = request.POST.get('username')
......@@ -46,33 +48,74 @@ def upload_csv(request):
quotechar='"'
)
# create and save model instances for each row of data
for row in csv_data:
report_no = row[0]
employee_id = row[1]
prod_date_str = row[2]
#prod_date = row[2]
encountered_empty_row = False
# Loop through data rows
for i, row in enumerate(csv_data):
# Check if this row is the start of a new report
if row[0] == 'PRODUCTIVITY REPORT':
# Reset variables for the new report
report_no = row[1]
employee = None
prod_date = None
if prod_date_str:
prod_date = parse_date(prod_date_str)
workinghours_str = row[3]
workinghours = None
if workinghours_str:
workinghours = parse_duration(workinghours_str)
remarks = row[4]
prod_score = row[5]
remarks = None
prod_score = None
joborder_no = None
process = None
status = None
# Skip 8 rows
count = 0
while count < 8:
next(csv_data)
count += 1
# Reset the flag for encountered empty row
encountered_empty_row = False
# 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_id=employee_id,
prod_date=prod_date,
#report_no=report_no,
#employee=employee,
#prod_date=prod_date,
workinghours=workinghours,
remarks=remarks,
prod_score=prod_score,
# add more fields as needed
#prod_score=prod_score,
joborder_no=joborder_no,
process=process,
status=status
)
return redirect('show_csv_data')
return render(request, 'EmployeeProdDB/upload_csv.html', {'message': 'Data imported successfully!'})
return render(request, 'EmployeeProdDB/upload_csv.html')
......@@ -81,7 +124,6 @@ def show_csv_data(request):
csv_data = Productivity.objects.all()
return render(request, 'EmployeeProdDB/show_csv_data.html', {'csv_data': csv_data})
#def handle_file_upload(request):
# if request.method == 'POST':
# 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