Example #1
0
def manipulate_plan(conn, cursor, fmr, plan_fmt):

    if plan_fmt['plan_name'] != fmr.plan_name:
        if is_duplicate_plan(mysql, fmr.plan_name):
            return short_error(err_list=['This plan is already exists'])

    if plan_fmt['is_active']:
        return short_error(err_list=['This plan is currently active'])

    if is_expired_plan(mysql, fmr.plan_name):
        return short_error(err_list=['This plan is expired and is read-only.'])

    p = Plan(cursor, conn=conn)
    p.update(fmr, plan_fmt['id'])

    q_del = """DELETE FROM employee_plan WHERE ep_employee_FK = %s AND ep_plan_FK = %s"""
    q_ins = """INSERT INTO employee_plan(ep_employee_FK, ep_plan_FK) VALUES (%s, %s)"""
    uq = get_unique_employees(plan_fmt['employees_list'], fmr.employees_list)

    if uq['operation'] == OperationType.DELETE_EMPLOYEE:
        [
            cursor.execute(q_del, (record['id'], plan_fmt['id']))
            for record in uq['diff_list']
        ]
    else:
        [
            cursor.execute(q_ins, (record['id'], plan_fmt['id']))
            for record in uq['diff_list']
        ]

    conn.commit()
    conn.close()
    return short_success(ManipulationType.UPDATED)
Example #2
0
 def update(self):
     try:
         get_plan = Plan.get(name=self.name)
         plan = Plan.update(
             name=self.name, price=self.price,
             quantity=self.quantity).where(Plan.name == get_plan.name)
         plan.execute()
         return {"message": "Plan updated successfully"}
     except Plan.DoesNotExist:
         if Plan.DoesNotExist:
             return {"message": "Plan does not exist"}