def participant_form(request): response = {'data': None, 'error': None, 'statusCode': 1} try: configureLogging() if request.method == "POST": data = json.loads(request.body) paper_detatils = Paper.objects.get(paper_id=data['paper_id']) Participant(first_name=data['first_name'], last_name=data['last_name'], registration_date=datetime.datetime.now(), company_id=paper_detatils.company_id, paper_id=data['paper_id'], participant_email=data['email'], participant_phone=data['mobile_no'], participant_city=data['City'], participant_state=data['state'], participant_country=data['country']).save() response['statusCode'] = 2 calender = Calender.objects.get(paper_id=data['paper_id']) Paper_Instance(paper_id=data['paper_id'], participant_id=Participant.objects.latest('id').id, calender_id=calender.id).save() sendEmail_to_participant(Participant.objects.latest('id').id) return JsonResponse(response) except Exception as e: logging.error(str(e))
def examinar_dashboard(request): response = { 'participants': None, 'total_papers': None, 'papers': None, 'error': None, 'statusCode': 1 } try: configureLogging() companies_list = [] companies = Company.objects.filter(company_status='A') for company in companies: companies_list.append(company.id) papers = Paper.objects.filter(company_id__in=companies_list) students = Participant.objects.all() papers_obj = [] for paper in papers: calender_details = Calender.objects.filter(paper_id=paper.paper_id) for calender_detail in calender_details: if calender_detail.schedule_time > datetime.datetime.now(): temp = { 'paper_id': paper.paper_id, 'paper_name': paper.paper_name, } papers_obj.append(temp) response['total_papers'] = len(papers) response['papers'] = papers_obj response['participants'] = len(students) response['statusCode'] = 0 return JsonResponse(response) except Exception as e: logging.error(str(e))
def exam_answers(request): response = {'data': None, 'error': None, 'statusCode': 1} try: configureLogging() if request.method == "POST": data = json.loads(request.body) paper_details = Paper.objects.get(paper_name=data['paper_name']) paper_answer, paper_answer_stat = Paper_answer.objects.get_or_create( answer_paper_instance_id=data['paper_instance_id'], answer_paper_id=paper_details.paper_id, answer_participant_id=data['participant_id'], answer_question_id=data['question_id']) paper_answer.answer_subject_id = paper_details.paper_subject if 'ans' in data: paper_answer.answer_question_response = data['ans'] paper_answer.answer_date = datetime.datetime.now() else: paper_answer.answer_question_response = "null" paper_answer.answer_date = datetime.datetime.now() paper_answer.save() return JsonResponse(response) except Exception as e: logging.error(str(e)) response['data'] = 'Error in saving paper answers' response['error'] = str(e) return JsonResponse(response)
def exam_code_verification_view(request): response = {'data': None, 'error': None, 'statusCode': 1} try: configureLogging() if request.method == "GET": exam_codes = Paper_Instance.objects.filter( participant_key__isnull=False) exam_code_obj = [] for exam_code in exam_codes: calender_details = Calender.objects.get( id=exam_code.calender_id) if ((calender_details.schedule_time - datetime.timedelta( minutes=6)) <= datetime.datetime.now() <= calender_details.schedule_time) or \ (calender_details.schedule_time < datetime.datetime.now() < ( calender_details.schedule_time + datetime.timedelta( minutes=calender_details.duration))): exam_code_obj.append(exam_code.participant_key) response['data'] = exam_code_obj response['statusCode'] = 0 return JsonResponse(response) except Exception as e: logging.error(str(e))
def oneExamView(request): response = { 'participant': None, 'paper_name': None, 'paper_instance_id': None, 'Instructions': None, 'questions': None, 'section': None, 'company_id': None, 'given_answers': None, 'error': None, 'statusCode': 1 } try: configureLogging() if request.method == "POST": global participant_key, instrctions_obj data = json.loads(request.body) if data != {}: section = [] participant_key = data['participant_key'] response['data'] = [{"response": data['participant_key']}] participant_details = Paper_Instance.objects.get( participant_key=participant_key) paper_id = participant_details.paper_id participant_id = participant_details.participant_id paper = Paper.objects.get(paper_id=paper_id) question_details = Paper_Question.objects.filter( paper_id=paper_id).order_by('question_order') questions_obj = [] for question_detail in question_details: question_db = Question.objects.filter( question_status='A', id=question_detail.question_id) for question in question_db: if question.question_type == 'C': options = Option.objects.filter( question_id=question.id).order_by( 'option_order') temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "opt1": options[0].option_name, "opt2": options[1].option_name, "opt3": options[2].option_name, "opt4": options[3].option_name, "opt5": options[4].option_name, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'I': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'P': media = Media.objects.get( id=question.question_image) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'A': media = Media.objects.get( id=question.question_audio) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'R': temp = { "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) paper_sections = Paper_section.objects.filter( section_paper_id=paper_id) for instruction in paper_sections: instrctions_obj = instruction.section_instructions.split( "||") section.append(instruction.section_name) response['participant'] = participant_id response['questions'] = questions_obj response['paper_name'] = paper.paper_name response['Instructions'] = instrctions_obj response['section'] = section response['company_id'] = paper.company_id response['paper_instance_id'] = participant_details.id return JsonResponse(response) else: section = [] participant_details = Paper_Instance.objects.get( participant_key=participant_key) paper_id = participant_details.paper_id participant_id = participant_details.participant_id q = [] paper_answers = Paper_answer.objects.filter( answer_participant_id=participant_id, answer_paper_instance_id=participant_details.id) answered = [] answers = [] for paper_answer in paper_answers: answered.append(paper_answer.answer_question_id) answers.append({ "quest_id": paper_answer.answer_question_id, "quest_given_ans": paper_answer.answer_question_response }) question_detai = Paper_Question.objects.filter( Q(paper_id=paper_id)).order_by('question_order') q = list(question_detai) ques = Paper_Question.objects.filter(Q(paper_id=paper_id)) questions_obj = [] paper_answers = Paper_answer.objects.filter( answer_participant_id=participant_id, answer_paper_instance_id=participant_details.id) if paper_answers: for question_detail in ques: question_db = Question.objects.filter( question_status='A', id=question_detail.question_id) for question in question_db: if question.question_type == 'C': options = Option.objects.filter( question_id=question.id).order_by( 'option_order') temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "opt1": options[0].option_name, "opt2": options[1].option_name, "opt3": options[2].option_name, "opt4": options[3].option_name, "opt5": options[4].option_name, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'I': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'P': media = Media.objects.get( id=question.question_image) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'A': media = Media.objects.get( id=question.question_audio) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'R': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) else: for question_detail in ques: question_db = Question.objects.filter( question_status='A', id=question_detail.question_id) # question_db = Question.objects.filter(question_status='A', id=question_detail.question_id) for question in question_db: if question.question_type == 'C': options = Option.objects.filter( question_id=question.id).order_by( 'option_order') temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "opt1": options[0].option_name, "opt2": options[1].option_name, "opt3": options[2].option_name, "opt4": options[3].option_name, "opt5": options[4].option_name, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'I': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'P': media = Media.objects.get( id=question.question_image) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'A': media = Media.objects.get( id=question.question_audio) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url, "q_time": question_detail.question_time } questions_obj.append(temp) if question.question_type == 'R': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "q_time": question_detail.question_time } questions_obj.append(temp) response['questions'] = questions_obj participant_id = participant_details.participant_id paper = Paper.objects.get(paper_id=paper_id) paper_sections = Paper_section.objects.filter( section_paper_id=paper_id) for instruction in paper_sections: instrctions_obj = instruction.section_instructions.split( "||") section.append(instruction.section_name) response['participant'] = participant_id response['paper_name'] = paper.paper_name response['paper_instance_id'] = participant_details.id response['Instructions'] = instrctions_obj response['section'] = section response['company_id'] = paper.company_id response['given_answers'] = answers participant_key = "" print(response, "respones") return JsonResponse(response) if request.method == "GET": instrc_db = Paper_section.objects.all() instrctions_obj = [] for instruction in instrc_db: temp = { "sect_paper_id": instruction.section_paper_id, "sect_name": instruction.section_name, "sect_ins": instruction.section_instructions, "sect_marks": instruction.section_marks, } instrctions_obj.append(temp) response['data'] = instrctions_obj response['statusCode'] = 0 return JsonResponse(response) except Exception as e: logging.error('Error in retrieving Exam paper details: ' + str(e)) response['error'] = 'Error in retrieving Exam paper details' + str(e) return JsonResponse(response)
def questionsView(request): response = {'data': None, 'error': None, 'statusCode': 1} try: configureLogging() if request.method == "GET": # paper_id = 1 question_details = Paper_Question.objects.all().order_by( 'question_order') questions_obj = [] for question_detail in question_details: question_db = Question.objects.filter( question_status='A', id=question_detail.question_id) for question in question_db: if question.question_type == 'C': options = Option.objects.filter( question_id=question.id).order_by('option_order') temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "opt1": options[0].option_name, "opt2": options[1].option_name, "opt3": options[2].option_name, "opt4": options[3].option_name, "opt5": options[4].option_name } questions_obj.append(temp) if question.question_type == 'I': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, } questions_obj.append(temp) if question.question_type == 'P': media = Media.objects.get(id=question.question_image) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url } questions_obj.append(temp) if question.question_type == 'A': media = Media.objects.get(id=question.question_audio) temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, "mediasource": media.mediasource, "media_url": media.media_url } questions_obj.append(temp) if question.question_type == 'R': temp = { "q_id": question.id, "q_name": question.question, "q_type": question.question_type, } questions_obj.append(temp) response['data'] = questions_obj response['statusCode'] = 0 return JsonResponse(response) except Exception as e: logging.error(str(e)) response['data'] = 'Error in retrieving questions' response['error'] = str(e)
def sendEmail_to_participant(participant_id): try: configureLogging() CRLF = "\r\n" paper_instance = Paper_Instance.objects.get( participant_id=participant_id) # for paper_instance in paper_instances: calender_details = Calender.objects.get(id=paper_instance.calender_id) if calender_details.schedule_time > datetime.datetime.now(): participant = Participant.objects.get( id=paper_instance.participant_id) email_template = Email_template.objects.get( company_id=participant.company_id) sender_email = email_config['sender'] receiver_email = participant.participant_email message = MIMEMultipart("mixed") message["Subject"] = email_template.email_subject message["From"] = f"OneExam <{sender_email}>" message["To"] = receiver_email exam_code = ''.join( random.choices(string.ascii_letters + string.digits, k=email_config.getint('code_length'))) paper = Paper.objects.get(paper_id=participant.paper_id) emailbody = MIMEText( email_template.email_body.replace( "{name}", participant.first_name).replace( "{paper}", paper.paper_name).replace( "{date}", calender_details.schedule_time.strftime( "%A %d %B %Y")).replace( "{time}", calender_details.schedule_time.strftime( " %I:%M %p ")).replace( "{link}", email_config['url']).replace( "{code}", str(exam_code)).replace( "{n}", "\n")) message.attach(emailbody) examdtstart = calender_details.schedule_time dur = datetime.timedelta(minutes=calender_details.duration) examdtend = examdtstart + dur dtstamp = datetime.datetime.now().strftime("%Y%m%dT%H%M%S") dtstart = examdtstart.strftime("%Y%m%dT%H%M%S") dtend = examdtend.strftime("%Y%m%dT%H%M%S") description = "DESCRIPTION: exam invitation" + CRLF organizer = "ORGANIZER;CN=OneExam:mailto:first" + CRLF + " @gmail.com" attendee = "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ- PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE" + CRLF + " ;CN=" + receiver_email + " mailto:" + receiver_email + CRLF ical = "BEGIN:VCALENDAR" + CRLF + "PRODID:pyICSParser" + CRLF + "VERSION:2.0" + CRLF + "CALSCALE:GREGORIAN" + CRLF ical += "METHOD:REQUEST" + CRLF + "BEGIN:VEVENT" + CRLF + "DTSTART:" + dtstart + CRLF + "DTEND:" + dtend + CRLF + "DTSTAMP:" + dtstamp + CRLF + organizer + CRLF ical += "UID:FIXMEUID" + dtstamp + CRLF ical += attendee + "CREATED:" + dtstamp + CRLF + description + "LAST-MODIFIED:" + dtstamp + CRLF + "LOCATION:" + CRLF + "SEQUENCE:0" + CRLF + "STATUS:CONFIRMED" + CRLF ical += "SUMMARY:Exam invitation " + CRLF + "TRANSP:OPAQUE" + CRLF + "END:VEVENT" + CRLF + "END:VCALENDAR" + CRLF eml_body = "\n.ics file will be visible in the invite of outlook and outlook.com but not google calendar" eml_body_bin = "This is the email body in binary - two steps" # message = MIMEMultipart('mixed') part_email = MIMEText(eml_body) message.attach(part_email) part_cal = MIMEText(ical, 'calendar;method=REQUEST') msgAlternative = MIMEMultipart('alternative') message.attach(msgAlternative) ical_atch = MIMEText('application/ics', ' ;name="%s"' % ("Exam invitation.ics")) ical_atch.set_payload(ical) encoders.encode_base64(ical_atch) ical_atch.add_header('Content-Disposition', 'attachment; filename="%s"' % ("invite.ics")) eml_atch = MIMEText('', 'plain') encoders.encode_base64(eml_atch) eml_atch.add_header('Content-Transfer-Encoding', "") # msgAlternative.attach(part_email) msgAlternative.attach(part_cal) context = ssl.create_default_context() with smtplib.SMTP_SSL(email_config['host'], email_config.getint('port'), context=context) as server: server.login(sender_email, email_config['password']) server.sendmail(sender_email, receiver_email, message.as_string()) # paper_instance,paper_instance_stat=Paper_Instance.objects.get_or_create(participant_id=participant.id) paper_instance.participant_key = exam_code paper_instance.paper_instance_date = datetime.datetime.now() paper_instance.save() logging.info(f"Email sent to mail id:{receiver_email}") except Exception as e: logging.error(str(e)) raise