Example #1
0
    def get_ajax(self, request, *args, **kwargs):
        try:
            doc_id = request.GET.get('docid')
            doc_id = str(doc_id)
        except KeyError:
            return JsonResponse({"message": "Docid was not passed."}, status=400)

        l = []
        doc = {'doc_id': doc_id}
        if '.' in doc_id:
            doc['doc_id'], doc['para_id'] = doc_id.split('.')
        l.append(doc)
        seed_query = self.request.user.current_session.topic.seed_query
        if 'doc' in self.request.user.current_session.strategy:
            result = DocEngine.get_documents([doc_id], seed_query)
        else:
            result = DocEngine.get_documents_with_snippet(l, seed_query)

        exists = Judgment.objects.filter(doc_id=doc_id,
                                         user=self.request.user,
                                         session=self.request.user.current_session
                                         )
        if exists:
            result[0]["rel"] = exists.first().relevance
            result[0]["additional_judging_criteria"] = exists.first().additional_judging_criteria

        return self.render_json_response(result)
Example #2
0
    def get_ajax(self, request, *args, **kwargs):
        session = self.request.user.current_task.uuid
        seed_query = self.request.user.current_task.topic.seed_query
        try:
            docs_ids_to_judge = CALFunctions.get_documents(str(session), 5,
                                                           seed_query)
            if not docs_ids_to_judge:
                return self.render_json_response([])

            doc_ids_hack = []
            for doc_id in docs_ids_to_judge:
                doc = {'doc_id': doc_id}
                if '.' in doc_id:
                    doc['doc_id'], doc['para_id'] = doc_id.split('.')
                doc_ids_hack.append(doc)

            if 'doc' in self.request.user.current_task.strategy:
                documents = DocEngine.get_documents(docs_ids_to_judge,
                                                    self.request.user.current_task.topic.seed_query)
            else:
                documents = DocEngine.get_documents_with_snippet(doc_ids_hack,
                                                    self.request.user.current_task.topic.seed_query)

            return self.render_json_response(documents)
        except TimeoutError:
            error_dict = {u"message": u"Timeout error. Please check status of servers."}
            return self.render_timeout_request_response(error_dict)
        except CALError as e:
            error_dict = {u"message": u"Error occurred."}

            # TODO: add proper http response for CAL errors
            return self.render_timeout_request_response(error_dict)
Example #3
0
    def get_ajax(self, request, *args, **kwargs):
        seed_query = self.request.user.current_session.topic.seed_query

        try:
            latest = Judgment.objects.filter(
                user=self.request.user,
                session=self.request.user.current_session,
            ).filter(relevance__isnull=False).order_by('-relevance')

            next_patch_ids = []
            for judgment in latest:
                next_patch_ids.append(judgment.doc_id)

            doc_ids_hack = []
            for doc_id in next_patch_ids:
                doc = {'doc_id': doc_id}
                if '.' in doc_id:
                    doc['doc_id'], doc['para_id'] = doc_id.split('.')
                doc_ids_hack.append(doc)

            if 'doc' in self.request.user.current_session.strategy:
                documents = DocEngine.get_documents(next_patch_ids, seed_query)
            else:
                documents = DocEngine.get_documents_with_snippet(
                    doc_ids_hack, seed_query)

            return self.render_json_response(documents)

        except TimeoutError:
            error_dict = {
                u"message": u"Timeout error. Please check status of servers."
            }
            return self.render_timeout_request_response(error_dict)
