<s> __author__ = '<STR_LIT>' <EOL> import uuid <EOL> import hmac <EOL> from hashlib import sha1 <EOL> from django . db import models <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . contrib . auth . models import User <EOL> from uuidfield import UUIDField <EOL> from django . core . exceptions import ValidationError <EOL> from schedule . models import Calendar <EOL> from django . contrib . auth import models as auth_models <EOL> from django . db . models import signals <EOL> from django . conf import settings <EOL> AUTH_USER_MODEL = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ python_2_unicode_compatible <EOL> class Token ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> key = models . CharField ( max_length = <NUM_LIT> , primary_key = True ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if not self . key : <EOL> self . key = self . generate_key ( ) <EOL> return super ( Token , self ) . save ( * args , ** kwargs ) <EOL> def generate_key ( self ) : <EOL> unique = uuid . uuid4 ( ) <EOL> return hmac . new ( unique . bytes , digestmod = sha1 ) . hexdigest ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . key <EOL> def __str__ ( self ) : <EOL> return self . key <EOL> @ python_2_unicode_compatible <EOL> class SchedulePolicy ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> repeat_times = models . IntegerField ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def natural_key ( self ) : <EOL> return ( self . name ) <EOL> @ python_2_unicode_compatible <EOL> class Service ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> id = UUIDField ( primary_key = True , auto = True ) <EOL> retry = models . IntegerField ( blank = True , null = True ) <EOL> policy = models . ForeignKey ( SchedulePolicy , blank = True , null = True ) <EOL> escalate_after = models . IntegerField ( blank = True , null = True ) <EOL> notifications_disabled = models . BooleanField ( default = False ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def natural_key ( self ) : <EOL> return ( self . id ) <EOL> @ python_2_unicode_compatible <EOL> class EventLog ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> ACTIONS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ property <EOL> def color ( self ) : <EOL> colort_dict = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:success>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:info>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:success>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return colort_dict [ self . action ] <EOL> user = models . ForeignKey ( User , blank = True , default = None , null = True , related_name = '<STR_LIT>' ) <EOL> incident_key = models . ForeignKey ( '<STR_LIT>' , blank = True , null = True ) <EOL> action = models . CharField ( choices = ACTIONS , default = '<STR_LIT>' , max_length = "<STR_LIT:100>" ) <EOL> service_key = models . ForeignKey ( Service ) <EOL> data = models . TextField ( ) <EOL> occurred_at = models . DateTimeField ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . data <EOL> def natural_key ( self ) : <EOL> return ( self . service_key , self . id ) <EOL> @ python_2_unicode_compatible <EOL> class Incident ( models . Model ) : <EOL> TRIGGER = "<STR_LIT>" <EOL> RESOLVE = "<STR_LIT>" <EOL> ACKNOWLEDGE = "<STR_LIT>" <EOL> """<STR_LIT>""" <EOL> service_key = models . ForeignKey ( Service ) <EOL> incident_key = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> event_type = models . CharField ( max_length = <NUM_LIT:15> ) <EOL> description = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> details = models . TextField ( ) <EOL> occurred_at = models . DateTimeField ( ) <EOL> @ property <EOL> def color ( self ) : <EOL> colort_dict = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:success>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:info>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return colort_dict [ self . event_type ] <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> unique_together = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> def __str__ ( self ) : <EOL> return self . incident_key <EOL> def natural_key ( self ) : <EOL> return ( self . service_key , self . incident_key ) <EOL> def clean ( self ) : <EOL> if self . event_type not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValidationError ( "<STR_LIT>" % self . event_type ) <EOL> @ python_2_unicode_compatible <EOL> class ServiceTokens ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> service_id = models . ForeignKey ( Service ) <EOL> token_id = models . ForeignKey ( Token ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> @ python_2_unicode_compatible <EOL> class SchedulePolicyRule ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> schedule_policy = models . ForeignKey ( SchedulePolicy , related_name = '<STR_LIT>' ) <EOL> position = models . IntegerField ( ) <EOL> user_id = models . ForeignKey ( User , blank = True , null = True ) <EOL> schedule = models . ForeignKey ( Calendar , blank = True , null = True ) <EOL> escalate_after = models . IntegerField ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return str ( self . id ) <EOL> @ classmethod <EOL> def getRulesForService ( cls , service ) : <EOL> return cls . objects . filter ( schedule_policy = service . policy ) <EOL> class UserProfile ( models . Model ) : <EOL> user = models . OneToOneField ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> phone_number = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> pushover_user_key = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> pushover_app_key = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> slack_room_name = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> prowl_api_key = models . CharField ( max_length = <NUM_LIT:50> , blank = True ) <EOL> prowl_application = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> prowl_url = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> class ServiceSilenced ( models . Model ) : <EOL> service = models . ForeignKey ( Service ) <EOL> silenced = models . BooleanField ( default = False ) <EOL> silenced_until = models . DateTimeField ( ) <EOL> class IncidentSilenced ( models . Model ) : <EOL> incident = models . ForeignKey ( Incident ) <EOL> silenced = models . BooleanField ( default = False ) <EOL> silenced_until = models . DateTimeField ( ) <EOL> def create_user_profile ( sender , instance , created , ** kwargs ) : <EOL> if created : <EOL> UserProfile . objects . create ( user = instance ) <EOL> signals . post_save . connect ( create_user_profile , sender = User ) <EOL> signals . post_syncdb . disconnect ( <EOL> sender = auth_models , <EOL> dispatch_uid = '<STR_LIT>' ) </s>
