Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 1 | |
| 2 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 3 | # License: GNU General Public License v3. See license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | |
| 7 | import frappe |
| 8 | import random |
| 9 | from frappe.utils import cstr |
| 10 | from frappe.utils.make_random import get_random |
| 11 | from datetime import timedelta |
| 12 | from erpnext.education.api import get_student_group_students, make_attendance_records, enroll_student, \ |
| 13 | get_fee_schedule, collect_fees, get_course |
| 14 | |
| 15 | |
| 16 | def work(): |
| 17 | frappe.set_user(frappe.db.get_global('demo_education_user')) |
| 18 | for d in xrange(20): |
| 19 | approve_random_student_applicant() |
| 20 | enroll_random_student(frappe.flags.current_date) |
| 21 | # if frappe.flags.current_date.weekday()== 0: |
| 22 | # make_course_schedule(frappe.flags.current_date, frappe.utils.add_days(frappe.flags.current_date, 5)) |
| 23 | mark_student_attendance(frappe.flags.current_date) |
| 24 | # make_assessment_plan() |
| 25 | make_fees() |
| 26 | |
| 27 | def approve_random_student_applicant(): |
| 28 | random_student = get_random("Student Applicant", {"application_status": "Applied"}) |
| 29 | if random_student: |
| 30 | status = ["Approved", "Rejected"] |
| 31 | frappe.db.set_value("Student Applicant", random_student, "application_status", status[weighted_choice([9,3])]) |
| 32 | |
| 33 | def enroll_random_student(current_date): |
| 34 | batch = ["Section-A", "Section-B"] |
| 35 | random_student = get_random("Student Applicant", {"application_status": "Approved"}) |
| 36 | if random_student: |
| 37 | enrollment = enroll_student(random_student) |
| 38 | enrollment.academic_year = get_random("Academic Year") |
| 39 | enrollment.enrollment_date = current_date |
| 40 | enrollment.student_batch_name = batch[weighted_choice([9,3])] |
| 41 | fee_schedule = get_fee_schedule(enrollment.program) |
| 42 | for fee in fee_schedule: |
| 43 | enrollment.append("fees", fee) |
| 44 | enrolled_courses = get_course(enrollment.program) |
| 45 | for course in enrolled_courses: |
| 46 | enrollment.append("courses", course) |
| 47 | enrollment.submit() |
| 48 | frappe.db.commit() |
| 49 | assign_student_group(enrollment.student, enrollment.student_name, enrollment.program, |
| 50 | enrolled_courses, enrollment.student_batch_name) |
| 51 | |
| 52 | def assign_student_group(student, student_name, program, courses, batch): |
| 53 | course_list = [d["course"] for d in courses] |
| 54 | for d in frappe.get_list("Student Group", fields=("name"), filters={"program": program, "course":("in", course_list)}): |
| 55 | student_group = frappe.get_doc("Student Group", d.name) |
| 56 | student_group.append("students", {"student": student, "student_name": student_name, |
| 57 | "group_roll_number":len(student_group.students)+1, "active":1}) |
| 58 | student_group.save() |
| 59 | student_batch = frappe.get_list("Student Group", fields=("name"), filters={"program": program, "group_based_on":"Batch", "batch":batch})[0] |
| 60 | student_batch_doc = frappe.get_doc("Student Group", student_batch.name) |
| 61 | student_batch_doc.append("students", {"student": student, "student_name": student_name, |
| 62 | "group_roll_number":len(student_batch_doc.students)+1, "active":1}) |
| 63 | student_batch_doc.save() |
| 64 | frappe.db.commit() |
| 65 | |
| 66 | def mark_student_attendance(current_date): |
| 67 | status = ["Present", "Absent"] |
| 68 | for d in frappe.db.get_list("Student Group", filters={"group_based_on": "Batch"}): |
| 69 | students = get_student_group_students(d.name) |
| 70 | for stud in students: |
| 71 | make_attendance_records(stud.student, stud.student_name, status[weighted_choice([9,4])], None, d.name, current_date) |
| 72 | |
| 73 | def make_fees(): |
| 74 | for d in range(1,10): |
| 75 | random_fee = get_random("Fees", {"paid_amount": 0}) |
| 76 | collect_fees(random_fee, frappe.db.get_value("Fees", random_fee, "outstanding_amount")) |
| 77 | |
| 78 | def make_assessment_plan(date): |
| 79 | for d in range(1,4): |
| 80 | random_group = get_random("Student Group", {"group_based_on": "Course"}, True) |
| 81 | doc = frappe.new_doc("Assessment Plan") |
| 82 | doc.student_group = random_group.name |
| 83 | doc.course = random_group.course |
| 84 | doc.assessment_group = get_random("Assessment Group", {"is_group": 0, "parent": "2017-18 (Semester 2)"}) |
| 85 | doc.grading_scale = get_random("Grading Scale") |
| 86 | doc.maximum_assessment_score = 100 |
| 87 | |
| 88 | def make_course_schedule(start_date, end_date): |
| 89 | for d in frappe.db.get_list("Student Group"): |
| 90 | cs = frappe.new_doc("Scheduling Tool") |
| 91 | cs.student_group = d.name |
| 92 | cs.room = get_random("Room") |
| 93 | cs.instructor = get_random("Instructor") |
| 94 | cs.course_start_date = cstr(start_date) |
| 95 | cs.course_end_date = cstr(end_date) |
| 96 | day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] |
| 97 | for x in xrange(3): |
| 98 | random_day = random.choice(day) |
| 99 | cs.day = random_day |
| 100 | cs.from_time = timedelta(hours=(random.randrange(7, 17,1))) |
| 101 | cs.to_time = cs.from_time + timedelta(hours=1) |
| 102 | cs.schedule_course() |
| 103 | day.remove(random_day) |
| 104 | |
| 105 | |
| 106 | def weighted_choice(weights): |
| 107 | totals = [] |
| 108 | running_total = 0 |
| 109 | |
| 110 | for w in weights: |
| 111 | running_total += w |
| 112 | totals.append(running_total) |
| 113 | |
| 114 | rnd = random.random() * running_total |
| 115 | for i, total in enumerate(totals): |
| 116 | if rnd < total: |
| 117 | return i |