Skip to content

Commit

Permalink
House-keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMadejski committed Nov 24, 2019
1 parent ff6a4b4 commit 9db1db1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ It uses email as an unique key and supports a range of authentication methods th
```python
from moonsheep.views import ChooseNicknameView

urlpatterns = [an url
urlpatterns = [
# name needs to be set to 'choose-nickname'
path('get-a-nickname', ChooseNicknameView.as_view(template_name='nickname.html'), name='choose-nickname'),
]
Expand Down
4 changes: 0 additions & 4 deletions moonsheep/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from django.core.validators import RegexValidator, ValidationError


class NewTaskForm(forms.Form):
url = forms.URLField(label="Report URL")


class MultipleRangeField(forms.CharField):
default_validators = [RegexValidator(regex="[\w\d]+[\w]?(-[\w\d]+[\w]?)?(,[\w\d]+[\w]?(-[\w\d]+[\w]?)?)*")]

Expand Down
4 changes: 2 additions & 2 deletions moonsheep/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
'MIN_ENTRIES_TO_CROSSCHECK': 3,
'MIN_ENTRIES_TO_MARK_DIRTY': 4,
'FAKER_LOCALE': 'it_IT', # See supported locales at https://github.com/joke2k/faker#localization
'USER_AUTHENTICATION': 'nickname' # available settings: 'nickname', 'anonymous', TODO email #60
# 'APP': 'myapp' # needs to be set in project
'USER_AUTHENTICATION': 'anonymous' # available settings: 'nickname', 'anonymous', TODO email #60
# 'APP': 'myapp' # needs to be set in project # TODO (should not be set at all)
}

AUTH_USER_MODEL = 'moonsheep.User'
Expand Down
2 changes: 1 addition & 1 deletion moonsheep/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AbstractTask(object):
def __init__(self, instance: Task):
self.instance = instance
self.id = instance.id # TODO attr rather than field
self.params = instance.params
self.params = instance.params # TODO attr rather than field

# per-instance overrides
if 'task_form' in self.params:
Expand Down
6 changes: 3 additions & 3 deletions moonsheep/templates/error-messages/no-tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<li> Provide tasks
<ol>
<li>For development of forms you might want to enable rotating through available task types by setting
<code>MOONSHEEP['DEV_ROTATE_TASKS'] = true</code>
<code>MOONSHEEP['DEV_ROTATE_TASKS'] = True</code>
</li>
<li><a href="{% url "ms-new-task" %}">In production you want to import tasks to the system</a>
and also come up with a better error template for your users than this
<li><a href="{% url "documents" %}">In production you want to import tasks to the system</a>
and also come up with a better error template for your users than this (TODO how to do it <!-- TODO configurable error_template or error handling --> )
</li>
</ol>
</li>
Expand Down
95 changes: 2 additions & 93 deletions moonsheep/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from unittest.mock import MagicMock, patch, sentinel, call

from moonsheep.exceptions import PresenterNotDefined, TaskMustSetTemplate, NoTasksLeft
from moonsheep.forms import NewTaskForm, MultipleRangeField
from moonsheep.forms import MultipleRangeField
from moonsheep.mapper import ModelMapper
from moonsheep.tasks import AbstractTask
from moonsheep.verifiers import equals, OrderedListVerifier
from moonsheep.views import unpack_post, TaskView, NewTaskFormView
from moonsheep.views import unpack_post, TaskView


# TODO dev env: `DJANGO_SETTINGS_MODULE=moonsheep.tests.migrations_settings django-admin makemigrations` should return that everything is up to date
Expand Down Expand Up @@ -84,9 +84,6 @@

# TODO test error handling for Tasks with no form and no template

# views.py
PYBOSSA_PROJECT_ID = 1


def setup_view(view, request, *args, **kwargs):
"""
Expand All @@ -112,10 +109,8 @@ def setUp(self):
'task_form': 'myapp.forms.TaskForm'
}
}
self.pybossa_project_id = 1
self.task_id = 1
self.post_data = {
'_project_id': self.pybossa_project_id,
'_task_id': self.task_id
}

Expand Down Expand Up @@ -302,9 +297,6 @@ def test_get_context_data(
context = view.get_context_data()
self.assertIsInstance(context['task'], AbstractTask)
self.assertEqual(context['task'], view.task)
self.assertEqual(context['project_id'], PYBOSSA_PROJECT_ID)
from moonsheep.settings import PYBOSSA_BASE_URL
self.assertEqual(context['pybossa_url'], PYBOSSA_BASE_URL)

@patch('moonsheep.views.TaskView._get_task')
@patch('moonsheep.views.TaskView.initialize_task_data')
Expand Down Expand Up @@ -337,9 +329,6 @@ def test_get_context_data_no_task(
self.assertTrue(context['error'])
self.assertEqual(context['message'], 'Sample error message')
self.assertEqual(context['template'], 'Sample error template')
self.assertEqual(context['project_id'], PYBOSSA_PROJECT_ID)
from moonsheep.settings import PYBOSSA_BASE_URL
self.assertEqual(context['pybossa_url'], PYBOSSA_BASE_URL)

@patch('moonsheep.models.klass_from_name')
def test_initialize_task_data(
Expand Down Expand Up @@ -408,7 +397,6 @@ def test_get_task_new(
# # TODO: FIXME
# @patch('pbclient.get_task')
# @patch('moonsheep.tasks.AbstractTask.create_task_instance')
# @override_settings(TASK_SOURCE=PYBOSSA_SOURCE)
# def test_get_task_old_not_development(
# self,
# create_task_instance_mock: MagicMock,
Expand Down Expand Up @@ -441,8 +429,6 @@ def test_form_valid(self):

# TODO: FIXME
# @patch('moonsheep.views.TaskView.get_random_pybossa_task')
# @override_settings(TASK_SOURCE=PYBOSSA_SOURCE)
# @patch('moonsheep.settings.TASK_SOURCE', PYBOSSA_SOURCE)
# def test_get_new_task_random_pybossa_task(self, get_random_pybossa_task_mock: MagicMock):
# from moonsheep.views import TaskView
# request = self.factory.get(self.fake_path)
Expand All @@ -467,7 +453,6 @@ def test_form_valid(self):
# TODO: test tasks rotation
def test_get_random_mocked_task_data(self):
request = self.factory.get(self.fake_path)
base_task.register(AbstractTask)
view = TaskView()
view = setup_view(view, request)
task = view.get_random_mocked_task_data()
Expand All @@ -479,7 +464,6 @@ def test_get_random_mocked_task_data(self):
},
'id': 'moonsheep.tasks.AbstractTask'
})
base_task.clear()

def test_get_random_mocked_task_data_no_registry(self):
request = self.factory.get(self.fake_path)
Expand Down Expand Up @@ -512,81 +496,6 @@ def test_send_task(self):
pass


@override_settings(ROOT_URLCONF='moonsheep.urls')
class NewTaskFormViewTest(DjangoTestCase):
def setUp(self):
self.factory = RequestFactory()
self.client = Client()
self.path = reverse('ms-new-task')
self.pybossa_project_id = 1
self.task_id = 1

def test_get_success_url(self):
request = self.factory.get(self.path)
view = NewTaskFormView()
view.request = request
success_url = view.get_success_url()
self.assertEquals(success_url, self.path)

@patch('pbclient.set')
@patch('pbclient.create_task')
def test_form_valid_no_registry(
self,
create_task_mock: MagicMock,
set_mock: MagicMock
):
request = self.factory.get(self.path)
view = NewTaskFormView()
view.request = request
form_data = {
'url': 'http://byleco.pl'
}
form = NewTaskForm(form_data)
form.full_clean()
with self.assertRaises(ImproperlyConfigured):
view.form_valid(form)

@patch('pbclient.set')
@patch('pbclient.create_task')
def test_form_valid(
self,
create_task_mock: MagicMock,
set_mock: MagicMock
):
request = self.factory.get(self.path)
initial_task.register(AbstractTask)
view = NewTaskFormView()
view.request = request
form_data = {
'url': 'http://byleco.pl'
}
form = NewTaskForm(form_data)
form.full_clean()
success_url = view.form_valid(form).url
self.assertEquals(success_url, self.path)
initial_task.clear()
# set_mock.assert_has_calls([call('endpoint', settings.PY)])
# create_task_mock.assert_any_call({
# 'project_id': PYBOSSA_PROJECT_ID,
# 'info': {
# 'type':
# }
# })

# def test_form_valid_no_base_tasks(self):


class WebhookTaskRunViewTest(UnitTestCase):
def test_dispatch(self):
pass

def test_get(self):
pass

def test_post(self):
pass


class UnpackPostTest(UnitTestCase):
"""
It tests moonsheep.views.unpack_post function.
Expand Down
9 changes: 5 additions & 4 deletions moonsheep/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@


class TaskView(UserRequiredMixin, FormView):
task_type = None
task_type: AbstractTask = None
template_name: str = None
form_class = None
error_message = None
error_template = None
Expand All @@ -49,16 +50,16 @@ def get(self, request, *args, **kwargs):
try:
self.task_type = self._get_task()
self.configure_template_and_form()
except NoTasksLeft:
except NoTasksLeft: # TODO test case for project
self.error_message = 'Task Chooser returned no tasks'
self.error_template = 'error-messages/no-tasks.html'
self.task_type = None
self.template_name = 'views/message.html'
self.template_name = 'error-messages/no-tasks.html' # TODO we do not want to define the main err template in Moonsheep. How to generally handle messages and errors?
except PresenterNotDefined:
self.error_message = 'Presenter not defined'
self.error_template = 'error-messages/presenter-not-defined.html'
self.task_type = None
self.template_name = 'views/message.html'
self.template_name = 'error-messages/presenter-not-defined.html' # TODO we do not want to main err template it in Moonsheep. How to generally handle messages and errors?

context = self.get_context_data(**kwargs)
return self.render_to_response(context)
Expand Down

0 comments on commit 9db1db1

Please sign in to comment.