Example #4
0
    def get_ajax(self, request, *args, **kwargs):
        session = self.request.user.current_task.uuid
        seed_query = self.request.user.current_task.topic.seed_query
        try:
            full_response = CALFunctions.get_documents(str(session), 5, seed_query)
            docs_ids_to_judge, top_terms = full_response['docs'], full_response['top-terms']
            test_scores = full_response['test-scores']
            model_number = full_response['model-number']
            top_model_terms = full_response['top-model-terms']

            if not docs_ids_to_judge:
                return self.render_json_response([])

            ret = {}
            next_patch_ids = []
            for doc_id_score in docs_ids_to_judge:
                doc_id, doc_score = doc_id_score['doc_id'], doc_id_score['score']
                ret[doc_id] = doc_score
                next_patch_ids.append(doc_id)

            doc_ids_hack = []
            for doc_id in next_patch_ids:
                doc = {'doc_id': doc_id}
                if '.' in doc_id:
                    doc['doc_id'], doc['para_id'] = doc_id.split('.')
                doc_ids_hack.append(doc)

            if 'doc' in self.request.user.current_task.strategy:
                documents = DocEngine.get_documents(next_patch_ids,
                                                    self.request.user.current_task.topic.seed_query)
            else:
                documents = DocEngine.get_documents_with_snippet(doc_ids_hack,
                                                    self.request.user.current_task.topic.seed_query)

            for i in range(len(documents)):
                id = documents[i]['doc_id']
                if id in ret:
                    documents[i]['score'] = json.dumps({"score": str(-1 * ret[id]), "model-no": model_number, "test-scores": test_scores, "top-model-terms": top_model_terms})
                    documents[i]['top_terms'] = json.dumps(top_terms[id])
                    documents[i]['content'] = DocEngine.add_highlighting(documents[i]['content'], top_terms[id])
                else:
                    documents[i]['score'] = "1"
                    documents[i]['top_terms'] = json.dumps({})

            return self.render_json_response(documents)
        except TimeoutError:
            error_dict = {u"message": u"Timeout error. Please check status of servers."}
            return self.render_timeout_request_response(error_dict)
        except CALError as e:
            error_dict = {u"message": u"Error occurred."}

            # TODO: add proper http response for CAL errors
            return self.render_timeout_request_response(error_dict)
Example #5
0
 def get_ajax(self, request, *args, **kwargs):
     try:
         current_task = self.request.user.current_task
         docs_ids = IterativeEngine.get_documents(current_task.topic.number)
         docs_ids = helpers.remove_judged_docs(docs_ids, self.request.user,
                                               current_task)
         documents = DocEngine.get_documents(docs_ids, query=None)
         return self.render_json_response(documents)
     except TimeoutError:
         error_dict = {
             u"message": u"Timeout error. Please check status of servers."
         }
         return self.render_timeout_request_response(error_dict)
Example #6
0
    def get_ajax(self, request, *args, **kwargs):
        session = self.request.user.current_session
        seed_query = self.request.user.current_session.topic.seed_query
        try:
            docids_to_judge, top_terms = CALFunctions.get_documents(
                str(session.uuid), 10)
            if not docids_to_judge:
                return self.render_json_response([])

            ret = {}
            next_patch_ids = []
            for docid_score_pair in docids_to_judge:
                doc_id, doc_score = docid_score_pair.rsplit(':', 1)
                ret[doc_id] = doc_score
                next_patch_ids.append(doc_id)

            doc_ids_hack = []
            for doc_id in next_patch_ids:
                doc = {'doc_id': doc_id}
                if '.' in doc_id:
                    doc['doc_id'], doc['para_id'] = doc_id.split('.')
                doc_ids_hack.append(doc)

            if 'doc' in self.request.user.current_session.strategy:
                documents = DocEngine.get_documents(next_patch_ids, seed_query,
                                                    top_terms)
            else:
                documents = DocEngine.get_documents_with_snippet(
                    doc_ids_hack, seed_query, top_terms)

            return self.render_json_response(documents)
        except TimeoutError:
            error_dict = {
                u"message": u"Timeout error. Please check status of servers."
            }
            return self.render_timeout_request_response(error_dict)
        except CALServerSessionNotFoundError:
            message = "Ops! Session is not found in CAL. "
            if "scal" not in self.request.user.current_session.strategy:
                seed_judgments = Judgment.objects.filter(
                    user=self.request.user,
                    session=session).filter(relevance__isnull=False)
                strategy = self.request.user.current_session.strategy
                CALFunctions.restore_session(session.uuid, seed_query,
                                             seed_judgments, strategy)
                message += "Restoring.. Please try in few minutes."
            return JsonResponse({"message": message}, status=404)
        except CALError as e:
            return JsonResponse({"message": "Ops! CALError."}, status=404)
