Example #1
0
    def create(self, validated_data):
        """
        Override the create method to deal with duplicate questions and
        other API-specific errors that can happen on Question creation.
        """

        ## Check to make sure there is no duplicate
        hash = signature(validated_data['text'])
        if Question.objects.filter(hash=hash).exists():
            raise DuplicateQuestion()

        ## Create the model as before
        return super(QuestionSerializer, self).create(validated_data)
Example #2
0
    def dedupe(self, raise_for_exceptions=False, **kwargs):
        """
        Essentially a GET or CREATE method that checks if a duplicate
        question already exists in the database by its hash. If
        raise_for_exceptions is True, then will raise a DuplicateQuestion
        exception, otherwise it will return None.

        Returns question, created where created is a Boolean
        """
        hash  = signature(kwargs['text'])
        query = self.filter(hash=hash)
        if query.exists():
            if raise_for_exceptions:
                raise DuplicateQuestion()
            return query.first(), False

        return self.create(**kwargs), True
Example #3
0
def question_normalization(sender, instance, *args, **kwargs):
    instance.hash = signature(instance.text)