summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Hagander2018-06-01 20:06:15 +0000
committerMagnus Hagander2018-06-01 20:06:15 +0000
commit1efc44dd93094ae556f340fbffe92a150c4a7439 (patch)
tree90238b9beebf1b6b3caf3cbbe8c74189d5cc5392
parentc7eae5c780768c00987789255a0580318584158b (diff)
Add basic API to retreive active CFs
This is a first step towards a more complete API to be used by the cfbot. Getting the initial one pushed in so we have a base to test on.
-rw-r--r--pgcommitfest/commitfest/api.py31
-rw-r--r--pgcommitfest/settings.py6
-rw-r--r--pgcommitfest/urls.py2
3 files changed, 39 insertions, 0 deletions
diff --git a/pgcommitfest/commitfest/api.py b/pgcommitfest/commitfest/api.py
new file mode 100644
index 0000000..ea12043
--- /dev/null
+++ b/pgcommitfest/commitfest/api.py
@@ -0,0 +1,31 @@
+from django.shortcuts import get_object_or_404
+from django.http import HttpResponse, Http404, HttpResponseForbidden
+from django.conf import settings
+from django.views.decorators.csrf import csrf_exempt
+from functools import wraps
+from django.utils.decorators import available_attrs
+
+import json
+
+from models import CommitFest
+
+def api_authenticate(view_func):
+ def _wrapped_view(request, *args, **kwargs):
+ if not request.META['REMOTE_ADDR'] in settings.API_AUTH:
+ return HttpResponseForbidden('Access Denied')
+ if not request.META.get('HTTP_X_API_AUTH', '') == settings.API_AUTH[request.META['REMOTE_ADDR']]:
+ return HttpResponseForbidden('Access Denied')
+ return view_func(request, *args, **kwargs)
+ return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
+
+@api_authenticate
+def active_commitfests(request):
+ cfs = list(CommitFest.objects.filter(status=CommitFest.STATUS_INPROGRESS))
+ if not cfs:
+ cfs = CommitFest.objects.filter(status=CommitFest.STATUS_OPEN).order_by('-id')[:1]
+
+ res = [
+ {'id': c.id, 'name': c.name, 'status': c.statusstring, 'numstatus': c.status} for c in cfs
+ ]
+ return HttpResponse(json.dumps(res),
+ content_type='application/json')
diff --git a/pgcommitfest/settings.py b/pgcommitfest/settings.py
index 1c1a860..7c84d25 100644
--- a/pgcommitfest/settings.py
+++ b/pgcommitfest/settings.py
@@ -174,6 +174,12 @@ HACKERS_EMAIL="pgsql-hackers-testing@localhost"
# Email address for outgoing system messages
NOTIFICATION_FROM="[email protected]"
+# IP addresses and passwords to access API
+API_AUTH = {
+ '127.0.0.1': 'foobarbaz123',
+}
+
+
# Load local settings overrides
try:
from local_settings import *
diff --git a/pgcommitfest/urls.py b/pgcommitfest/urls.py
index eafb99c..248efb6 100644
--- a/pgcommitfest/urls.py
+++ b/pgcommitfest/urls.py
@@ -8,6 +8,7 @@ admin.autodiscover()
import pgcommitfest.commitfest.views as views
import pgcommitfest.commitfest.reports as reports
import pgcommitfest.commitfest.ajax as ajax
+import pgcommitfest.commitfest.api as api
import pgcommitfest.auth
import pgcommitfest.userprofile.views
@@ -32,6 +33,7 @@ urlpatterns = [
url(r'^search/$', views.global_search),
url(r'^ajax/(\w+)/$', ajax.main),
url(r'^thread_notify/$', views.thread_notify),
+ url(r'^api/active_commitfests/$', api.active_commitfests),
url(r'^selectable/', include('selectable.urls')),