From 241f37c2beb36a892fb9c9c40ddcf4d31d0eed17 Mon Sep 17 00:00:00 2001
From: Brian Moe <brian.moe@ligo.org>
Date: Thu, 31 May 2012 15:36:57 -0500
Subject: [PATCH] stab at latest page

---
 gracedb/forms.py     |  2 +-
 gracedb/query.py     |  3 +++
 gracedb/views.py     | 19 +++++++++++++++++++
 static/css/style.css |  2 ++
 templates/base.html  |  1 +
 urls.py              |  2 ++
 6 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/gracedb/forms.py b/gracedb/forms.py
index 650b9d779..b9c905b5e 100644
--- a/gracedb/forms.py
+++ b/gracedb/forms.py
@@ -28,7 +28,7 @@ class GraceQueryField(forms.CharField):
             raise forms.ValidationError(str(e))
 
 class SimpleSearchForm(forms.Form):
-    query = GraceQueryField(required=True, widget=forms.TextInput(attrs={'size':60}))
+    query = GraceQueryField(required=False, widget=forms.TextInput(attrs={'size':60}))
 
 
 class CreateEventForm(forms.Form):
diff --git a/gracedb/query.py b/gracedb/query.py
index 6e7952db5..d54e6035e 100644
--- a/gracedb/query.py
+++ b/gracedb/query.py
@@ -161,6 +161,9 @@ q = (hasfarQ | gidQ | hidQ | tidQ | labelQ | atypeQ | groupQ | gpsQ | createdQ |
 
 def parseQuery(s):
     d={}
+    if not s:
+        # Empty query return everything not in Test group
+        return ~Q(group__name="Test")
     for (tag, qval) in (stringStart + OneOrMore(q) + stringEnd).parseString(s).asList():
         d[tag] = d.get(tag,Q()) | qval
     if s.find("Test") < 0 and "tid" not in d:
diff --git a/gracedb/views.py b/gracedb/views.py
index 14bf006d3..fc84b6074 100644
--- a/gracedb/views.py
+++ b/gracedb/views.py
@@ -934,3 +934,22 @@ Initial Entry for %s
     os.chmod(pname, 0644)
     os.chmod(rcsname, 0444)
 
+
+def latest(request):
+    context = {}
+
+    if request.method == "GET":
+        form = SimpleSearchForm(request.GET)
+    else:
+        form = SimpleSearchForm(request.POST)
+
+    context['form'] = form
+
+    if form.is_valid():
+        query = form.cleaned_data['query']
+        context['objects'] = Event.objects.filter(query).distinct().order_by("-created")[:10]
+
+    return render_to_response(
+            'gracedb/latest.html',
+            context,
+            context_instance=RequestContext(request))
diff --git a/static/css/style.css b/static/css/style.css
index 7d43311a0..268f6f7f9 100644
--- a/static/css/style.css
+++ b/static/css/style.css
@@ -149,6 +149,7 @@ span.coinc-0,
 #lab #nav-lab a,
 #reviews #nav-reviews a,
 #userprofile #nav-userprofile a,
+#latest #nav-latest a,
 #contact #nav-contact a {
     background: #a9b0ba;  /* Nav selected color */
     /* color:#fff;  / * Use if bg is dark */
@@ -165,6 +166,7 @@ span.coinc-0,
 #lab #nav-lab a:hover,
 #reviews #nav-reviews a:hover,
 #userprofile #nav-userprofile a:hover,
+#latest #nav-latest a:hover,
 #contact #nav-contact a:hover {
     /* background:#e35a00; */
     background: #a9b0ba;  /* Nav selected color */
diff --git a/templates/base.html b/templates/base.html
index df80171f9..f9eec575d 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -35,6 +35,7 @@ function changeTime(obj, label) {
     <li id="nav-create"><a href="{% url create %}">Create</a></li>
     <li id="nav-reports"><a href="{% url reports %}">Reports</a></li>
     <li id="nav-feeds"><a href="{% url feeds %}">RSS</a></li>
+    <li id="nav-latest"><a href="{% url latest %}">Latest</a></li>
     <li id="nav-userprofile"><a href="{% url userprofile-home %}">Options</a></li>
     {% if ligouser %}<li id="nav-user">Authenticated as: {{ ligouser.name }}</li>{% endif %}
 </ul>
diff --git a/urls.py b/urls.py
index 021b2905c..3f5836eba 100644
--- a/urls.py
+++ b/urls.py
@@ -31,6 +31,8 @@ urlpatterns = patterns('',
     (r'^reports/(?P<path>.+)$', 'django.views.static.serve',
             {'document_root': settings.LATENCY_REPORT_DEST_DIR}),
 
+    url (r'^latest', 'gracedb.views.latest', name="latest"),
+
     # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
     # to INSTALLED_APPS to enable admin documentation:
     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
-- 
GitLab