Code Duplication    Length = 61-65 lines in 2 locations

myems-api/core/virtualpowerplant.py 1 location

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

myems-api/core/energystoragecontainer.py 1 location

@@ 5841-5905 (lines=65) @@
5838
        resp.status = falcon.HTTP_204
5839
5840
5841
class EnergyStorageContainerClone:
5842
    def __init__(self):
5843
        """Initializes Class"""
5844
        pass
5845
5846
    @staticmethod
5847
    def on_options(req, resp, id_):
5848
        _ = req
5849
        resp.status = falcon.HTTP_200
5850
        _ = id_
5851
5852
    @staticmethod
5853
    @user_logger
5854
    def on_post(req, resp, id_):
5855
        """Handles POST requests"""
5856
        admin_control(req)
5857
        if not id_.isdigit() or int(id_) <= 0:
5858
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5859
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5860
5861
        cnx = mysql.connector.connect(**config.myems_system_db)
5862
        cursor = cnx.cursor()
5863
5864
        query = (" SELECT id, name, uuid, "
5865
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
5866
                 " FROM tbl_energy_storage_containers "
5867
                 " WHERE id = %s ")
5868
        cursor.execute(query, (id_,))
5869
        row = cursor.fetchone()
5870
5871
        if row is None:
5872
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5873
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5874
        else:
5875
            meta_result = {"id": row[0],
5876
                           "name": row[1],
5877
                           "uuid": row[2],
5878
                           "rated_capacity": row[3],
5879
                           "rated_power": row[4],
5880
                           "contact_id": row[5],
5881
                           "cost_center_id": row[6],
5882
                           "description": row[7]}
5883
            timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
5884
            if config.utc_offset[0] == '-':
5885
                timezone_offset = -timezone_offset
5886
            new_name = str.strip(meta_result['name']) + \
5887
                (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')
5888
            add_values = (" INSERT INTO tbl_energy_storage_containers "
5889
                          "    (name, uuid, rated_capacity, rated_power, contact_id, "
5890
                          "     cost_center_id, description) "
5891
                          " VALUES (%s, %s, %s, %s, %s, %s, %s) ")
5892
            cursor.execute(add_values, (new_name,
5893
                                        str(uuid.uuid4()),
5894
                                        meta_result['rated_capacity'],
5895
                                        meta_result['rated_power'],
5896
                                        meta_result['contact_id'],
5897
                                        meta_result['cost_center_id'],
5898
                                        meta_result['description']))
5899
            new_id = cursor.lastrowid
5900
            cnx.commit()
5901
            cursor.close()
5902
            cnx.close()
5903
5904
            resp.status = falcon.HTTP_201
5905
            resp.location = '/energystoragecontainers/' + str(new_id)
5906
5907
5908
class EnergyStorageContainerExport: