def handle_authorize(remote, token, user_info): user = None try: user = _UserService.get_user_by_email(_UserService, user_info['email']) user = dict_to_model(User, user) user.last_login_date = to_utc_datetime() user.save() user = model_to_dict(user) except BaseException: pass try: if user is None: user = _UserService.create_user(_UserService, email=user_info['email']) identity_object = { "is_user_token": True, "email": user_info['email'], "acces_token": token['access_token'], "user": user } access_token = create_access_token(identity=identity_object) response = make_response(redirect('/')) session["user"] = user print(session["user"]) set_access_cookies(response, access_token) return response except Exception as err: return ErrorObject.create_response(_UserService, err.args[0], err.args[1])
def deactivate_token_of_user(self, user_id: int, data_source_token_id: int): """Deactivates the token of the user Arguments: user_id {int} -- User id data_source_token_id {int} -- Token id Returns: DataSourceToken -- Deactivated DataSourceToken object """ try: data_source_token = DataSourceToken.get( (DataSourceToken.id == data_source_token_id) & (DataSourceToken.user_id == user_id)) if data_source_token.is_active: data_source_token.is_active = False data_source_token.deactivated_since = to_utc_datetime() data_source_token.save() return model_to_dict(data_source_token, recurse=False) else: return_dict = model_to_dict(data_source_token, recurse=False) return_dict = add_extra_info_to_dict( return_dict, 'message', f'Token with id {data_source_token_id} has already been ' f'deactivated.') return return_dict except DoesNotExist: raise ValueError( HTTPStatus.NOT_FOUND, 'Unable to find data source token given user and token id')
def admin_revoke_token(self, data_source_token_id: int): """Revokes token of data source as an admin. Arguments: data_source_token_id {int} -- Id of data source token Returns: DataSourceToken -- Deactivated DataSourceToken object """ data_source_token = DataSourceTokenService.get_token_by_id( self, data_source_token_id) if data_source_token is not None: if data_source_token.is_active: data_source_token.is_active = False data_source_token.deactivated_since = to_utc_datetime() data_source_token.save() return model_to_dict(data_source_token, recurse=False) else: return_dict = model_to_dict(data_source_token, recurse=False) return_dict = add_extra_info_to_dict( return_dict, 'message', f'Token with id {data_source_token_id} has already been ' f'deactivated.') return return_dict else: raise
def get_weather_forecast_5d_3h(self): """Retrieves the 5 days 3 hour weather forecast""" weather_data = _client.get_weather_forecast_5d_3h() try: Weather.create(created_date=to_utc_datetime(), data=weather_data, data_source=1, weather_forecast_type=Forecast.FIVE_DAYS_THREE_HOUR) return HTTPStatus.CREATED except BaseException: raise
def get_current_weather(self): """Retrieves the current weather data""" weather_data = _client.get_current_weather() try: Weather.create(created_date=to_utc_datetime(), data=weather_data, data_source=1, weather_forecast_type=Forecast.HOURLY) return HTTPStatus.CREATED except BaseException: raise
def create_user(self, email: str, username=None): """Creates a new user Arguments: email {str} -- email of user Keyword Arguments: username {str} -- Optional: username of user (default: {None}) Raises: ValueError: Email is required BaseException: Internal server error Returns: User -- Newly created user """ join_date = to_utc_datetime() last_login_date = to_utc_datetime() try: if username is not None: return model_to_dict( User.create(username=username, email=email, join_date=join_date, last_login_date=last_login_date)) else: return model_to_dict( User.create(email=email, join_date=join_date, last_login_date=last_login_date)) except IntegrityError: if email is None: raise ValueError(HTTPStatus.BAD_REQUEST, 'Email is required') else: raise ValueError(HTTPStatus.CONFLICT, 'Email already exists') except Exception: raise BaseException(HTTPStatus.INTERNAL_SERVER_ERROR, 'Internal server error')
def update_hit_count_and_date(self, data_source_token_id: int): """Update the hit counter of an data source token Arguments: data_source_token_id {int} -- Id of data source token Returns: HTTPstatus code -- 204 No Content """ data_source_token = DataSourceTokenService.get_token_by_id( self, data_source_token_id) if data_source_token is not None: data_source_token.no_of_usage += 1 data_source_token.last_activity_date = to_utc_datetime() data_source_token.save() return HTTPStatus.NO_CONTENT else: raise
def _seed(): """Creates the admin and a data source""" from api.helpers import to_utc_datetime join_date = to_utc_datetime() user = User.get_or_create(email='*****@*****.**', defaults={ 'join_date': join_date, 'last_login_date': join_date, 'username': '******' }) DataSource.get_or_create( id=1, defaults={ 'source': 'system_scheduled_task', 'description': 'A data source that is used to send the result from the scheduled \ task', 'user': user[0].id })
def post_data(self, data_source_id: int, create_data_source_data_dto: CreateDataSourceDataDto): """Creates a data point Arguments: data_source_id {int} -- id of data source create_data_source_data_dto {CreateDataSourceDataDto} -- Data transfer object containing the payload for the data point Raises: ValueError: Data source not found with given id Returns: DataSourceData -- DataSourceData object """ data_source = None try: data_source = _DataSourceService.get_data_source_by_id( self, data_source_id) except Exception: raise try: if create_data_source_data_dto: return model_to_dict( DataSourceData.create( data_source=dict_to_model(DataSource, data_source), no_of_clients=create_data_source_data_dto. no_of_clients, created_date=to_utc_datetime())) else: raise ValueError(HTTPStatus.BAD_REQUEST, 'Body is required') except IntegrityError: raise ValueError(HTTPStatus.INTERNAL_SERVER_ERROR, 'Internal server error')
def create_token_for_data_source(self, data_source_id: int, user_id: int): """Creates a JWT for an data source Arguments: data_source_id {int} -- Id of data source user_id {int} -- Id of user Raises: ValueError: Unable to create token for data source Returns: JWT -- Returns an JWT """ user: User = None data_source: DataSource = None try: if user_id: user = _UserService.get_user_by_id(self, user_id) except Exception: raise try: if data_source_id: data_source = dict_to_model( DataSource, _DataSourceService.get_data_source_by_id( self, data_source_id)) except Exception: raise if user is not None and isinstance( user, User) and data_source is not None and isinstance( data_source, DataSource): last_activity_date = None # Set validity of token to 1 year expiry_date = to_utc_datetime( (datetime.utcnow() + relativedelta(years=1))) no_of_usage = 0 try: result = model_to_dict(DataSourceToken.create( user=user, data_source=data_source, created_date=to_utc_datetime(), last_activity_date=last_activity_date, expiry_date=expiry_date, no_of_usage=no_of_usage, is_active=True), recurse=False) redundant_keys = [ 'last_activity_date', 'no_of_usage', 'is_active', 'deactivated_since' ] result = remove_items_from_dict(result, redundant_keys) identity_object = { "is_user_token": False, "data_source_token": result } return create_access_token( identity=identity_object, expires_delta=relativedelta(years=1)) except Exception: raise ValueError( HTTPStatus.INTERNAL_SERVER_ERROR, 'Unable to create token for data source {}'.format( data_source_id))
def create_next_week_prediction(self, start_date=None, number_of_weeks_to_use: int = 4, use_start_of_the_week: bool = True): """Create next week prediction Keyword Arguments: start_date -- start_date is the starting point to get the data of the past week and to calculate the following week. (default : {None}) number_of_weeks_to_use {int} -- number of weeks to use as input data (default: {3}) use_start_of_the_week {bool} -- boolean to use the start of the week (default: {True}) Returns: CrowdForecast -- A crowd forecast object will be returned """ # retrieve the last three weeks for prediction # This service should run every saturday to get the forecast of the # following week import datetime as dt if start_date: validate_dateformat('start_date', start_date) start_date = dt.datetime.strptime(start_date, '%Y-%m-%d').date() if not number_of_weeks_to_use: number_of_weeks_to_use = 4 if not use_start_of_the_week: use_start_of_the_week = True try: use_start_of_the_week = validate_string_bool(use_start_of_the_week) except ValueError as err: raise ValueError(HTTPStatus.BAD_REQUEST, str(err)) try: if not use_start_of_the_week: week_day = dt.date.today().weekday() else: week_day = 0 # let day of the week start at 0 start, end = _df_helper.get_past_weeks( start_date=start_date, number_of_weeks=validate_string_int(number_of_weeks_to_use), use_start_of_the_week=use_start_of_the_week) data = _DataSourceDataService.get_all_data_from_data_source( _DataSourceDataService, data_source_id=2, limit=10000, start_date=start, end_date=end) data_frame = _transform_data_to_dataframe(data) data_frame = _add_time_characteristics(data_frame) next_week_dataframe = _create_next_week_dataframe( start_date, week_day) next_week_start = dt.datetime.fromtimestamp( next_week_dataframe.iloc[[ 0 ]]['created_date'].values[0].astype(int) // 10**9) next_week_end = dt.datetime.fromtimestamp( next_week_dataframe.iloc[[ -1 ]]['created_date'].values[0].astype(int) // 10**9) train, test = train_test_split(data_frame, test_size=0.25, shuffle=False) scaler = StandardScaler() train_X_scaled = scaler.fit_transform(train.iloc[:, range(2, 5)]) train_y = train.iloc[:, 1] future_X_scaled = scaler.fit_transform( next_week_dataframe.iloc[:, range(1, 4)]) svr_rbf = SVR(C=100.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma=1.0, kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False) result = svr_rbf.fit(train_X_scaled, train_y).predict(future_X_scaled) next_week_dataframe['predicted_no_of_clients'] = result result = CrowdForecast.create( created_date=to_utc_datetime(), date_range_used=f"{start}_{end}", number_of_weeks_used=number_of_weeks_to_use, prediction_for_week_nr=next_week_start.isocalendar()[1], prediction_start_date=next_week_start.strftime('%Y-%m-%d'), prediction_end_date=next_week_end.strftime('%Y-%m-%d'), prediction_data=next_week_dataframe.to_json()) return model_to_dict(result) except IntegrityError as err: print(err) # replace with a logger raise ValueError( HTTPStatus.CONFLICT, f"A forecast already exists with parameters:" f"date_range_used: from {start} - {end} " f"prediction_for_date: from " f"{next_week_start.strftime('%Y-%m-%d')} - " f"{next_week_end.strftime('%Y-%m-%d')}") except Exception as err: print(err) # replace with a logger raise ValueError(HTTPStatus.INTERNAL_SERVER_ERROR, "Internal error has occured.")