This Python code defines a RESTful API using the FastAPI module. The API handles various endpoints for managing organizations, variables, users, zones, and measurements. It also utilizes a RequestMediator
to handle requests and interact with the underlying data storage.
- Route:
/
- Method: GET
- Function:
read_root
- Description: Returns a simple JSON response indicating that the API is connected.
- Route:
/measurements/{zone_id}/{variable_id}
- Method: GET
- Function:
get_measurements_by_zone_and_variable
- Parameters:
zone_id
: ID of the zonevariable_id
: ID of the variable
- Description: Retrieves measurements for a specific zone and variable. Handles errors with appropriate HTTP responses.
- Route:
/organization
- Method: POST
- Function:
create_organization
- Parameters: JSON payload representing an organization
- Description: Creates a new organization and returns a success message along with the organization's ID. Handles errors with appropriate HTTP responses.
- Route:
/variable
- Method: POST
- Function:
create_variable
- Parameters: JSON payload representing a variable
- Description: Creates a new variable and returns a success message along with the variable's ID. Handles errors with appropriate HTTP responses.
- Route:
/register
- Method: POST
- Function:
register
- Parameters: JSON payload representing a user
- Description: Registers a new user and returns a success message along with the user's ID. Handles errors with appropriate HTTP responses.
- Route:
/login
- Method: POST
- Function:
login
- Parameters: JSON payload representing a user
- Description: Logs in a user and returns a success message along with the user's ID. Handles errors with appropriate HTTP responses.
- Route:
/zone
- Method: POST
- Function:
create_zone
- Parameters: JSON payload representing a zone
- Description: Creates a new zone and returns a success message along with the zone's ID. Handles errors with appropriate HTTP responses.
- Route:
/measurement
- Method: POST
- Function:
add_measurement
- Parameters: JSON payload representing a measurement
- Description: Adds a new measurement and returns a success message along with the measurement's ID. Handles errors with appropriate HTTP responses.
Note: The code includes commented-out lines at the end (# if __name__ == '__main__':
) which suggests that it may be intended for use as a standalone script. Uncommenting these lines would allow the API to be run independently.
This Python code defines Pydantic data models for the application, which serves as the basis for request validation and serialization.
- Variable:
app
- Type:
FastAPI
- Description: An instance of the FastAPI class representing the main application.
- Type:
The code includes several Pydantic models that define the structure and validation rules for different data entities:
- Attributes:
username
(str): User's usernameemail
(str): User's email addresspassword
(str): User's password
- Type:
BaseModel
- Description: Represents the structure and validation rules for user-related data.
- Attributes:
org_id
(str): Organization ID to which the zone belongsname
(str): Name of the zonelatitude
(float): Latitude coordinates of the zonelongitude
(float): Longitude coordinates of the zone
- Type:
BaseModel
- Description: Defines the structure and validation rules for zone-related data.
- Attributes:
variable_id
(str): ID of the variable associated with the measurementcrop_id
(str): ID of the crop associated with the measurementdatetime
(str): Date and time of the measurementvalue
(float): Measurement value
- Type:
BaseModel
- Description: Represents the structure and validation rules for measurement-related data.
- Attributes:
name
(str): Name of the organizationdescription
(str): Description of the organizationpassword
(str): Password for the organization
- Type:
BaseModel
- Description: Defines the structure and validation rules for organization-related data.
- Attributes:
name
(str): Name of the variableunits
(str): Measurement units for the variabledescription
(str): Description of the variable
- Type:
BaseModel
- Description: Represents the structure and validation rules for variable-related data.
This Python code defines a RequestMediator
class that acts as an intermediary between a FastAPI application and a PostgreSQL database using the psycopg2
library. The class includes methods for handling various requests related to user creation, variable creation, organization creation, zone creation, and measurement addition.
- psycopg2: A PostgreSQL adapter for Python.
- data_validator: An external module for validating user data.
-
Attributes:
adapter
(DatabaseAdapter): An instance of theDatabaseAdapter
class for interfacing with the PostgreSQL database.data_validator
(DataValidator): An instance of thedata_validator.DataValidator
class for validating user data.
-
Methods:
-
create_user(self, user: str, email: str, password: str) -> Union[Tuple[int, str], bool]
- Creates a new user in the database.
- Validates user data and handles potential exceptions.
-
create_variable(self, name: str, units: str, description: str) -> Union[Tuple[int, str], bool]
- Creates a new variable in the database.
- Handles potential exceptions such as unique violations.
-
create_org(self, name: str, description: str, password: str) -> Union[Tuple[int, str], bool]
- Creates a new organization in the database.
- Validates organization data and handles potential exceptions.
-
create_zone(self, org_id: str, name: str, latitud: float, longitud: float) -> Union[Tuple[int, str], bool]
- Creates a new zone in the database.
- Handles potential exceptions, including unique violations and foreign key violations.
-
add_measurement(self, datetime: str, crop_id: str, value: float, variable_id: str) -> Union[Tuple[int, str], bool]
- Adds a new measurement to the database.
- Handles potential exceptions, including foreign key violations and invalid text representations.
-
login(self, user: str, password: str) -> Union[Tuple[int, str], User]
- Logs in a user and returns user data if successful.
- Validates user data and handles potential exceptions.
-
get_measurements(self, crop_id: str, variable_id: str) -> Union[Tuple[int, str], List[Measurement]]
- Retrieves measurements based on crop and variable IDs.
- Handles potential exceptions, including foreign key violations and invalid text representations.
__init__(self) -> None
- Initializes the
RequestMediator
class. - Prints an initialization message.
- Initializes the
-
- The code includes a section for main execution (
if __name__ == '__main__':
) with commented-out lines. - Demonstrates the usage of the
RequestMediator
class with example operations such as user creation, login, variable creation, organization creation, zone creation, and measurement addition.
This Python code defines a DataValidator
class responsible for validating user input, particularly user registration data such as username, email, and password.
- Description: Validates user registration data, including username, email, and password.
- Parameters:
user
(str): User's usernameemail
(str): User's email addresspassword
(str): User's password
- Returns: Boolean indicating whether the user data is valid.
- Description: Validates the username.
- Parameters:
user
(str): User's username
- Returns: Boolean indicating whether the username is valid.
- Validation Criteria:
- Length between 3 and 12 characters
- Alphanumeric characters, printable characters, or alphabetic characters only
- No spaces
- All characters with ASCII code less than 128
- Description: Validates the email address.
- Parameters:
email
(str): User's email address
- Returns: Boolean indicating whether the email address is valid.
- Validation Criteria:
- Follows the standard email format (local-part@domain)
#DB folder #Database Module
This Python code represents a database module that provides access to database services, adapters, and schema definitions. It includes several key components such as DBService
, DatabaseAdapter
, and various schema models for different entities.
- Description: A class providing services related to database operations.
- Source: Defined in the
db_service
module.
- Description: A class serving as an adapter for interacting with the database.
- Source: Defined in the
db_adapter
module.
- Org: Represents the organization schema.
- Crop: Represents the crop schema.
- Variable: Represents the variable schema.
- Condition: Represents the condition schema.
- ActuatorType: Represents the actuator type schema.
- Actuator: Represents the actuator schema.
- Measurement: Represents the measurement schema.
- User: Represents the user schema.
- Permission: Represents the permission schema.
- Base: Represents the base schema.
Source: Defined in the schema
module.
- Description: A function for creating a database engine with retry capability.
- Source: Defined in the module.
-
__name__ = 'db'
- Specifies the module name as 'db'.
-
__all__ = ['DatabaseAdapter', 'DBService', 'Org', 'Crop', 'Variable', 'Condition', 'ActuatorType', 'Actuator', 'Measurement', 'User', 'Permission', 'Base', 'create_engine_with_retry']
- Specifies the list of symbols exported by the module.
-
Import this module to access database-related classes and schema definitions in other parts of the application.
-
Use the exported symbols for database operations, including interaction with different schema models and creating a database engine.
-
Description: Validates the user password.
-
Parameters:
password
(str): User's password
-
Returns: Boolean indicating whether the password is valid.
-
Validation Criteria:
- Length between 8 and 60 characters
- Alphanumeric characters, alphabetic characters, printable characters, or spaces only
- All characters with ASCII code less than 128
__init__(self)
- Initializes the
DataValidator
class. - No additional setup is performed in the constructor.
- Initializes the
- Call the
validate_user
method with the user's username, email, and password to determine whether the user input is valid. - The class includes print statements for validation results.
This Python code defines a database module containing abstract classes DatabaseClient
and DatabaseAdapter
. These classes serve as an interface and implementation, respectively, for interacting with a database using a DBService
. The module includes methods for creating users, organizations, variables, zones, and measurements, as well as login and retrieving measurements.
- Description: An abstract base class defining the interface for database clients.
- Methods:
create_user(self, user: str, password: str) -> User
: Abstract method for creating a user.create_zone(self, zone: dict)
: Abstract method for creating a zone.add_measurement(self, measurement: dict)
: Abstract method for adding a measurement.
-
Description: A concrete implementation of
DatabaseClient
that interacts with the database using aDBService
. -
Methods:
create_user(self, user: str, email: str, password: str) -> User
: Creates a new user in the database.create_org(self, name: str, description: str, password: str) -> Org
: Creates a new organization in the database.create_variable(self, name: str, units: str, description: str) -> Variable
: Creates a new variable in the database.create_zone(self, org_id: str, name: str, latitud: str, longitud: str) -> Crop
: Creates a new zone in the database.add_measurement(self, datetime: str, crop_id: str, value: float, variable_id: str) -> Measurement
: Adds a new measurement to the database.
login(self, user: str, password: str) -> User
: Logs in a user and returns user data.get_measurements(self, crop_id: str, variable_id: str) -> list
: Retrieves measurements based on crop and variable IDs.
__init__(self)
: Initializes theDatabaseAdapter
class.- Prints an initialization message.
- Creates an instance of
DBService
for interacting with the database.
- Import this module to use the
DatabaseAdapter
class for database operations. - Implement a concrete class that inherits from
DatabaseClient
to define specific database interactions. - The module includes print statements for logging database operations.
This Python code defines a DBService
class responsible for interacting with a PostgreSQL database. It includes methods for creating users, organizations, crops, variables, and measurements, as well as retrieving user and measurement data. The class uses SQLAlchemy for database operations and includes print statements for logging.
- Description: A class providing services for interacting with a PostgreSQL database.
- Dependencies: Requires the
schema
module for entity definitions andcreate_engine_with_retry
for creating a database engine. - Initialization:
- Loads environment variables using
dotenv
. - Establishes a connection to the database using SQLAlchemy.
- Creates an instance of the
sessionmaker
for managing sessions.
- Loads environment variables using
create_user(self, user: str, email: str, password: str) -> User
: Creates a new user in the database.create_org(self, name: str, description: str, password: str) -> Org
: Creates a new organization in the database.create_crop(self, org_id: str, name: str, lat: float, lon: float) -> Crop
: Creates a new crop in the database.create_variable(self, name: str, units: str, description: str) -> Variable
: Creates a new variable in the database.add_measurement(self, datetime: str, crop_id: str, value: float, variable_id: str)
: Adds a new measurement to the database.
get_user(self, username: str) -> User
: Retrieves user data by username.login(self, username: str, password: str) -> User
: Logs in a user and returns user data.get_measurement_by_id(self, measurement_id: str) -> Measurement
: Retrieves a measurement by ID.get_measurements_by_crop_id(self, crop_id: str) -> List[Measurement]
: Retrieves measurements for a specific crop.get_measurements_by_crop_and_variable(self, crop_id: str, variable_id: str) -> List[Measurement]
: Retrieves measurements for a specific crop and variable.
- The code includes a section for main execution (
if __name__ == '__main__':
) with commented-out lines. - Demonstrates the usage of the
DBService
class with example operations such as user creation, organization creation, crop creation, variable creation, and measurement addition.
This Python code defines a SQLAlchemy database schema for PostgreSQL integration. It includes several classes representing entities such as organizations, crops, variables, conditions, actuator types, actuators, measurements, users, and permissions.
- Description: Base class for all database models using SQLAlchemy's declarative base.
- Attributes:
id
: UUID primary key for all entities.
- Description: Represents an organization in the database.
- Attributes:
id
: UUID primary key.name
: Organization name.description
: Organization description.password
: Hashed organization password.crops
: Relationship with theCrop
class.
- Description: Represents a crop in the database.
- Attributes:
id
: UUID primary key.org_id
: UUID foreign key referencing theOrg
class.name
: Crop name.coordinate_latitude
: Latitude coordinate.coordinate_longitude
: Longitude coordinate.conditions
: Relationship with theCondition
class.actuators
: Relationship with theActuator
class.measurements
: Relationship with theMeasurement
class.
- Description: Represents a variable in the database.
- Attributes:
id
: UUID primary key.name
: Variable name.units
: Variable units.description
: Variable description.
- Description: Represents a condition in the database.
- Attributes:
id
: UUID primary key.crop_id
: UUID foreign key referencing theCrop
class.variable_id
: UUID foreign key referencing theVariable
class.min_value
: Minimum value for the condition.max_value
: Maximum value for the condition.crop
: Relationship with theCrop
class.variable
: Relationship with theVariable
class.
- Description: Represents an actuator type in the database.
- Attributes:
id
: UUID primary key.name
: Actuator type name.description
: Actuator type description.
- Description: Represents an actuator in the database.
- Attributes:
id
: UUID primary key.name
: Actuator name.mqtt_topic
: MQTT topic for the actuator.crop_id
: UUID foreign key referencing theCrop
class.actuator_type_id
: UUID foreign key referencing theActuatorType
class.start_time
: Start time for the actuator.end_time
: End time for the actuator.crop
: Relationship with theCrop
class.actuator_type
: Relationship with theActuatorType
class.
- Description: Represents a measurement in the database.
- Attributes:
id
: UUID primary key.datetime
: Date and time of the measurement.crop_id
: UUID foreign key referencing theCrop
class.value
: Measurement value.variable_id
: UUID foreign key referencing theVariable
class.crop
: Relationship with theCrop
class.variable
: Relationship with theVariable
class.
- Description: Represents a user in the database.
- Attributes:
id
: UUID primary key.email
: User email.username
: Username.password
: Hashed user password.
- Description: Represents a permission in the database.
- Attributes:
id
: UUID primary key.user_id
: UUID foreign key referencing theUser
class.org_id
: UUID foreign key referencing theOrg
class.crop_id
: UUID foreign key referencing theCrop
class.granted
: Boolean indicating if the permission is granted._id
: Unique identifier combining user, organization, and permission type.user
: Relationship with theUser
class.org
: Relationship with theOrg
class.crop
: Relationship with theCrop
class.
- Description: Defines a SQLAlchemy engine with a retry mechanism for connecting to the database.
- Parameters:
db_url
: Database URL.
- Returns:
- SQLAlchemy engine.
- The code includes a section for main execution (
if __name__ == '__main__':
) with commented-out lines. - Demonstrates the usage of environment variables for database configuration, engine creation, and schema initialization.