Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit 867311101d10b27b06ffb301b79123f6881710a0
Author: Richard Marko <rmarko(a)fedoraproject.org>
Date: Mon Jan 27 13:53:43 2014 +0100
[frontend] move Serializer to helpers
Signed-off-by: Richard Marko <rmarko(a)fedoraproject.org>
---------------------------------------------------------------
coprs_frontend/coprs/helpers.py | 70 ++++++++++++++++++++++++++++
coprs_frontend/coprs/models.py | 95 +++++---------------------------------
2 files changed, 83 insertions(+), 82 deletions(-)
diff --git a/coprs_frontend/coprs/helpers.py b/coprs_frontend/coprs/helpers.py
index ce91ae7..048042a 100644
--- a/coprs_frontend/coprs/helpers.py
+++ b/coprs_frontend/coprs/helpers.py
@@ -164,3 +164,73 @@ def is_int(s):
return True
except ValueError:
return False
+
+
+class Serializer(object):
+
+ def to_dict(self, options={}):
+ """
+ Usage:
+
+ SQLAlchObject.to_dict() => returns a flat dict of the object
+ SQLAlchObject.to_dict({"foo": {}}) => returns a dict of the object
+ and will include a flat dict of object foo inside of that
+ SQLAlchObject.to_dict({"foo": {"bar": {}}, "spam":
{}}) => returns
+ a dict of the object, which will include dict of foo
+ (which will include dict of bar) and dict of spam.
+
+ Options can also contain two special values: __columns_only__
+ and __columns_except__
+
+ If present, the first makes only specified fiels appear,
+ the second removes specified fields. Both of these fields
+ must be either strings (only works for one field) or lists
+ (for one and more fields).
+
+ SQLAlchObject.to_dict({"foo": {"__columns_except__":
["id"]},
+ "__columns_only__": "name"}) =>
+
+ The SQLAlchObject will only put its "name" into the resulting dict,
+ while "foo" all of its fields except "id".
+
+ Options can also specify whether to include foo_id when displaying
+ related foo object (__included_ids__, defaults to True).
+ This doesn"t apply when __columns_only__ is specified.
+ """
+
+ result = {}
+ columns = self.serializable_attributes
+
+ if "__columns_only__" in options:
+ columns = options["__columns_only__"]
+ else:
+ columns = set(columns)
+ if "__columns_except__" in options:
+ columns_except = options["__columns_except__"]
+ if not isinstance(options["__columns_except__"], list):
+ columns_except = [options["__columns_except__"]]
+
+ columns -= set(columns_except)
+
+ if ("__included_ids__" in options and
+ options["__included_ids__"] is False):
+
+ related_objs_ids = [
+ r + "_id" for r, o in options.items()
+ if not r.startswith("__")]
+
+ columns -= set(related_objs_ids)
+
+ columns = list(columns)
+
+ for column in columns:
+ result[column] = getattr(self, column)
+
+ for related, values in options.items():
+ if hasattr(self, related):
+ result[related] = getattr(self, related).to_dict(values)
+ return result
+
+ @property
+ def serializable_attributes(self):
+ return map(lambda x: x.name, self.__table__.columns)
diff --git a/coprs_frontend/coprs/models.py b/coprs_frontend/coprs/models.py
index 97dc899..a342c20 100644
--- a/coprs_frontend/coprs/models.py
+++ b/coprs_frontend/coprs/models.py
@@ -9,77 +9,7 @@ from coprs import db
from coprs import helpers
-class Serializer(object):
-
- def to_dict(self, options={}):
- """
- Usage:
-
- SQLAlchObject.to_dict() => returns a flat dict of the object
- SQLAlchObject.to_dict({"foo": {}}) => returns a dict of the object
- and will include a flat dict of object foo inside of that
- SQLAlchObject.to_dict({"foo": {"bar": {}}, "spam":
{}}) => returns
- a dict of the object, which will include dict of foo
- (which will include dict of bar) and dict of spam.
-
- Options can also contain two special values: __columns_only__
- and __columns_except__
-
- If present, the first makes only specified fiels appear,
- the second removes specified fields. Both of these fields
- must be either strings (only works for one field) or lists
- (for one and more fields).
-
- SQLAlchObject.to_dict({"foo": {"__columns_except__":
["id"]},
- "__columns_only__": "name"}) =>
-
- The SQLAlchObject will only put its "name" into the resulting dict,
- while "foo" all of its fields except "id".
-
- Options can also specify whether to include foo_id when displaying
- related foo object (__included_ids__, defaults to True).
- This doesn"t apply when __columns_only__ is specified.
- """
-
- result = {}
- columns = self.serializable_attributes
-
- if "__columns_only__" in options:
- columns = options["__columns_only__"]
- else:
- columns = set(columns)
- if "__columns_except__" in options:
- columns_except = options["__columns_except__"]
- if not isinstance(options["__columns_except__"], list):
- columns_except = [options["__columns_except__"]]
-
- columns -= set(columns_except)
-
- if ("__included_ids__" in options and
- options["__included_ids__"] is False):
-
- related_objs_ids = [
- r + "_id" for r, o in options.items()
- if not r.startswith("__")]
-
- columns -= set(related_objs_ids)
-
- columns = list(columns)
-
- for column in columns:
- result[column] = getattr(self, column)
-
- for related, values in options.items():
- if hasattr(self, related):
- result[related] = getattr(self, related).to_dict(values)
- return result
-
- @property
- def serializable_attributes(self):
- return map(lambda x: x.name, self.__table__.columns)
-
-
-class User(db.Model, Serializer):
+class User(db.Model, helpers.Serializer):
"""
Represents user of the copr frontend
@@ -89,12 +19,12 @@ class User(db.Model, Serializer):
# openid_name for fas, e.g.
http://bkabrda.id.fedoraproject.org/
openid_name = db.Column(db.String(100), nullable=False)
# just mail :)
- mail = db.Column(db.String(150), nullable = False)
+ mail = db.Column(db.String(150), nullable=False)
# just timezone ;)
- timezone = db.Column(db.String(50), nullable = True)
+ timezone = db.Column(db.String(50), nullable=True)
# is this user proven? proven users can modify builder memory and
# timeout for single builds
- proven = db.Column(db.Boolean, default = False)
+ proven = db.Column(db.Boolean, default=False)
# is this user admin of the system?
admin = db.Column(db.Boolean, default=False)
# stuff for the cli interface
@@ -195,7 +125,8 @@ class User(db.Model, Serializer):
except IOError:
return ""
-class Copr(db.Model, Serializer):
+
+class Copr(db.Model, helpers.Serializer):
"""
Represents a single copr (private repo with builds, mock chroots, etc.).
@@ -290,7 +221,7 @@ class Copr(db.Model, Serializer):
return result
-class CoprPermission(db.Model, Serializer):
+class CoprPermission(db.Model, helpers.Serializer):
"""
Association class for Copr<->Permission relation
@@ -309,7 +240,7 @@ class CoprPermission(db.Model, Serializer):
copr = db.relationship("Copr",
backref=db.backref("copr_permissions"))
-class Build(db.Model, Serializer):
+class Build(db.Model, helpers.Serializer):
"""
Representation of one build in one copr
@@ -385,7 +316,7 @@ class Build(db.Model, Serializer):
return self.status == helpers.StatusEnum("pending")
-class MockChroot(db.Model, Serializer):
+class MockChroot(db.Model, helpers.Serializer):
"""
Representation of mock chroot
@@ -415,7 +346,7 @@ class MockChroot(db.Model, Serializer):
arch=self.arch)
-class CoprChroot(db.Model, Serializer):
+class CoprChroot(db.Model, helpers.Serializer):
"""
Representation of Copr<->MockChroot relation
@@ -434,7 +365,7 @@ class CoprChroot(db.Model, Serializer):
cascade="all,delete,delete-orphan"))
-class BuildChroot(db.Model, Serializer):
+class BuildChroot(db.Model, helpers.Serializer):
"""
Representation of Build<->MockChroot relation
@@ -468,7 +399,7 @@ class BuildChroot(db.Model, Serializer):
return "unknown"
-class LegalFlag(db.Model, Serializer):
+class LegalFlag(db.Model, helpers.Serializer):
id = db.Column(db.Integer, primary_key=True)
# message from user who raised the flag (what he thinks is wrong)
raise_message = db.Column(db.Text)
@@ -497,7 +428,7 @@ class LegalFlag(db.Model, Serializer):
primaryjoin="LegalFlag.resolver_id==User.id")
-class Action(db.Model, Serializer):
+class Action(db.Model, helpers.Serializer):
"""
Representation of a custom action that needs