Code Duplication    Length = 53-57 lines in 2 locations

myems-api/core/workingcalendar.py 1 location

@@ 559-615 (lines=57) @@
556
        resp.status = falcon.HTTP_200
557
558
559
class WorkingCalendarExport:
560
    def __init__(self):
561
        pass
562
563
    @staticmethod
564
    def on_options(req, resp, id_):
565
        _ = req
566
        resp.status = falcon.HTTP_200
567
        _ = id_
568
569
    @staticmethod
570
    def on_get(req, resp, id_):
571
        if 'API-KEY' not in req.headers or \
572
                not isinstance(req.headers['API-KEY'], str) or \
573
                len(str.strip(req.headers['API-KEY'])) == 0:
574
            access_control(req)
575
        else:
576
            api_key_control(req)
577
        if not id_.isdigit() or int(id_) <= 0:
578
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
579
                                   description='API.INVALID_WORKING_CALENDAR_ID')
580
581
        cnx = mysql.connector.connect(**config.myems_system_db)
582
        cursor = cnx.cursor()
583
584
        cursor.execute(" SELECT id, name, description"
585
                       " FROM tbl_working_calendars "
586
                       " WHERE id = %s ", (id_,))
587
        row = cursor.fetchone()
588
589
        if row is None:
590
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
591
                                   description='API.WORKING_CALENDAR_NOT_FOUND')
592
593
        meta_result = {"id": row[0],
594
                       "name": row[1],
595
                       "description": row[2],
596
                       "non_working_days": None}
597
598
        cursor.execute(" SELECT id, working_calendar_id, date_local, description"
599
                       " FROM tbl_working_calendars_non_working_days "
600
                       " WHERE working_calendar_id = %s ", (id_,))
601
        rows_date_local = cursor.fetchall()
602
603
        result = list()
604
        if rows_date_local is not None and len(rows_date_local) > 0:
605
            for row in rows_date_local:
606
                date_local_dict = {'id': row[0],
607
                                   'working_calendar_id': row[1],
608
                                   'date_local': row[2].isoformat()[0:10],
609
                                   'description': row[3]}
610
                result.append(date_local_dict)
611
        meta_result['non_working_days'] = result
612
        cursor.close()
613
        cnx.close()
614
        resp.text = json.dumps(meta_result)
615
616
617
class WorkingCalendarImport:
618
    def __init__(self):

myems-api/core/distributionsystem.py 1 location

@@ 348-400 (lines=53) @@
345
        resp.status = falcon.HTTP_200
346
347
348
class DistributionSystemDistributionCircuitCollection:
349
    def __init__(self):
350
        pass
351
352
    @staticmethod
353
    def on_options(req, resp, id_):
354
        _ = req
355
        resp.status = falcon.HTTP_200
356
        _ = id_
357
358
    @staticmethod
359
    def on_get(req, resp, id_):
360
        if 'API-KEY' not in req.headers or \
361
                not isinstance(req.headers['API-KEY'], str) or \
362
                len(str.strip(req.headers['API-KEY'])) == 0:
363
            access_control(req)
364
        else:
365
            api_key_control(req)
366
        if not id_.isdigit() or int(id_) <= 0:
367
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
368
                                   description='API.INVALID_DISTRIBUTION_SYSTEM_ID')
369
370
        cnx = mysql.connector.connect(**config.myems_system_db)
371
        cursor = cnx.cursor()
372
373
        cursor.execute(" SELECT name "
374
                       " FROM tbl_distribution_systems "
375
                       " WHERE id = %s ", (id_,))
376
        if cursor.fetchone() is None:
377
            cursor.close()
378
            cnx.close()
379
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
380
                                   description='API.DISTRIBUTION_SYSTEM_NOT_FOUND')
381
382
        query = (" SELECT id, name, uuid, "
383
                 "        distribution_room, switchgear, peak_load, peak_current, customers, meters "
384
                 " FROM tbl_distribution_circuits "
385
                 " WHERE distribution_system_id = %s "
386
                 " ORDER BY name ")
387
        cursor.execute(query, (id_,))
388
        rows = cursor.fetchall()
389
390
        result = list()
391
        if rows is not None and len(rows) > 0:
392
            for row in rows:
393
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2],
394
                               "distribution_room": row[3], "switchgear": row[4],
395
                               "peak_load": row[5], "peak_current": row[6],
396
                               "customers": row[7], "meters": row[8]}
397
                result.append(meta_result)
398
399
        resp.text = json.dumps(result)
400
401
402
class DistributionSystemExport:
403
    def __init__(self):