|
| 1 | +# fugato.managers |
| 2 | +# Custom managers for the fugato models |
| 3 | +# |
| 4 | +# Author: Benjamin Bengfort <[email protected]> |
| 5 | +# Created: Wed Jul 22 14:03:52 2015 -0400 |
| 6 | +# |
| 7 | +# Copyright (C) 2016 District Data Labs |
| 8 | +# For license information, see LICENSE.txt |
| 9 | +# |
| 10 | +# ID: managers.py [] [email protected] $ |
| 11 | + |
| 12 | +""" |
| 13 | +Custom managers for the fugato models |
| 14 | +""" |
| 15 | + |
| 16 | +########################################################################## |
| 17 | +## Imports |
| 18 | +########################################################################## |
| 19 | + |
| 20 | +from django.db import models |
| 21 | +from minent.utils import signature |
| 22 | + |
| 23 | +########################################################################## |
| 24 | +## Questions Manager |
| 25 | +########################################################################## |
| 26 | + |
| 27 | +class QuestionManager(models.Manager): |
| 28 | + |
| 29 | + def dedupe(self, raise_for_exceptions=False, **kwargs): |
| 30 | + """ |
| 31 | + Essentially a GET or CREATE method that checks if a duplicate |
| 32 | + question already exists in the database by its signature. If |
| 33 | + raise_for_exceptions is True, then will raise a DuplicateQuestion |
| 34 | + exception, otherwise it will return None. |
| 35 | +
|
| 36 | + Returns question, created where created is a Boolean |
| 37 | + """ |
| 38 | + qsig = signature(kwargs['text']) |
| 39 | + query = self.filter(signature=qsig) |
| 40 | + if query.exists(): |
| 41 | + if raise_for_exceptions: |
| 42 | + raise DuplicateQuestion() |
| 43 | + return query.first(), False |
| 44 | + |
| 45 | + return self.create(**kwargs), True |
0 commit comments