Code Duplication    Length = 64-65 lines in 2 locations

myems-api/core/command.py 1 location

@@ 667-731 (lines=65) @@
664
        resp.location = '/commands/' + str(new_id)
665
666
667
class CommandClone:
668
    def __init__(self):
669
        """"Initializes CommandItem"""
670
        pass
671
672
    @staticmethod
673
    def on_options(req, resp, id_):
674
        _=req
675
        resp.status = falcon.HTTP_200
676
        _=id_
677
    @staticmethod
678
    @user_logger
679
    def on_post(req, resp, id_):
680
        if 'API-KEY' not in req.headers or \
681
                not isinstance(req.headers['API-KEY'], str) or \
682
                len(str.strip(req.headers['API-KEY'])) == 0:
683
            access_control(req)
684
        else:
685
            api_key_control(req)
686
        if not id_.isdigit() or int(id_) <= 0:
687
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
688
                                   description='API.INVALID_COMMAND_ID')
689
690
        cnx = mysql.connector.connect(**config.myems_system_db)
691
        cursor = cnx.cursor()
692
693
        query = (" SELECT id, name, uuid, topic, payload, set_value, description "
694
                 " FROM tbl_commands "
695
                 " WHERE id = %s ")
696
        cursor.execute(query, (id_,))
697
        row = cursor.fetchone()
698
699
        if row is None:
700
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
701
                                   description='API.COMMAND_NOT_FOUND')
702
703
        result = {"id": row[0],
704
                  "name": row[1],
705
                  "uuid": row[2],
706
                  "topic": row[3],
707
                  "payload": row[4],
708
                  "set_value": row[5],
709
                  "description": row[6]}
710
        add_row = (" INSERT INTO tbl_commands "
711
                   "     (name, uuid, topic, payload, set_value, description) "
712
                   " VALUES (%s, %s, %s, %s, %s, %s) ")
713
714
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
715
        if config.utc_offset[0] == '-':
716
            timezone_offset = -timezone_offset
717
        new_name = (str.strip(result['name']) +
718
                    (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds'))
719
        cursor.execute(add_row, (new_name,
720
                                 str(uuid.uuid4()),
721
                                 result['topic'],
722
                                 result['payload'],
723
                                 result['set_value'],
724
                                 result['description']))
725
        new_id = cursor.lastrowid
726
        cnx.commit()
727
        cursor.close()
728
        cnx.close()
729
730
        resp.status = falcon.HTTP_201
731
        resp.location = '/commands/' + str(new_id)
732
733

myems-api/core/virtualpowerplant.py 1 location

@@ 815-878 (lines=64) @@
812
        resp.location = '/virtualpowerplants/' + str(new_id)
813
814
815
class VirtualPowerPlantClone:
816
    def __init__(self):
817
        """"Initializes VirtualPowerPlantExport"""
818
        pass
819
820
    @staticmethod
821
    def on_options(req, resp, id_):
822
        resp.status = falcon.HTTP_200
823
824
    @staticmethod
825
    @user_logger
826
    def on_post(req, resp, id_):
827
        if 'API-KEY' not in req.headers or \
828
                not isinstance(req.headers['API-KEY'], str) or \
829
                len(str.strip(req.headers['API-KEY'])) == 0:
830
            access_control(req)
831
        else:
832
            api_key_control(req)
833
        if not id_.isdigit() or int(id_) <= 0:
834
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
835
                                   description='API.INVALID_VIRTUAL_POWER_PLANT_ID')
836
837
        cnx = mysql.connector.connect(**config.myems_system_db)
838
        cursor = cnx.cursor()
839
840
        query = (" SELECT id, name, uuid, "
841
                 "        cost_center_id, balancing_price_point_id, svg_id, description "
842
                 " FROM tbl_virtual_power_plants "
843
                 " WHERE id = %s ")
844
        cursor.execute(query, (id_,))
845
        row = cursor.fetchone()
846
847
        if row is None:
848
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
849
                                   description='API.VIRTUAL_POWER_PLANT_NOT_FOUND')
850
        else:
851
            meta_result = {"id": row[0],
852
                           "name": row[1],
853
                           "uuid": row[2],
854
                           "cost_center_id": row[3],
855
                           "balancing_price_point_id": row[4],
856
                           "svg_id": row[5],
857
                           "description": row[6]}
858
            timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
859
            if config.utc_offset[0] == '-':
860
                timezone_offset = -timezone_offset
861
            new_name = (str.strip(meta_result['name']) +
862
                        (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds'))
863
            add_values = (" INSERT INTO tbl_virtual_power_plants "
864
                          "    (name, uuid, cost_center_id, balancing_price_point_id, svg_id, description) "
865
                          " VALUES (%s, %s, %s, %s, %s, %s) ")
866
            cursor.execute(add_values, (new_name,
867
                                        str(uuid.uuid4()),
868
                                        meta_result['cost_center_id'],
869
                                        meta_result['balancing_price_point_id'],
870
                                        meta_result['svg_id'],
871
                                        meta_result['description']))
872
            new_id = cursor.lastrowid
873
            cnx.commit()
874
            cursor.close()
875
            cnx.close()
876
877
            resp.status = falcon.HTTP_201
878
            resp.location = '/virtualpowerplants/' + str(new_id)
879
880