<s> import datetime <EOL> import hashlib <EOL> import operator <EOL> from base64 import urlsafe_b64encode , urlsafe_b64decode <EOL> from collections import defaultdict <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . mail import send_mail <EOL> from django . db import models <EOL> from settings import ONLINE_PETITION_FROM_ADDRESS , ONLINE_PETITION_SECRET , DEPLOYMENT_ROOT_URL <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> def get_domain_from_email ( email ) : <EOL> alpha_index = email . rfind ( '<STR_LIT:@>' ) <EOL> return email [ alpha_index + <NUM_LIT:1> : ] <EOL> def uri_b64encode ( s ) : <EOL> return urlsafe_b64encode ( s ) . strip ( '<STR_LIT:=>' ) <EOL> def uri_b64decode ( s ) : <EOL> return urlsafe_b64decode ( s + '<STR_LIT:=>' * ( <NUM_LIT:4> - len ( s ) % <NUM_LIT:4> ) ) <EOL> class Domain ( models . Model ) : <EOL> name = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:255> , unique = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def is_satisfied_by ( self , domain ) : <EOL> return domain . endswith ( self . name ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class Campaign ( models . Model ) : <EOL> title = models . CharField ( _ ( "<STR_LIT:title>" ) , max_length = <NUM_LIT:50> ) <EOL> description = models . TextField ( _ ( "<STR_LIT:description>" ) ) <EOL> start_date = models . DateTimeField ( _ ( "<STR_LIT>" ) , default = datetime . datetime . now ) <EOL> end_date = models . DateTimeField ( _ ( "<STR_LIT>" ) , default = datetime . datetime . now ) <EOL> valid_domains = models . ManyToManyField ( Domain , verbose_name = _ ( '<STR_LIT>' ) , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> blank = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> @ property <EOL> def signatures ( self ) : <EOL> return map ( lambda x : x . name + "<STR_LIT>" + self . obscured_field ( getattr ( x , '<STR_LIT:email>' ) ) , <EOL> Signature . objects . filter ( campaign = self , is_verified = True ) ) <EOL> def is_registered ( self , email_to_check_for ) : <EOL> return Signature . objects . filter ( campaign = self , email = email_to_check_for ) <EOL> @ property <EOL> def active_campaigns_count ( self ) : <EOL> return Campaign . objects . filter ( start_date__lte = datetime . datetime . now ( ) , <EOL> end_date__gte = datetime . datetime . now ( ) ) . count ( ) <EOL> @ property <EOL> def active_signatures_count ( self ) : <EOL> return self . signature_set . filter ( is_verified = True ) . count ( ) <EOL> @ property <EOL> def not_verified_signatures_count ( self ) : <EOL> return self . signature_set . filter ( is_verified = False ) . count ( ) <EOL> @ property <EOL> def signed_domains ( self ) : <EOL> return [ s . domain for s in self . signature_set . filter ( is_verified = True ) ] <EOL> @ property <EOL> def domain_stats ( self ) : <EOL> if self . valid_domains . count ( ) == <NUM_LIT:0> : <EOL> return sorted ( [ ( domain , self . signed_domains . count ( domain ) ) for domain in set ( self . signed_domains ) ] , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) [ : <NUM_LIT:5> ] <EOL> result_map = defaultdict ( int ) <EOL> for d in self . signed_domains : <EOL> for vd in sorted ( self . valid_domains . all ( ) , key = lambda d : d . name . count ( '<STR_LIT:.>' ) ) : <EOL> if vd . is_satisfied_by ( d ) : <EOL> result_map [ vd . name ] += <NUM_LIT:1> <EOL> break <EOL> return sorted ( result_map . items ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) [ : <NUM_LIT:5> ] <EOL> @ property <EOL> def is_active ( self ) : <EOL> return self . start_date <= datetime . datetime . now ( ) and self . end_date >= datetime . datetime . now ( ) <EOL> def obscured_field ( self , field ) : <EOL> alpha_index = field . rfind ( '<STR_LIT:@>' ) <EOL> dot_index = field . rfind ( '<STR_LIT:.>' ) <EOL> return field [ : alpha_index ] + '<STR_LIT>' + field [ dot_index : ] <EOL> def is_valid_domain ( self , users_email ) : <EOL> valid_domains = Domain . objects . filter ( campaign__id = self . id ) <EOL> if len ( valid_domains ) == <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> user_domain = get_domain_from_email ( users_email ) <EOL> for valid in valid_domains : <EOL> if user_domain . endswith ( valid . name ) : <EOL> return True <EOL> return False <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class Signature ( models . Model ) : <EOL> name = models . CharField ( _ ( "<STR_LIT:name>" ) , max_length = <NUM_LIT:100> ) <EOL> email = models . CharField ( _ ( "<STR_LIT>" ) , <EOL> max_length = <NUM_LIT:255> ) <EOL> signed_date = models . DateTimeField ( _ ( "<STR_LIT>" ) , auto_now_add = True ) <EOL> campaign = models . ForeignKey ( Campaign ) <EOL> is_verified = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False ) <EOL> @ property <EOL> def domain ( self ) : <EOL> alpha_index = self . email . rfind ( '<STR_LIT:@>' ) <EOL> return self . email [ alpha_index + <NUM_LIT:1> : ] <EOL> def get_salt ( self ) : <EOL> return hashlib . sha256 ( <EOL> ONLINE_PETITION_SECRET + unicode ( self . campaign . start_date ) + unicode ( <EOL> self . campaign . end_date ) ) . hexdigest ( ) <EOL> def verify_hash ( self , hash ) : <EOL> salt = self . get_salt ( ) <EOL> correct_hash = hashlib . sha256 ( salt + self . email + unicode ( self . signed_date ) ) . hexdigest ( ) <EOL> if hash == correct_hash : <EOL> self . is_verified = True <EOL> self . save ( ) <EOL> return True <EOL> return False <EOL> def send_verify_email ( self ) : <EOL> salt = self . get_salt ( ) <EOL> activate = hashlib . sha256 ( salt + self . email + unicode ( self . signed_date ) ) . hexdigest ( ) <EOL> send_mail ( _ ( u'<STR_LIT>' ) + self . campaign . title , <EOL> _ ( u"""<STR_LIT>""" ) . format ( self . campaign . title , <EOL> DEPLOYMENT_ROOT_URL + reverse ( '<STR_LIT>' , args = [ self . campaign . pk , ] ) , <EOL> DEPLOYMENT_ROOT_URL + reverse ( '<STR_LIT>' , <EOL> args = [ self . campaign . pk , uri_b64encode ( self . email ) , activate , ] ) ) , <EOL> ONLINE_PETITION_FROM_ADDRESS , ( self . email , ) , fail_silently = False ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) </s>
