def delete_survey(request, survey_id): company = is_company(request.user) if request.user.is_authenticated and company is not None: survey = get_object_or_404(Survey, id=survey_id) if survey.company == company: survey.delete() return redirect('survey:surveys_view') return redirect(_404(request, 'Page not found', '404.html'))
def surveys_view(request): company = is_company(request.user) if request.user.is_authenticated and is_company(request.user) is not None: surveys = company.surveys.order_by('-id') context = { 'surveys': surveys, } return render(request, 'surveys/survey_list.html', context) return redirect(_404(request, 'Page not found', '404.html'))
def survey_details_view(request, survey_id): company = is_company(request.user) if request.user.is_authenticated and company is not None: survey = get_object_or_404(Survey, id=survey_id) sheets = AnswerSheet.objects.filter(survey=survey) answered = sheets.exclude(final_answer="unanswered") unanswered = sheets.filter(final_answer="unanswered") context = { 'survey': survey, 'answered': answered, 'unanswered': unanswered, } return render(request, 'surveys/survey_details.html', context) return redirect(_404(request, 'Page not found', '404.html'))
def create_survey(request): user = request.user company = is_company(user) if request.user.is_authenticated and company is not None: if request.method == 'POST': responsible = request.POST["responsible"] main_activities = request.POST["main_activities"] responsible_id = request.POST["responsible_id"] if responsible_id == "": responsible_id = 0 employees = company.employees.all() company_size = employees.count() filenames = [] if company_size > 50: filenames.append('./nom035/static/surveys/guide_III.txt') elif company_size > 15: filenames.append('./nom035/static/surveys/guide_II.txt') filenames.append('./nom035/static/surveys/guide_I.txt') for filename in filenames: survey = None section = None with open(filename, encoding='utf-8') as survey_guide: for line in survey_guide.readlines()[1:]: data = line.split('|') if data[0].isdigit(): survey = Survey.objects.create( company=company, main_activities=main_activities, guide_number=data[0], responsible=responsible, responsible_id=responsible_id, title=data[2]) elif data[0] == "Section": section = Section.objects.create( survey=survey, content=data[2], section_type=data[1]) else: Question.objects.create(section=section, number=data[1], content=data[2], inverted=data[3]) link_survey(survey, employees) return redirect('survey:surveys_view') else: return redirect('survey:surveys_view') else: return redirect(_404(request, 'Page not found', '404.html'))
def load_survey_data(request, survey_id): company = is_company(request.user) if request.user.is_authenticated and company is not None: survey = get_object_or_404(Survey, id=survey_id) if survey.company == company: survey = [{ "responsible": survey.responsible, "responsible_id": survey.responsible_id, "conclusions": survey.conclusions, "method": survey.method, "objective": survey.objective, "recommendations": survey.recommendations, "main_activities": survey.main_activities }] return JsonResponse(survey, content_type='application/json', safe=False) return redirect(_404(request, 'Page not found', '404.html')) return redirect('index_view')
def assign_surveys(request): company = is_company(request.user) if request.user.is_authenticated and company is not None: if request.method == 'POST': surveys = company.surveys.order_by('-id')[:2] for survey in surveys: employees = company.employees.all() for employee in employees: if not employee.answer_sheets.filter( survey=survey).exists(): info_log = employee.information_logs.last() AnswerSheet.objects.create(survey=survey, employee=employee, information_log=info_log) messages.success( request, "Los empleados faltantes se han asignado a las encuestas activas." ) return redirect('survey:surveys_view') return redirect(_404(request, 'Page not found', '404.html'))
def update_survey(request, survey_id): company = is_company(request.user) if request.user.is_authenticated and company is not None: if request.method == 'POST': survey = get_object_or_404(Survey, id=survey_id) if survey.company == company: responsible = request.POST["responsible"] responsible_id = request.POST["responsible_id"] main_activities = request.POST["main_activities"] conclusions = request.POST.get('conclusions', "") method = request.POST.get('method', "") objective = request.POST.get('objective', "") recommendations = request.POST.get('recommendations', "") survey.responsible = responsible survey.responsible_id = responsible_id survey.main_activities = main_activities survey.conclusions = conclusions survey.method = method survey.objective = objective survey.recommendations = recommendations survey.save() return redirect('survey:surveys_view') return redirect(_404(request, 'Page not found', '404.html')) return redirect('index_view')