Commit 6552fa80 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB

Separate fields, validators, and models in alerts app

parent c7c42b4c
from django.db import models
from .validators import validate_phone
class PhoneNumberField(models.CharField):
validators = [validate_phone]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-17 20:11
# Generated by Django 1.11.18 on 2019-01-17 20:24
from __future__ import unicode_literals
import alerts.models
import alerts.fields
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
......@@ -24,7 +24,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('desc', models.CharField(max_length=20)),
('email', models.EmailField(blank=True, max_length=254)),
('phone', alerts.models.PhoneNumberField(blank=True, max_length=255, validators=[alerts.models.validate_phone])),
('phone', alerts.fields.PhoneNumberField(blank=True, max_length=255)),
('call_phone', models.BooleanField(default=False)),
('text_phone', models.BooleanField(default=False)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
......
from collections import defaultdict
from django.contrib.auth import get_user_model
from django.db import models
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.contrib.auth.models import User
from events.models import Label, Pipeline
from .fields import PhoneNumberField
from collections import defaultdict
import phonenumbers
import logging
log = logging.getLogger(__name__)
def validate_phone(value):
try:
phone = phonenumbers.parse(value, 'US')
except phonenumbers.NumberParseException:
raise ValidationError('Not a valid phone number: {0}'.format(value))
if not phonenumbers.is_valid_number(phone):
raise ValidationError('Not a valid phone number: {0}'.format(value))
return phonenumbers.format_number(phone, phonenumbers.PhoneNumberFormat.E164)
class PhoneNumberField(models.CharField):
def __init__(self, *args, **kwargs):
super(PhoneNumberField, self).__init__(*args, **kwargs)
def get_prep_value(self, value):
if value:
return validate_phone(value)
else:
return ''
#class Notification(models.Model):
# user = models.ForeignKey(User, null=False)
# onLabel = models.ManyToManyField(Label, blank=True)
# onTypeCreate = models.CharField(max_length=20, choices=TYPES, blank=True)
# onTypeChange = models.CharField(max_length=20, choices=TYPES, blank=True)
# email = models.EmailField()
# Set up user model
UserModel = get_user_model()
class Contact(models.Model):
user = models.ForeignKey(User, null=False)
#new_user = models.ForeignKey(DjangoUser, null=True)
user = models.ForeignKey(UserModel, null=False)
desc = models.CharField(max_length=20)
email = models.EmailField(blank=True)
phone = PhoneNumberField(blank=True, max_length=255,
validators=[validate_phone])
phone = PhoneNumberField(blank=True, max_length=255)
# These fields specify whether alert should be a phone
# call or text (or both).
call_phone = models.BooleanField(default=False)
......@@ -86,12 +60,13 @@ class Contact(models.Model):
print('\tPhone: {0} (call={1}, text={2})'.format(self.phone,
self.call_phone, self.text_phone))
class Trigger(models.Model):
# TP 6 Jul 2017: TYPES and triggerType don't seem to be used anywhere...
TYPES = ( ("create", "create"), ("change","change"), ("label","label") )
triggerType = models.CharField(max_length=20, choices=TYPES, blank=True)
user = models.ForeignKey(User, null=False)
user = models.ForeignKey(UserModel, null=False)
labels = models.ManyToManyField(Label, blank=True)
pipelines = models.ManyToManyField(Pipeline, blank=True)
contacts = models.ManyToManyField(Contact, blank=False)
......@@ -120,4 +95,3 @@ class Trigger(models.Model):
thresh,
", ".join([x.desc for x in self.contacts.all()])
)
import phonenumbers
def validate_phone(value):
# Try to parse phone number
try:
phone = phonenumbers.parse(value, 'US')
except phonenumbers.NumberParseException:
raise ValidationError('Not a valid phone number: {0}'.format(value))
# Validate phone number
if not phonenumbers.is_valid_number(phone):
raise ValidationError('Not a valid phone number: {0}'.format(value))
# Return phone number as a formatted string
return phonenumbers.format_number(phone,
phonenumbers.PhoneNumberFormat.E164)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment