Skip to content

Commit

Permalink
Add ?debugPresenter=true option to Send Tasks form.
Browse files Browse the repository at this point in the history
debugPresenter configures Pybossa to load task presenters from local dev server.
  • Loading branch information
normangilmore committed Jun 7, 2017
1 parent 40f5321 commit e6ce921
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
43 changes: 29 additions & 14 deletions data/pybossa_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class ImproperConfigForRemote(Exception):
class InvalidTaskRun(Exception):
pass

def create_or_update_remote_project(profile, project):
def create_or_update_remote_project(profile,
project,
debug_presenter=False,
debug_server=''):
"""
This functions enqueues the worker to create a project on a remote
Pybossa server.
Expand All @@ -45,23 +48,30 @@ def create_or_update_remote_project(profile, project):
# models for later use.
# Note: If for some reason the profile or project records disappear
# by the time the worker runs, then we WANT the worker to fail.
return create_or_update_remote_project_worker.delay(profile_id=profile.id, project_id=project.id)
return create_or_update_remote_project_worker.delay(profile_id=profile.id,
project_id=project.id,
debug_presenter=debug_presenter,
debug_server=debug_server)

@django_rq.job('task_exporter', timeout=60, result_ttl=24*3600)
def create_or_update_remote_project_worker(profile_id=None, project_id=None):
def create_or_update_remote_project_worker(profile_id=None,
project_id=None,
debug_presenter=False,
debug_server=''):
profile = UserProfile.objects.get(pk=profile_id)
url = urljoin(profile.pybossa_url, "/api/project")

params = {'api_key': profile.pybossa_api_key}

project = Project.objects.get(pk=project_id)
bundlePath = getPresenterPath(project.task_type)
payload = {
"name": project.name,
"short_name": project.short_name,
"description": project.instructions,
"info": {
"task_presenter": getPresenter(bundlePath)
"task_presenter": getPresenter(project.task_type,
debug_presenter,
debug_server)
}
}
headers = {'content-type': 'application/json'}
Expand Down Expand Up @@ -135,21 +145,26 @@ def delete_remote_project_worker(profile_id=None, project_id=None):
# Pybossa returns JSON if DELETE has error.
raise ImproperConfigForRemote(resp.text)

def getPresenterPath(task_type):
def getPresenter(task_type, debug_presenter=False, debug_server=''):
if task_type == "HLTR":
return settings.HIGHLIGHTER_BUNDLE_JS
bundle_path = settings.HIGHLIGHTER_BUNDLE_JS
url_path = settings.HIGHLIGHTER_BUNDLE_URLPATH
elif task_type == "QUIZ":
return settings.QUIZ_BUNDLE_JS
bundle_path = settings.QUIZ_BUNDLE_JS
url_path = settings.QUIZ_BUNDLE_URLPATH
else:
raise InvalidTaskType("Project task type must be 'HLTR' or 'QUIZ'")

def getPresenter(bundlePath):
if os.path.isfile(bundlePath):
with open(bundlePath) as f:
js = f.read()
return "<script>\n%s\n</script>" % js
if debug_presenter:
url = urljoin(debug_server, url_path)
return '<script type="text/javascript" src="%s"></script>' % url
else:
raise FileNotFound("Task Presenter bundle.js not found: %s" % (bundlePath))
if os.path.isfile(bundle_path):
with open(bundle_path) as f:
js = f.read()
return '<script>\n%s\n</script>' % js
else:
raise FileNotFound("Task Presenter bundle.js not found: %s" % (bundle_path))

# Use our default user and projects to exercise the API.
def testCreateRemoteProjects():
Expand Down
10 changes: 9 additions & 1 deletion researcher/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.forms.widgets import SelectMultiple
from django.forms.widgets import SelectMultiple, HiddenInput, TextInput

from thresher.models import Project, Topic

Expand All @@ -24,6 +24,7 @@ def label_from_instance(self, t):
help_select_project = "Select the Project for which you would like to generate tasks."
help_select_topics = "The selected topics will be used for task generation."
help_with_nlp = "Checking this box will generate NLP hints instead of generating tasks. Potentially time consuming."
help_with_debug_server = "The task presenter for this project will be retrieved from this server on each page load."
class SendTasksForm(forms.Form):
project = SelectProjectField(Project.objects.all().order_by("name"),
empty_label=None,
Expand All @@ -36,3 +37,10 @@ class SendTasksForm(forms.Form):
add_nlp_hints = forms.BooleanField(required=False,
label="Begin NLP processing",
help_text=help_with_nlp)
debug_presenter = forms.BooleanField(required=False, initial=False, widget=HiddenInput)
debug_server = forms.CharField(required=False,
label="Debug presenter flag active",
max_length=200,
initial="http://localhost:3001",
help_text=help_with_debug_server,
widget=TextInput(attrs={"size":40}))
22 changes: 20 additions & 2 deletions researcher/templates/researcher/send_tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 class="main-label">
<div class="form-label">
NLP Hints
</div>
<div class="form-selector" id="ending-article-id">
<div class="form-selector">
{{ form.add_nlp_hints }}
<span class="nlp-hints-label">
{{ form.add_nlp_hints.label }}
Expand All @@ -80,7 +80,25 @@ <h1 class="main-label">
</div>
</div>

<!-- {{ form.as_p }} -->
{% if form.debug_presenter.value %}
<div class="form-container">
<div class="form-label">
Debug flag active
</div>
<div class="form-selector">
{{ form.debug_server }}
</div>
<div class="form-hint">
{{ form.debug_server.help_text }}
</div>
</div>
{% endif %}

{{ form.debug_presenter }}

{% comment %}
<!-- {{ form.as_p }} -->
{% endcomment %}
{% csrf_token %}
<button type="submit" class="submit">
Send Tasks
Expand Down
12 changes: 9 additions & 3 deletions researcher/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ def get_task_generator(self, project):
def get(self, request):
agg = Article.objects.aggregate(Min('id'), Max('id'))
initial = { 'starting_article_id': agg['id__min'],
'ending_article_id': agg['id__max']
'ending_article_id': agg['id__max'],
'debug_presenter': request.GET.get("debugPresenter", False)
}
return render(
request,
self.template_name,
{'form': self.form_class(initial=initial),
'user': request.user
'user': request.user,
}
)

Expand Down Expand Up @@ -167,7 +168,12 @@ def post(self, request):
if bound_form.cleaned_data['add_nlp_hints']:
generator = generate_nlp_tasks_worker.delay
else:
job = create_or_update_remote_project(request.user.userprofile, project)
debug_presenter = bound_form.cleaned_data['debug_presenter']
debug_server = bound_form.cleaned_data['debug_server']
job = create_or_update_remote_project(request.user.userprofile,
project,
debug_presenter=debug_presenter,
debug_server=debug_server)
generator = self.get_task_generator(project)

generator(profile_id=profile_id,
Expand Down
2 changes: 2 additions & 0 deletions thresher_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
# host mounted volume. Devs should 'npm run build' inside the container only.
if os.environ.get("WEBPACK_BUILD_DIR"):
BASE_DIR = os.environ.get("WEBPACK_BUILD_DIR")
HIGHLIGHTER_BUNDLE_URLPATH = '/highlight.bundle.js'
HIGHLIGHTER_BUNDLE_JS = os.path.join(BASE_DIR, 'dist/highlight.bundle.js')
QUIZ_BUNDLE_URLPATH = '/quiz.bundle.js'
QUIZ_BUNDLE_JS = os.path.join(BASE_DIR, 'dist/quiz.bundle.js')

# REST Framework settings
Expand Down

0 comments on commit e6ce921

Please sign in to comment.