Code Duplication    Length = 53-57 lines in 2 locations

myems-api/core/workingcalendar.py 1 location

@@ 524-580 (lines=57) @@
521
        resp.status = falcon.HTTP_200
522
523
524
class WorkingCalendarExport:
525
    def __init__(self):
526
        """"Initializes WorkingCalendarExport"""
527
        pass
528
529
    @staticmethod
530
    def on_options(req, resp, id_):
531
        _ = req
532
        resp.status = falcon.HTTP_200
533
        _ = id_
534
535
    @staticmethod
536
    def on_get(req, resp, id_):
537
        if 'API-KEY' not in req.headers or \
538
                not isinstance(req.headers['API-KEY'], str) or \
539
                len(str.strip(req.headers['API-KEY'])) == 0:
540
            access_control(req)
541
        else:
542
            api_key_control(req)
543
        if not id_.isdigit() or int(id_) <= 0:
544
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
545
                                   description='API.INVALID_WORKING_CALENDAR_ID')
546
547
        cnx = mysql.connector.connect(**config.myems_system_db)
548
        cursor = cnx.cursor()
549
550
        cursor.execute(" SELECT id, name, description"
551
                       " FROM tbl_working_calendars "
552
                       " WHERE id = %s ", (id_,))
553
        row = cursor.fetchone()
554
555
        if row is None:
556
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
557
                                   description='API.WORKING_CALENDAR_NOT_FOUND')
558
559
        meta_result = {"id": row[0],
560
                       "name": row[1],
561
                       "description": row[2],
562
                       "non_working_days": None}
563
564
        cursor.execute(" SELECT id, working_calendar_id, date_local, description"
565
                       " FROM tbl_working_calendars_non_working_days "
566
                       " WHERE working_calendar_id = %s ", (id_,))
567
        rows_date_local = cursor.fetchall()
568
569
        result = list()
570
        if rows_date_local is not None and len(rows_date_local) > 0:
571
            for row in rows_date_local:
572
                date_local_dict = {'id': row[0],
573
                                   'working_calendar_id': row[1],
574
                                   'date_local': row[2].isoformat()[0:10],
575
                                   'description': row[3]}
576
                result.append(date_local_dict)
577
        meta_result['non_working_days'] = result
578
        cursor.close()
579
        cnx.close()
580
        resp.text = json.dumps(meta_result)
581
582
583
class WorkingCalendarImport:

myems-api/core/distributionsystem.py 1 location

@@ 319-371 (lines=53) @@
316
        resp.status = falcon.HTTP_200
317
318
319
class DistributionSystemDistributionCircuitCollection:
320
    def __init__(self):
321
        """Initializes DistributionSystemDistributionCircuitCollection"""
322
        pass
323
324
    @staticmethod
325
    def on_options(req, resp, id_):
326
        _ = req
327
        resp.status = falcon.HTTP_200
328
        _ = id_
329
330
    @staticmethod
331
    def on_get(req, resp, id_):
332
        if 'API-KEY' not in req.headers or \
333
                not isinstance(req.headers['API-KEY'], str) or \
334
                len(str.strip(req.headers['API-KEY'])) == 0:
335
            access_control(req)
336
        else:
337
            api_key_control(req)
338
        if not id_.isdigit() or int(id_) <= 0:
339
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
340
                                   description='API.INVALID_DISTRIBUTION_SYSTEM_ID')
341
342
        cnx = mysql.connector.connect(**config.myems_system_db)
343
        cursor = cnx.cursor()
344
345
        cursor.execute(" SELECT name "
346
                       " FROM tbl_distribution_systems "
347
                       " WHERE id = %s ", (id_,))
348
        if cursor.fetchone() is None:
349
            cursor.close()
350
            cnx.close()
351
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
352
                                   description='API.DISTRIBUTION_SYSTEM_NOT_FOUND')
353
354
        query = (" SELECT id, name, uuid, "
355
                 "        distribution_room, switchgear, peak_load, peak_current, customers, meters "
356
                 " FROM tbl_distribution_circuits "
357
                 " WHERE distribution_system_id = %s "
358
                 " ORDER BY name ")
359
        cursor.execute(query, (id_,))
360
        rows = cursor.fetchall()
361
362
        result = list()
363
        if rows is not None and len(rows) > 0:
364
            for row in rows:
365
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2],
366
                               "distribution_room": row[3], "switchgear": row[4],
367
                               "peak_load": row[5], "peak_current": row[6],
368
                               "customers": row[7], "meters": row[8]}
369
                result.append(meta_result)
370
371
        resp.text = json.dumps(result)
372
373
374
class DistributionSystemExport: