[CalendarServer-changes] [12690] CalendarServer/trunk/calendarserver/webadmin

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:20:26 PDT 2014


Revision: 12690
          http://trac.calendarserver.org//changeset/12690
Author:   wsanchez at apple.com
Date:     2014-02-13 17:28:27 -0800 (Thu, 13 Feb 2014)
Log Message:
-----------
Use more template and less code.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/webadmin/principals.py
    CalendarServer/trunk/calendarserver/webadmin/principals.xhtml

Modified: CalendarServer/trunk/calendarserver/webadmin/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/webadmin/principals.py	2014-02-13 22:14:57 UTC (rev 12689)
+++ CalendarServer/trunk/calendarserver/webadmin/principals.py	2014-02-14 01:28:27 UTC (rev 12690)
@@ -50,7 +50,10 @@
 
 
     @renderer
-    def search_value(self, request, tag):
+    def search_terms(self, request, tag):
+        """
+        Inserts search terms as a text child of C{tag}.
+        """
         terms = searchTerms(request)
         if terms:
             return tag(value=u" ".join(terms))
@@ -59,28 +62,40 @@
 
 
     @renderer
-    @inlineCallbacks
-    def search_results(self, request, tag):
-        terms = searchTerms(request)
+    def search_results_display(self, request, tag):
+        """
+        Renders C{tag} if there are search results, otherwise removes it.
+        """
+        if searchTerms(request):
+            return tag
+        else:
+            return u""
 
-        if not terms:
-            returnValue(u"")
 
-        records = tuple((
-            yield self.recordsForSearchTerms(terms)
-        ))
+    @renderer
+    def search_results_row(self, request, tag):
+        def rowsForRecords(records):
+            for record in records:
+                yield tag.clone().fillSlots(
+                    **slotsForRecord(record)
+                )
 
-        if records:
-            returnValue(tag(recordsTable(records)))
-        else:
-            returnValue(tag(u"No records found."))
+        d = self.recordsForSearchTerms(request)
+        d.addCallback(rowsForRecords)
+        return d
 
 
-    def recordsForSearchTerms(self, terms):
-        return self._directory.recordsMatchingTokens(terms)
+    @inlineCallbacks
+    def recordsForSearchTerms(self, request):
+        if not hasattr(request, "_search_result_records"):
+            terms = searchTerms(request)
+            records = yield self._directory.recordsMatchingTokens(terms)
+            request._search_result_records = tuple(records)
 
+        returnValue(request._search_result_records)
 
 
+
 class PrincipalsResource(TemplateResource):
     """
     Principal management page resource.
@@ -178,86 +193,64 @@
 
 
 def searchTerms(request):
-    if request.args:
+    if not hasattr(request, "_search_terms"):
         terms = set()
 
-        for query in request.args.get(u"search", []):
-            for term in query.split(u" "):
-                terms.add(term)
+        if request.args:
 
-        for term in request.args.get(u"term", []):
-            terms.add(term)
+            for query in request.args.get(u"search", []):
+                for term in query.split(u" "):
+                    if term:
+                        terms.add(term)
 
-        return terms
+            for term in request.args.get(u"term", []):
+                if term:
+                    terms.add(term)
 
-    else:
-        return set()
+        request._search_terms = terms
 
+    return request._search_terms
 
 
-def recordsTable(records):
-    def multiValue(values):
-        return ((s, tags.br()) for s in values)
 
-    def recordRows(records):
-        attrs_record = {"class": "record"}
-        attrs_fullName = {"class": "record_full_name"}
-        attrs_uid = {"class": "record_uid"}
-        attrs_recordType = {"class": "record_type"}
-        attrs_shortName = {"class": "record_short_name"}
-        attrs_email = {"class": "record_email"}
+def slotsForRecord(record):
+    def one(value):
+        if value is None:
+            return u"(no value)"
+        else:
+            try:
+                return unicode(value)
+            except UnicodeDecodeError:
+                try:
+                    return unicode(repr(value))
+                except UnicodeDecodeError:
+                    return u"(error rendering value)"
 
-        i0 = u"\n" + (6 * u" ") + (0 * 2 * u" ")
-        i1 = u"\n" + (6 * u" ") + (1 * 2 * u" ")
-        i2 = u"\n" + (6 * u" ") + (2 * 2 * u" ")
+    def many(values):
+        noValues = True
 
-        yield (
-            i0,
-            tags.thead(
-                i1,
-                tags.tr(
-                    i2, tags.th(u"Full name", **attrs_fullName),
-                    i2, tags.th(u"UID", **attrs_uid),
-                    i2, tags.th(u"Record Type", **attrs_recordType),
-                    i2, tags.th(u"Short Name", **attrs_shortName),
-                    i2, tags.th(u"Email Address", **attrs_email),
-                    i1,
-                    **attrs_record
-                ),
-                i0,
-            ),
-            i0,
-        )
+        for value in values:
+            if not noValues:
+                yield tags.br()
 
-        yield (
-            tags.tbody(
-                (
-                    i1,
-                    tags.tr(
-                        i2, tags.td(record.fullName, **attrs_fullName),
-                        i2, tags.td(record.uid, **attrs_uid),
-                        i2, tags.td(record.recordType, **attrs_recordType),
-                        i2, tags.td(
-                            multiValue(record.shortNames), **attrs_shortName
-                        ),
-                        i2, tags.td(
-                            multiValue(record.emailAddresses), **attrs_email
-                        ),
-                        i1,
-                        onclick=(
-                            'window.open("./{0}");'
-                            .format(record.uid)
-                        ),
-                        **attrs_record
-                    ),
-                )
-                for record in sorted(records, key=lambda record: record.uid)
-            ),
-            i0
-        )
+            yield one(value)
 
-    return tags.table(
-        tags.caption(u"Records"),
-        recordRows(records),
-        id="records",
-    )
+            noValues = False
+
+        if noValues:
+            yield u"(no values)"
+
+    return {
+        u"service": (
+            u"{record.service.__class__.__name__}: {record.service.realmName}"
+            .format(record=record)
+        ),
+        u"uid": one(record.uid),
+        u"guid": one(record.guid),
+        u"record_type": one(record.recordType),
+        u"short_names": many(record.shortNames),
+        u"full_names": one(record.fullName),
+        u"email_addresses": many(record.emailAddresses),
+        u"calendar_user_addresses": many(record.calendarUserAddresses),
+        u"server_id": one(record.serverID),
+    }

Modified: CalendarServer/trunk/calendarserver/webadmin/principals.xhtml
===================================================================
--- CalendarServer/trunk/calendarserver/webadmin/principals.xhtml	2014-02-13 22:14:57 UTC (rev 12689)
+++ CalendarServer/trunk/calendarserver/webadmin/principals.xhtml	2014-02-14 01:28:27 UTC (rev 12690)
@@ -17,10 +17,30 @@
 
     <form id="search_form" action="" method="get">
       Search:
-      <input id="search_field" type="search" name="search" placeholder="Search..." size="40" autofocus="true" t:render="search_value" />
+      <input id="search_field" type="search" name="search" placeholder="Search..." size="40" autofocus="true" t:render="search_terms" />
     </form>
 
-    <div t:render="search_results" />
+    <table id="records" t:render="search_results_display">
+      <caption>Records</caption>
+      <thead>
+        <tr class="record">
+          <th class="record_full_name">Full name</th>
+          <th class="record_uid">UID</th>
+          <th class="record_type">Record Type</th>
+          <th class="record_short_name">Short Name</th>
+          <th class="record_email">Email Address</th>
+        </tr>
+      </thead>
+      <tbody>
+        <tr class="record" onclick="window.open(&quot;./0CE0BF31-5F9E-4801-A489-8C70CF287F5F&quot;);" t:render="search_results_row">
+          <td class="record_full_name"><t:slot name="full_names" /></td>
+          <td class="record_uid"><t:slot name="uid" /></td>
+          <td class="record_type"><t:slot name="record_type" /></td>
+          <td class="record_short_name"><t:slot name="short_names" /></td>
+          <td class="record_email"><t:slot name="email_addresses" /></td>
+        </tr>
+      </tbody>
+    </table>
 
   </body>
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/8cacbffb/attachment.html>


More information about the calendarserver-changes mailing list