Code Duplication    Length = 53-57 lines in 2 locations

myems-api/core/workingcalendar.py 1 location

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

myems-api/core/distributionsystem.py 1 location

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