From 092f0365a591103e3d3d830edfbe872cdf38b5bb Mon Sep 17 00:00:00 2001 From: Conrad S Date: Mon, 8 May 2017 16:47:20 -0400 Subject: [PATCH 1/2] Add expire hit endpoint --- app/controllers/turk_hit.py | 46 +++++++++++++++++++++++++++++++++++-- app/forms/turk_hit.py | 21 +++++++++++++++++ app/lib/mech_turk.py | 8 +++++++ app/views/turk_hit.py | 12 +++++++++- 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/app/controllers/turk_hit.py b/app/controllers/turk_hit.py index 69d6e0b..1809a41 100644 --- a/app/controllers/turk_hit.py +++ b/app/controllers/turk_hit.py @@ -15,10 +15,10 @@ from .unapproved_window_door import UnapprovedWindowDoorController from .unapproved_building_dimensions import UnapprovedBuildingDimensionsController from ..lib.database import proc, db from ..lib.mech_turk import create_hit, approve_or_reject_hit,\ - get_hit_status + get_hit_status, expire_hit from ..lib.service import services from ..models.turk_hit import TurkHit -from ..forms.turk_hit import TurkHitPostForm, TurkHitPutForm +from ..forms.turk_hit import TurkHitPostForm, TurkHitPutForm, TurkHitDeleteForm class TurkHitController(RestController): @@ -46,6 +46,10 @@ class TurkHitController(RestController): """Return the turk_hit form.""" return TurkHitPutForm + def get_delete_form(self, filter_data): + """Return the turk_hit form.""" + return TurkHitDeleteForm + def get(self, id_, filter_data): """ Retrieve a list of TurkHit objects @@ -374,3 +378,41 @@ class TurkHitController(RestController): return hit_list[0] else: raise BadRequest("There were no completed assignments to approve or reject") + + def delete(self, id_, data, filter_data): + """ + Expire a TurkHit object + + Args: + id_ (str) db_id of a hit + + Returns; + dict TurkHit objet + + """ + form = self.get_delete_form(filter_data)(formdata=MultiDict(data)) + if not form.validate(): + raise BadRequest(form.errors) + if int(id_) != data["db_id"]: + raise BadRequest("The URL id and the body db_id are not the same") + + expire_hit(data["amazon_hit_id"]) + + status_id = self.STATUS_DICT_TEXT['Expired'] + clean_model = self.Model() + form.populate_obj(clean_model) + clean_data = clean_model.get_dictionary() + clean_data['status_id'] = status_id + + hit_list = proc( + self.Model, + self.Model.PROCS['UPDATE'], + **clean_data, + ) + return hit_list[0] + + + + + + diff --git a/app/forms/turk_hit.py b/app/forms/turk_hit.py index 1699e91..01161ac 100644 --- a/app/forms/turk_hit.py +++ b/app/forms/turk_hit.py @@ -69,3 +69,24 @@ class TurkHitPutForm(wtf.Form): self.response_message.errors.append('If accept is 0 explanation must be nonempty') return False return True + +class TurkHitDeleteForm(wtf.Form): + """ A form for validating turk hits.""" + db_id = wtf.IntegerField( + validators=[wtf.validators.Required()]) + building_id = wtf.IntegerField( + validators=[wtf.validators.Required()]) + amazon_hit_id = wtf.StringField( + validators=[wtf.validators.Required()]) + status_id = wtf.IntegerField( + validators=[wtf.validators.Required()]) + hit_date = wtf.StringField( + validators=[wtf.validators.Optional()]) + requester_name = wtf.StringField( + validators=[wtf.validators.Optional()]) + csv_document_key = wtf.StringField( + validators=[wtf.validators.Optional()]) + shapefile_document_key = wtf.StringField( + validators=[wtf.validators.Optional()]) + response_message = wtf.StringField( + validators=[wtf.validators.Optional()]) diff --git a/app/lib/mech_turk.py b/app/lib/mech_turk.py index 83a3341..2c8983a 100644 --- a/app/lib/mech_turk.py +++ b/app/lib/mech_turk.py @@ -116,3 +116,11 @@ def approve_or_reject_hit(amazon_hit_id, approve, response_message): mturk_connection.reject_assignment(assignment.AssignmentId, feedback=response_message) return True return False + +def expire_hit(amazon_hit_id): + """ + Expire the hit associated with the amazon hit id + """ + mturk_connection = get_mturk_connection() + mturk_connection.expire_hit(amazon_hit_id) + return True diff --git a/app/views/turk_hit.py b/app/views/turk_hit.py index f2e0f3a..8b15862 100644 --- a/app/views/turk_hit.py +++ b/app/views/turk_hit.py @@ -60,4 +60,14 @@ class TurkHitView(RestView): return self.json(self.parse(response)) def delete(self, id_): - raise MethodNotAllowed() + try: + response = self.get_controller().delete( + id_, + self.request_json(), + request.args + ) + # Catch an MTurkRequestError (usually AWS) and return error code + except MTurkRequestError as error: + raise BadGateway(error.error_code) + + return self.json(self.parse(response), 200) -- GitLab From 4f9264d8578d12ca7e3c34fbd070139157616273 Mon Sep 17 00:00:00 2001 From: Conrad S Date: Tue, 9 May 2017 15:10:39 -0400 Subject: [PATCH 2/2] Use inheritence in turk_hit forms --- app/forms/turk_hit.py | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/app/forms/turk_hit.py b/app/forms/turk_hit.py index 01161ac..c8990f3 100644 --- a/app/forms/turk_hit.py +++ b/app/forms/turk_hit.py @@ -37,7 +37,7 @@ class TurkHitPostForm(wtf.Form): validators=[wtf.validators.Required()]) -class TurkHitPutForm(wtf.Form): +class TurkHitDeleteForm(wtf.Form): """ A form for validating turk hits.""" db_id = wtf.IntegerField( validators=[wtf.validators.Required()]) @@ -58,6 +58,8 @@ class TurkHitPutForm(wtf.Form): response_message = wtf.StringField( validators=[wtf.validators.Optional()]) +class TurkHitPutForm(TurkHitDeleteForm): + """ A form for validating turk hits.""" approve = wtf.IntegerField( validators=[wtf.validators.AnyOf([0, 1])]) @@ -70,23 +72,3 @@ class TurkHitPutForm(wtf.Form): return False return True -class TurkHitDeleteForm(wtf.Form): - """ A form for validating turk hits.""" - db_id = wtf.IntegerField( - validators=[wtf.validators.Required()]) - building_id = wtf.IntegerField( - validators=[wtf.validators.Required()]) - amazon_hit_id = wtf.StringField( - validators=[wtf.validators.Required()]) - status_id = wtf.IntegerField( - validators=[wtf.validators.Required()]) - hit_date = wtf.StringField( - validators=[wtf.validators.Optional()]) - requester_name = wtf.StringField( - validators=[wtf.validators.Optional()]) - csv_document_key = wtf.StringField( - validators=[wtf.validators.Optional()]) - shapefile_document_key = wtf.StringField( - validators=[wtf.validators.Optional()]) - response_message = wtf.StringField( - validators=[wtf.validators.Optional()]) -- GitLab