def training_program_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select tp.id, tp.title, tp.start_date, tp.end_date, tp.capacity, tp.description from hrapp_trainingprogram tp """) all_training_programs = [] dataset = db_cursor.fetchall() for row in dataset: training_program = TrainingProgram() training_program.id = row['id'] training_program.title = row['title'] training_program.start_date = row['start_date'] training_program.end_date = row['end_date'] training_program.capacity = row['capacity'] training_program.description = row['description'] training_program.start_date = datetime.strptime(row['start_date'], '%Y-%m-%d') now = datetime.today() if training_program.start_date >= now: all_training_programs.append(training_program) template = 'training_programs/list.html' context = { 'training_programs': all_training_programs } return render(request, template, context) elif request.method == 'POST': form_data = request.POST with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute(""" INSERT INTO hrapp_trainingprogram ( title, start_date, end_date, capacity, description ) VALUES (?, ?, ?, ?, ?) """, (form_data['title'], form_data['start_date'], form_data['end_date'], form_data['capacity'], form_data['description'])) return redirect(reverse('hrapp:training_program_list'))
def training_program_details(request, training_program_id): if request.method == "GET": training_program_details = get_training_program_details( training_program_id) employee_training = TrainingProgram() for row in training_program_details: if employee_training.title == "": employee_training.id = row['id'] employee_training.title = row['title'] employee_training.start_date = row['start_date'] employee_training.end_date = row['end_date'] employee_training.capacity = row['capacity'] employee_training.is_future = is_future_training( employee_training.start_date) employee_training.attendees = [] if row['employee_id']: employee_training.attendees.append( (row['employee_id'], f"{row['first_name']} {row['last_name']}")) else: employee_training.attendees.append( (row['employee_id'], f"{row['first_name']} {row['last_name']}")) template = "training_programs/training_programs_detail.html" context = {"training_details": employee_training} return render(request, template, context) if request.method == 'POST': form_data = request.POST if ("actual_method" in form_data and form_data["actual_method"] == "DELETE"): with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute( """ DELETE FROM hrapp_trainingprogram WHERE id = ? """, (training_program_id, )) return redirect(reverse('hrapp:training_program_list')) # Check if this POST is for editing a book if "actual_method" in form_data and form_data["actual_method"] == "PUT": with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute( """ UPDATE hrapp_trainingprogram SET title = ?, start_date = ?, end_date = ?, capacity = ? WHERE id = ? """, ( form_data["title"], form_data["start_date"], form_data["end_date"], form_data["capacity"], training_program_id, ), ) return redirect( reverse("hrapp:training_program_details", kwargs={'training_program_id': training_program_id}))