Example #7
0
    def get_ajax(self, request, *args, **kwargs):
        session = self.request.user.current_session.uuid
        seed_query = self.request.user.current_session.topic.seed_query
        try:
            docids_to_judge, top_terms = CALFunctions.get_documents(
                str(session), 10)
            if not docids_to_judge:
                return self.render_json_response([])

            ret = {}
            next_patch_ids = []
            for docid_score_pair in docids_to_judge:
                doc_id, doc_score = docid_score_pair.split(':')
                ret[doc_id] = doc_score
                next_patch_ids.append(doc_id)

            doc_ids_hack = []
            for doc_id in next_patch_ids:
                doc = {'doc_id': doc_id}
                if '.' in doc_id:
                    doc['doc_id'], doc['para_id'] = doc_id.split('.')
                doc_ids_hack.append(doc)

            if 'doc' in self.request.user.current_session.strategy:
                documents = DocEngine.get_documents(next_patch_ids, seed_query,
                                                    top_terms)
            else:
                documents = DocEngine.get_documents_with_snippet(
                    doc_ids_hack, seed_query, top_terms)

            return self.render_json_response(documents)
        except TimeoutError:
            error_dict = {
                u"message": u"Timeout error. Please check status of servers."
            }
            return self.render_timeout_request_response(error_dict)
        except CALError as e:
            return JsonResponse({"message": "Ops! CALError."}, status=404)
