class RegexModel(models.Model): """ A model for testing saving and compiling of regexs. """ regex = RegexField(max_length=128) with_validator = RegexField(max_length=128, validators=[MaxLengthValidator(4)]) with_options = RegexField(max_length=128, re_flags=re.IGNORECASE | re.UNICODE)
class User(models.Model): institution = models.ForeignKey(Institution, on_delete=models.PROTECT) first_name = models.CharField(max_length=128) last_name = models.CharField(max_length=128) email = models.EmailField() user_id = RegexField(max_length=128) def __str__(self): return self.last_name
class Responder(models.Model): """ When an Issue is created, there is often an appropriate response. A Responder record encodes a particular type of Issue to watch for and what actions to take when an Issue is opened. Examples might be emailing an admin, opening a ticket in PagerDuty, or running a bit of code to fix a problem. The actions to be taken are implemented as ResponderActions that ForeignKey to a particular Responder record. """ watch_pattern = RegexField(null=True, max_length=128) # whether to allow responder to respond, even when actions were already executed for a given issue allow_retry = models.BooleanField(default=False) def __str__(self): return 'Responder: {watch_pattern.pattern}'.format( watch_pattern=self.watch_pattern) def respond(self, issue): """ Check if the provided issue matches our watch pattern. If it does, execute the associated ResponderActions. """ if self._match(issue.name): self._execute(issue) return True else: return False def _match(self, issue_name): return self.watch_pattern.match(issue_name) def _execute(self, issue): """ Execute in order all of the ResponderActions associated with this Responder. """ IssueAction.objects.bulk_create([ a.execute(issue) for a in self._get_pending_actions_for_issue(issue) if a.is_time_to_execute(issue) ]) def _get_pending_actions_for_issue(self, issue): """ Determine which actions should be executed for the given issue """ if self.allow_retry: return self.actions.order_by('delay_sec') # exclude actions that have already been executed for the given issue already_executed_action_pks = issue.executed_actions.values_list( 'responder_action__pk', flat=True).all() return self.actions.exclude( pk__in=already_executed_action_pks).order_by('delay_sec')
class BotQuestionRegexModel(models.Model): regex = RegexField(max_length=500, re_flags=re.IGNORECASE) answer = models.CharField(max_length=100, choices=ANSWER_CHOICES) link = models.ForeignKey(BotAnswerLinkModel, null=True, blank=True, on_delete=models.CASCADE) text = models.ForeignKey(BotAnswerTextModel, null=True, blank=True, on_delete=models.CASCADE) def __str__(self): return str(self.regex)
class RegexModel(models.Model): """ A model for testing saving and compiling of regexs. """ regex = RegexField(max_length=128)
class NullTrueModel(models.Model): """ A model with a null=True RegexField. """ regex = RegexField(null=True, max_length=128)
class BlankTrueModel(models.Model): """ A model with a blank=True RegexField. """ regex = RegexField(blank=True, max_length=128)
def test_value_to_string(self): """ This is for coverage to hit the end of the block and return None """ field = RegexField() self.assertIsNone(field.value_to_string(object()))
class CourseCodeFormat(models.Model): institution = models.ForeignKey(Institution, on_delete=models.PROTECT) regex = RegexField(max_length=128) def __str__(self): return str(self.regex)