Skip to content

Commit

Permalink
Backport PR #111: get_or_create_source might create data source with …
Browse files Browse the repository at this point in the history
…type=user but no user set (#111)

* make sure we don't create data sources with type=user but no user; complain if no type was passed and no user either

Co-authored-by: nhoening <nhoening@users.noreply.github.com>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
3 people authored and Flix6x committed May 6, 2021
1 parent 3ffa607 commit 9413a45
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion flexmeasures/data/models/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(
name = user.username
type = "user"
self.user_id = user.id
elif user is None and type == "user":
raise TypeError("A data source cannot have type 'user' but no user set.")
self.type = type
tb.BeliefSourceDBMixin.__init__(self, name=name)
db.Model.__init__(self, **kwargs)
Expand All @@ -57,8 +59,10 @@ def __repr__(self):


def get_or_create_source(
source: Union[User, str], source_type: str = "user", flush: bool = True
source: Union[User, str], source_type: Optional[str] = None, flush: bool = True
) -> DataSource:
if is_user(source):
source_type = "user"
query = DataSource.query.filter(DataSource.type == source_type)
if is_user(source):
query = query.filter(DataSource.user == source)
Expand All @@ -72,6 +76,8 @@ def get_or_create_source(
if is_user(source):
_source = DataSource(user=source)
else:
if source_type is None:
raise TypeError("Please specify a source type")
_source = DataSource(name=source, type=source_type)
db.session.add(_source)
if flush:
Expand Down

0 comments on commit 9413a45

Please sign in to comment.