Example #8
0
    def post(self, request, *args, **kwargs):
        try:
            doc_id = self.request_json[u"doc_id"]
            doc_title = self.request_json[u"doc_title"]
            doc_CAL_snippet = self.request_json[u"doc_CAL_snippet"]
            doc_search_snippet = self.request_json[u"doc_search_snippet"]
            relevance = self.request_json[u"relevance"]
            source = self.request_json[u"source"]
            method = self.request_json[u"method"]
            query = self.request_json.get(u"query", None)
            client_time = self.request_json.get(u"client_time", None)
            timeVerbose = self.request_json.get(u"timeVerbose")
            search_query = self.request_json[u"search_query"]
            ctrl_f_terms_input = self.request_json[u"ctrl_f_terms_input"]
            found_ctrl_f_terms_in_title = self.request_json[
                u"found_ctrl_f_terms_in_title"]
            found_ctrl_f_terms_in_summary = self.request_json[
                u"found_ctrl_f_terms_in_summary"]
            found_ctrl_f_terms_in_full_doc = self.request_json[
                u"found_ctrl_f_terms_in_full_doc"]
        except KeyError:
            error_dict = {
                u"message":
                u"your input must include doc_id, doc_title, "
                u"relevance, etc.."
            }

            return self.render_bad_request_response(error_dict)

        # Check if a judgment exists already, if so, update the db row.
        exists = Judgment.objects.filter(user=self.request.user,
                                         doc_id=doc_id,
                                         task=self.request.user.current_task)

        if exists:
            exists = exists.first()
            exists.query = query
            exists.doc_title = doc_title
            if doc_CAL_snippet != "":
                exists.doc_CAL_snippet = doc_CAL_snippet
            if doc_search_snippet != "":
                exists.doc_search_snippet = doc_search_snippet
            exists.relevance = relevance
            exists.source = source
            exists.method = method
            exists.timeVerbose.append(timeVerbose)
            exists.search_query = search_query
            exists.ctrl_f_terms_input = ctrl_f_terms_input
            exists.found_ctrl_f_terms_in_title = found_ctrl_f_terms_in_title
            exists.found_ctrl_f_terms_in_summary = found_ctrl_f_terms_in_summary
            exists.found_ctrl_f_terms_in_full_doc = found_ctrl_f_terms_in_full_doc
            exists.save()

        else:
            Judgment.objects.create(
                user=self.request.user,
                doc_id=doc_id,
                doc_title=doc_title,
                doc_CAL_snippet=doc_CAL_snippet,
                doc_search_snippet=doc_search_snippet,
                task=self.request.user.current_task,
                query=query,
                relevance=relevance,
                source=source,
                method=method,
                timeVerbose=[timeVerbose],
                search_query=search_query,
                ctrl_f_terms_input=ctrl_f_terms_input,
                found_ctrl_f_terms_in_title=found_ctrl_f_terms_in_title,
                found_ctrl_f_terms_in_summary=found_ctrl_f_terms_in_summary,
                found_ctrl_f_terms_in_full_doc=found_ctrl_f_terms_in_full_doc)

        context = {
            u"message":
            u"Your judgment on {} has been received!".format(doc_id),
            u"is_max_judged_reached": False
        }
        error_message = None

        is_from_cal = source == "CAL"
        is_from_search = "search" in source
        # mark relevant documents as 1 to CAL.
        rel_CAL = -1 if relevance <= 0 else 1

        if is_from_cal:
            context[u"next_docs"] = []
            try:
                next_patch = CALFunctions.send_judgment(
                    self.request.user.current_task.uuid, doc_id, rel_CAL)
                if not next_patch:
                    return self.render_json_response(context)

                doc_ids_hack = []
                for doc_id in next_patch:
                    doc = {'doc_id': doc_id}
                    if '.' in doc_id:
                        doc['doc_id'], doc['para_id'] = doc_id.split('.')
                    doc_ids_hack.append(doc)

                if 'doc' in self.request.user.current_task.strategy:
                    documents = DocEngine.get_documents(
                        next_patch,
                        self.request.user.current_task.topic.seed_query)
                else:
                    documents = DocEngine.get_documents_with_snippet(
                        doc_ids_hack,
                        self.request.user.current_task.topic.seed_query)
                context[u"next_docs"] = documents
            except TimeoutError:
                error_dict = {
                    u"message":
                    u"Timeout error. "
                    u"Please check status of servers."
                }
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                error_message = str(e)

        elif is_from_search:
            try:
                CALFunctions.send_judgment(self.request.user.current_task.uuid,
                                           doc_id, rel_CAL)
            except TimeoutError:
                traceback.print_exc()
                error_dict = {
                    u"message":
                    u"Timeout error. "
                    u"Please check status of servers."
                }
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                traceback.print_exc()
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                traceback.print_exc()
                error_message = str(e)

        if error_message:
            log_body = {
                "user": self.request.user.username,
                "client_time": client_time,
                "result": {
                    "message": error_message,
                    "action": "create",
                    "doc_judgment": {
                        "doc_id":
                        doc_id,
                        "doc_title":
                        doc_title,
                        "topic_number":
                        self.request.user.current_task.topic.number,
                        "session":
                        str(self.request.user.current_task.uuid),
                        "query":
                        query,
                        "relevance":
                        relevance,
                        "source":
                        source,
                        "method":
                        method,
                        "timeVerbose":
                        timeVerbose,
                        "search_query":
                        search_query,
                        "ctrl_f_terms_input":
                        ctrl_f_terms_input,
                        "found_ctrl_f_terms_in_title":
                        found_ctrl_f_terms_in_title,
                        "found_ctrl_f_terms_in_summary":
                        found_ctrl_f_terms_in_summary,
                        "found_ctrl_f_terms_in_full_doc":
                        found_ctrl_f_terms_in_full_doc
                    }
                }
            }

            logger.error("[{}]".format(json.dumps(log_body)))

        if not exists:
            # Check if user has judged `max_judged` documents in total.
            judgements = Judgment.objects.filter(
                user=self.request.user, task=self.request.user.current_task)
            max_judged = self.request.user.current_task.max_number_of_judgments
            # Exit task only if number of judgments reached max (and maxjudged is enabled)
            if len(judgements) >= max_judged > 0:
                self.request.user.current_task = None
                self.request.user.save()

                message = 'You have judged >={} (max number of judgment you have ' \
                          'specified for this task) documents.'.format(max_judged)
                messages.add_message(request, messages.SUCCESS, message)
                context[u"is_max_judged_reached"] = True

        return self.render_json_response(context)
