some nice mixins for tags, ratings, relations
[zzz-floof.git] / floof / lib / dbhelpers.py
index 4d10030..23fd3b4 100644 (file)
@@ -2,4 +2,18 @@ def find_or_create(model, **kwargs):
     instance = model.get_by(**kwargs)
     if not instance:
         instance = 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
     return instance
\ No newline at end of file