Code Duplication    Length = 61-65 lines in 2 locations

myems-api/core/virtualpowerplant.py 1 location

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

myems-api/core/energystoragecontainer.py 1 location

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