Example #9
0
    def post(self, request, *args, **kwargs):
        try:
            doc_id = self.request_json[u"doc_id"]
            doc_title = self.request_json[u"doc_title"]
            doc_CAL_snippet = self.request_json.get(u"doc_CAL_snippet", None)
            doc_search_snippet = self.request_json.get(u"doc_search_snippet", None)
            relevance = self.request_json[u"relevance"]
            source = self.request_json[u"source"]
            method = self.request_json.get(u"method", None)
            query = self.request_json.get(u"query", None)
            client_time = self.request_json.get(u"client_time", None)
            historyItem = self.request_json.get(u"historyItem")
            search_query = self.request_json.get(u"search_query", None)
            ctrl_f_terms_input = self.request_json.get(u"ctrl_f_terms_input", None)
            found_ctrl_f_terms_in_title = self.request_json.get(u"found_ctrl_f_terms_in_title", None)
            found_ctrl_f_terms_in_summary = self.request_json.get(u"found_ctrl_f_terms_in_summary", None)
            found_ctrl_f_terms_in_full_doc = self.request_json.get(u"found_ctrl_f_terms_in_full_doc", None)
        except KeyError:
            error_dict = {u"message": u"POST input missing important fields"}

            return self.render_bad_request_response(error_dict)

        # Check if a judgment exists already, if so, update the db row.
        exists = Judgment.objects.filter(user=self.request.user,
                                         doc_id=doc_id,
                                         session=self.request.user.current_session)

        if exists:
            exists = exists.first()
            exists.query = query
            exists.doc_title = doc_title
            if doc_CAL_snippet != "":
                exists.doc_CAL_snippet = doc_CAL_snippet
            if doc_search_snippet != "":
                exists.doc_search_snippet = doc_search_snippet
            exists.relevance = relevance
            exists.source = source
            exists.method = method
            exists.historyVerbose.append(historyItem)
            exists.search_query = search_query
            exists.ctrl_f_terms_input = ctrl_f_terms_input
            exists.found_ctrl_f_terms_in_title = found_ctrl_f_terms_in_title
            exists.found_ctrl_f_terms_in_summary = found_ctrl_f_terms_in_summary
            exists.found_ctrl_f_terms_in_full_doc = found_ctrl_f_terms_in_full_doc
            exists.save()

        else:
            Judgment.objects.create(
                user=self.request.user,
                doc_id=doc_id,
                doc_title=doc_title,
                doc_CAL_snippet=doc_CAL_snippet,
                doc_search_snippet=doc_search_snippet,
                session=self.request.user.current_session,
                query=query,
                relevance=relevance,
                source=source,
                method=method,
                historyVerbose=[historyItem],
                search_query=search_query,
                ctrl_f_terms_input=ctrl_f_terms_input,
                found_ctrl_f_terms_in_title=found_ctrl_f_terms_in_title,
                found_ctrl_f_terms_in_summary=found_ctrl_f_terms_in_summary,
                found_ctrl_f_terms_in_full_doc=found_ctrl_f_terms_in_full_doc
            )

        context = {u"message": u"Your judgment on {} has been received!".format(doc_id),
                   u"is_max_judged_reached": False}
        error_message = None

        is_from_cal = source == "CAL"
        is_from_search = "search" in source
        # mark relevant documents as 1 to CAL.
        rel_CAL = -1 if relevance <= 0 else 1

        if rel_CAL == 0 and self.request.user.current_session.is_shared:
            # Check if someone else in the shared session has marked it as rel
            # if so, cant re-train CAL with this doc as non-relevant
            others_judged_as_rel = Judgment.objects.filter(
                session=self.request.user.current_session,
                relevance__lgt=0
            ).exclude(user=self.request.user).exists()
            if others_judged_as_rel:
                rel_CAL = 1

        if is_from_cal:
            context[u"next_docs"] = []

            try:
                next_patch, top_terms = CALFunctions.send_judgment(
                    self.request.user.current_session.uuid,
                    doc_id,
                    rel_CAL)
                if not next_patch:
                    return self.render_json_response(context)

                ret = {}
                next_patch_ids = []
                for docid_score_pair in next_patch:
                    doc_id, doc_score = docid_score_pair.split(':')
                    ret[doc_id] = doc_score
                    next_patch_ids.append(doc_id)

                doc_ids_hack = []
                for doc_id in next_patch_ids:
                    doc = {'doc_id': doc_id}
                    if '.' in doc_id:
                        doc['doc_id'], doc['para_id'] = doc_id.split('.')
                    doc_ids_hack.append(doc)
                seed_query = self.request.user.current_session.topic.seed_query
                if 'doc' in self.request.user.current_session.strategy:
                    documents = DocEngine.get_documents(next_patch_ids,
                                                        seed_query,
                                                        top_terms)
                else:
                    documents = DocEngine.get_documents_with_snippet(doc_ids_hack,
                                                                     seed_query,
                                                                     top_terms)
                context[u"next_docs"] = documents
            except TimeoutError:
                error_dict = {u"message": u"Timeout error. "
                                          u"Please check status of servers."}
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                error_message = str(e)

        elif is_from_search:
            try:
                CALFunctions.send_judgment(self.request.user.current_session.uuid,
                                           doc_id,
                                           rel_CAL)
            except TimeoutError:
                traceback.print_exc()
                error_dict = {u"message": u"Timeout error. "
                                          u"Please check status of servers."}
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                traceback.print_exc()
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                traceback.print_exc()
                error_message = str(e)

        if error_message:
            log_body = {
                "user": self.request.user.username,
                "client_time": client_time,
                "result": {
                    "message": error_message,
                    "action": "create",
                    "doc_judgment": {
                        "doc_id": doc_id,
                        "doc_title": doc_title,
                        "topic_number": self.request.user.current_session.topic.number,
                        "session": str(self.request.user.current_session.uuid),
                        "query": query,
                        "relevance": relevance,
                        "source": source,
                        "method": method,
                        "historyVerbose": historyItem,
                        "search_query": search_query,
                        "ctrl_f_terms_input": ctrl_f_terms_input,
                        "found_ctrl_f_terms_in_title": found_ctrl_f_terms_in_title,
                        "found_ctrl_f_terms_in_summary": found_ctrl_f_terms_in_summary,
                        "found_ctrl_f_terms_in_full_doc": found_ctrl_f_terms_in_full_doc
                    }
                }
            }

            logger.error("[{}]".format(json.dumps(log_body)))

        if not exists:
            # Check if user has judged `max_judged` documents in total.
            judgements = Judgment.objects.filter(user=self.request.user,
                                                 session=self.request.user.current_session)
            max_judged = self.request.user.current_session.max_number_of_judgments
            # Exit task only if number of judgments reached max (and maxjudged is enabled)
            if len(judgements) >= max_judged > 0:
                self.request.user.current_session = None
                self.request.user.save()

                message = 'You have judged >={} (max number of judgment you have ' \
                          'specified for this task) documents.'.format(max_judged)
                messages.add_message(request,
                                     messages.SUCCESS,
                                     message)
                context[u"is_max_judged_reached"] = True

        return self.render_json_response(context)
