X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/3bdc7971c3359efe7f38eb1b94253b416471b87b..d4609fff2bfd910366c41116f1da35e4037da7d4:/floof/lib/dbhelpers.py diff --git a/floof/lib/dbhelpers.py b/floof/lib/dbhelpers.py index 4d10030..0da968b 100644 --- a/floof/lib/dbhelpers.py +++ b/floof/lib/dbhelpers.py @@ -2,4 +2,18 @@ def find_or_create(model, **kwargs): instance = model.get_by(**kwargs) if not instance: instance = model(**kwargs) + return instance + +def update_or_create(model, get_by, update_with): + instance = model.get_by(**get_by) + if instance: + # set new values + for key,value in update_with.items(): + setattr(instance, key, value) + else: + # create it + both = {} + both.update(get_by) + both.update(update_with) + instance = model(**both) return instance \ No newline at end of file