0da968b5d4aae9fc6d9b1cd11be9114c10b6fbc1
[zzz-floof.git] / floof / lib / dbhelpers.py
1 def find_or_create(model, **kwargs):
2 instance = model.get_by(**kwargs)
3 if not instance:
4 instance = model(**kwargs)
5 return instance
6
7 def update_or_create(model, get_by, update_with):
8 instance = model.get_by(**get_by)
9 if instance:
10 # set new values
11 for key,value in update_with.items():
12 setattr(instance, key, value)
13 else:
14 # create it
15 both = {}
16 both.update(get_by)
17 both.update(update_with)
18 instance = model(**both)
19 return instance