Example #10
0
    def post(self, request, *args, **kwargs):
        user = self.request.user
        current_session = self.request.user.current_session

        try:
            doc_id = self.request_json[u"doc_id"]
            doc_title = self.request_json[u"doc_title"]
            doc_CAL_snippet = self.request_json.get(u"doc_CAL_snippet", None)
            doc_search_snippet = self.request_json.get(u"doc_search_snippet", None)
            relevance = self.request_json.get(u"relevance", None)
            additional_judging_criteria = self.request_json.get(u"additional_judging_criteria", None)
            source = self.request_json[u"source"]
            method = self.request_json.get(u"method", None)
            query = self.request_json.get(u"query", None)
            client_time = self.request_json.get(u"client_time", None)
            historyItem = self.request_json.get(u"historyItem")
            search_query = self.request_json.get(u"search_query", None)
            ctrl_f_terms_input = self.request_json.get(u"ctrl_f_terms_input", None)
            found_ctrl_f_terms_in_title = self.request_json.get(u"found_ctrl_f_terms_in_title", None)
            found_ctrl_f_terms_in_summary = self.request_json.get(u"found_ctrl_f_terms_in_summary", None)
            found_ctrl_f_terms_in_full_doc = self.request_json.get(u"found_ctrl_f_terms_in_full_doc", None)
            current_docview_stack_size = self.request_json.get(u"current_docview_stack_size", None)
        except KeyError:
            error_dict = {u"message": u"POST input missing important fields"}

            return self.render_bad_request_response(error_dict)

        # Check if a judgment exists already, if so, update the db row.
        exists = Judgment.objects.filter(user=user,
                                         doc_id=doc_id,
                                         session=current_session)

        if exists:
            exists = exists.first()
            exists.query = query
            exists.doc_title = doc_title
            if doc_CAL_snippet != "":
                exists.doc_CAL_snippet = doc_CAL_snippet
            if doc_search_snippet != "":
                exists.doc_search_snippet = doc_search_snippet
            if relevance is not None:
                exists.relevance = relevance
            if additional_judging_criteria is not None:
                exists.additional_judging_criteria = additional_judging_criteria
            exists.source = source
            exists.method = method
            exists.historyVerbose.append(historyItem)
            exists.search_query = search_query
            exists.ctrl_f_terms_input = ctrl_f_terms_input
            exists.found_ctrl_f_terms_in_title = found_ctrl_f_terms_in_title
            exists.found_ctrl_f_terms_in_summary = found_ctrl_f_terms_in_summary
            exists.found_ctrl_f_terms_in_full_doc = found_ctrl_f_terms_in_full_doc
            exists.save()

        else:
            Judgment.objects.create(
                user=user,
                doc_id=doc_id,
                doc_title=doc_title,
                doc_CAL_snippet=doc_CAL_snippet,
                doc_search_snippet=doc_search_snippet,
                session=current_session,
                query=query,
                relevance=relevance,
                additional_judging_criteria=additional_judging_criteria,
                source=source,
                method=method,
                historyVerbose=[historyItem],
                search_query=search_query,
                ctrl_f_terms_input=ctrl_f_terms_input,
                found_ctrl_f_terms_in_title=found_ctrl_f_terms_in_title,
                found_ctrl_f_terms_in_summary=found_ctrl_f_terms_in_summary,
                found_ctrl_f_terms_in_full_doc=found_ctrl_f_terms_in_full_doc
            )

        context = {u"message": u"Your judgment on {} has been received!".format(doc_id),
                   u"is_max_judged_reached": False}
        error_message = None

        # This will take care of incomplete judgments (e.g. updating additional judging
        # criteria without making a final judgment on the document)
        if relevance is None:
            return self.render_json_response(context)

        is_from_cal = source == "CAL"
        # mark relevant documents as 1 to CAL.
        rel_CAL = -1 if relevance <= 0 else 1

        if rel_CAL == 0 and current_session.is_shared:
            # Check if someone else in the shared session has marked it as rel
            # if so, cant re-train CAL with this doc as non-relevant
            others_judged_as_rel = Judgment.objects.filter(
                session=current_session,
                relevance__lgt=0
            ).exclude(user=user).exists()
            if others_judged_as_rel:
                rel_CAL = 1

        if is_from_cal:
            context[u"next_docs"] = []

            try:
                next_batch, top_terms = CALFunctions.send_judgment(
                    current_session.uuid,
                    doc_id,
                    rel_CAL)
                if not next_batch:
                    return self.render_json_response(context)

                ret = {}
                next_batch_ids = []
                for docid_score_pair in next_batch:
                    doc_id, doc_score = docid_score_pair.rsplit(':', 1)
                    ret[doc_id] = doc_score
                    next_batch_ids.append(doc_id)

                doc_ids_hack = []
                for doc_id in next_batch_ids:
                    doc = {'doc_id': doc_id}
                    if '.' in doc_id:
                        doc['doc_id'], doc['para_id'] = doc_id.split('.')
                    doc_ids_hack.append(doc)
                seed_query = current_session.topic.seed_query
                if 'doc' in current_session.strategy:
                    documents = DocEngine.get_documents(next_batch_ids,
                                                        seed_query,
                                                        top_terms)
                else:
                    documents = DocEngine.get_documents_with_snippet(doc_ids_hack,
                                                                     seed_query,
                                                                     top_terms)

                # append debugging rel to documents
                for doc in documents:
                    doc["debugging_rel"] = -1
                    djudged = DebuggingJudgment.objects.filter(user=self.request.user,
                                                               doc_id=doc["doc_id"],
                                                               session=self.request.user.current_session)
                    if djudged.exists():
                        djudged = djudged.first()
                        doc["debugging_rel"] = djudged.relevance
                    else:
                        doc["debugging_rel"] = -1  # unjudged

                context[u"next_docs"] = documents
            except TimeoutError:
                context["CALFailedToReceiveJudgment"] = True
                error_dict = {u"message": u"Timeout error. "
                                          u"Please check status of servers."}
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                context["CALFailedToReceiveJudgment"] = True
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                context["CALFailedToReceiveJudgment"] = True
                error_message = str(e)

        else:
            try:
                CALFunctions.send_judgment(current_session.uuid, doc_id, rel_CAL)

            except TimeoutError:
                context["CALFailedToReceiveJudgment"] = True
                traceback.print_exc()
                error_dict = {u"message": u"Timeout error. "
                                          u"Please check status of servers."}
                return self.render_timeout_request_response(error_dict)
            except CALError as e:
                context["CALFailedToReceiveJudgment"] = True
                traceback.print_exc()
                error_message = "CAL Exception: {}".format(str(e))
            except Exception as e:
                context["CALFailedToReceiveJudgment"] = True
                traceback.print_exc()
                error_message = str(e)

        if error_message:
            log_body = {
                "user": user.username,
                "client_time": client_time,
                "result": {
                    "message": error_message,
                    "action": "create",
                    "doc_judgment": {
                        "doc_id": doc_id,
                        "doc_title": doc_title,
                        "topic_number": current_session.topic.number,
                        "session": str(current_session.uuid),
                        "query": query,
                        "relevance": relevance,
                        "additional_judging_criteria": additional_judging_criteria,
                        "source": source,
                        "method": method,
                        "historyVerbose": historyItem,
                        "search_query": search_query,
                        "ctrl_f_terms_input": ctrl_f_terms_input,
                        "found_ctrl_f_terms_in_title": found_ctrl_f_terms_in_title,
                        "found_ctrl_f_terms_in_summary": found_ctrl_f_terms_in_summary,
                        "found_ctrl_f_terms_in_full_doc": found_ctrl_f_terms_in_full_doc
                    }
                }
            }

            logger.error("[{}]".format(json.dumps(log_body)))

        if not exists:
            # Check if user has judged `max_judged` documents in total.
            total_judgements = Judgment.objects.filter(
                user=user,
                session=current_session).filter(
                relevance__isnull=False,
                is_seed=False).count()
            max_judged = current_session.max_number_of_judgments
            # Exit task only if number of judgments reached max (and maxjudged is enabled)
            if total_judgements >= max_judged > 0 and (
                'scal' not in current_session.strategy or
                (is_from_cal and current_docview_stack_size is not None and current_docview_stack_size <= 0)
            ):
                current_session.max_number_of_judgments_reached = True
                current_session.save()

                message = 'You have reached the max number of judgments allowed for ' \
                          'this session (>={} documents).'.format(max_judged)
                messages.add_message(request,
                                     messages.SUCCESS,
                                     message)
                context[u"is_max_judged_reached"] = True

        return self.render_json_response(context)