예제 #1
0
파일: serializers.py 프로젝트: mclumd/kyudo
    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)
예제 #2
0
파일: managers.py 프로젝트: mclumd/kyudo
    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
예제 #3
0
파일: signals.py 프로젝트: mclumd/kyudo
def question_normalization(sender, instance, *args, **kwargs):
    instance.hash = signature(instance.text)