From 18c810b5225c54f29392c3b477e75ffb3bb44470 Mon Sep 17 00:00:00 2001 From: Anurag Sharma Date: Mon, 12 Oct 2015 18:57:13 +0530 Subject: [PATCH] migrated commit_represent to S3Represent --- modules/s3db/req.py | 96 ++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/modules/s3db/req.py b/modules/s3db/req.py index c7b808181e..4ba0476cbf 100644 --- a/modules/s3db/req.py +++ b/modules/s3db/req.py @@ -47,6 +47,7 @@ "req_req_list_layout", "req_customise_commit_fields", "req_commit_list_layout", + "req_CommitRepresent" ) from gluon import * @@ -2349,14 +2350,16 @@ def model(self): msg_record_deleted = T("Commitment Canceled"), msg_list_empty = T("No Commitments")) + commit_represent = req_CommitRepresent() + # Reusable Field commit_id = S3ReusableField("commit_id", "reference %s" % tablename, label = T("Commitment"), ondelete = "CASCADE", - represent = self.commit_represent, + represent = commit_represent, requires = IS_EMPTY_OR( IS_ONE_OF(db, "req_commit.id", - self.commit_represent, + commit_represent, orderby="req_commit.date", sort=True)), sortby = "date", @@ -2423,35 +2426,6 @@ def model(self): return dict(req_commit_id = commit_id, ) - # ------------------------------------------------------------------------- - @staticmethod - def commit_represent(id, row=None): - """ - Represent a Commit - - @ToDo: Migrate to S3Represent - """ - - if row: - table = current.db.req_commit - elif not id: - return current.messages["NONE"] - else: - db = current.db - table = db.req_commit - row = db(table.id == id).select(table.type, - table.date, - table.organisation_id, - table.site_id, - limitby=(0, 1)).first() - if row.type == 1: - # Items - return "%s - %s" % (table.site_id.represent(row.site_id), - table.date.represent(row.date)) - else: - return "%s - %s" % (table.organisation_id.represent(row.organisation_id), - table.date.represent(row.date)) - # ------------------------------------------------------------------------- @staticmethod def commit_onvalidation(form): @@ -2764,6 +2738,66 @@ def commit_ondelete(row): # @ToDo: Provide a way for the committer to specify this db(rtable.id == req_id).update(commit_status=REQ_STATUS_NONE) +# ----------------------------------------------------------------------------- +class req_CommitRepresent(S3Represent): + """Representation of Commit""" + + def __init__(self): + """ + Constructor + """ + + table = current.s3db.req_commit + super(req_CommitRepresent, self).__init__() + + def lookup_rows(self, key, values, fields=[]): + """ + Custom lookup method + @param key: the key Field + @param values: the values + @param fields: the fields to retrieve + """ + + if len(values) == 1: + query = (key == values[0]) + else: + query = key.belongs(values) + + rows = current.db(query).select(self.table.type, + self.table.date, + self.table.organisation_id, + self.table.site_id, + limitby=(0, len(values))) + + commit_dates = [row[str(self.table.date)] for row in rows] + if commit_dates: + self.table.date.represent.bulk(commit_dates) + + organisation_ids = [row[str(self.table.organisation_id)] for row in rows] + if organisation_ids: + self.table.organisation_id.represent.bulk(organisation_ids) + + site_ids = [row[str(self.table.site_id)] for row in rows] + if site_ids: + self.table.site_id.represent.bulk(site_ids) + + return rows + + # ------------------------------------------------------------------------- + def represent_row(self, row): + """ + Represent a single Row + + @param row: the Row + """ + + if row.type == 1: + return "%s - %s" % (self.table.site_id.represent(row.site_id), + self.table.date.represent(row.date)) + else: + return "%s - %s" % (self.table.organisation_id.represent(row.organisation_id), + self.table.date.represent(row.date)) + # ============================================================================= class S3CommitItemModel(S3Model): """