<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django import forms <EOL> from django . conf import settings as django_settings <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . sites . models import get_current_site <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . core . validators import validate_email <EOL> from avocado . models import DataContext , DataView , DataQuery <EOL> from serrano import utils <EOL> from serrano . conf import settings <EOL> log = logging . getLogger ( __name__ ) <EOL> SHARED_QUERY_EMAIL_TITLE = '<STR_LIT>' '<STR_LIT>' <EOL> SHARED_QUERY_EMAIL_BODY = '<STR_LIT>' <EOL> class ContextForm ( forms . ModelForm ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> super ( ContextForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def save ( self , commit = True ) : <EOL> instance = super ( ContextForm , self ) . save ( commit = False ) <EOL> request = self . request <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> instance . user = request . user <EOL> else : <EOL> instance . session_key = request . session . session_key <EOL> if commit : <EOL> instance . save ( ) <EOL> return instance <EOL> class Meta ( object ) : <EOL> model = DataContext <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ViewForm ( forms . ModelForm ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> super ( ViewForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def save ( self , commit = True ) : <EOL> instance = super ( ViewForm , self ) . save ( commit = False ) <EOL> request = self . request <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> instance . user = request . user <EOL> else : <EOL> instance . session_key = request . session . session_key <EOL> if commit : <EOL> instance . save ( ) <EOL> return instance <EOL> class Meta ( object ) : <EOL> model = DataView <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class QueryForm ( forms . ModelForm ) : <EOL> usernames_or_emails = forms . CharField ( widget = forms . Textarea , <EOL> required = False ) <EOL> message = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> super ( QueryForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_usernames_or_emails ( self ) : <EOL> """<STR_LIT>""" <EOL> user_labels = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> emails = set ( ) <EOL> for label in user_labels . split ( '<STR_LIT:U+002C>' ) : <EOL> label = "<STR_LIT>" . join ( label . split ( ) ) <EOL> if not label : <EOL> continue <EOL> try : <EOL> validate_email ( label ) <EOL> emails . add ( label ) <EOL> except ValidationError : <EOL> try : <EOL> user = User . objects . only ( '<STR_LIT:email>' ) . get ( username = label ) <EOL> emails . add ( user . email ) <EOL> except User . DoesNotExist : <EOL> log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( label ) ) <EOL> return emails <EOL> def save ( self , commit = True ) : <EOL> instance = super ( QueryForm , self ) . save ( commit = False ) <EOL> request = self . request <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> instance . user = request . user <EOL> else : <EOL> instance . session_key = request . session . session_key <EOL> if commit : <EOL> instance . save ( ) <EOL> script_name = getattr ( django_settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> all_emails = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> existing_emails = set ( instance . shared_users . all ( ) . values_list ( <EOL> '<STR_LIT:email>' , flat = True ) ) <EOL> new_emails = all_emails - existing_emails <EOL> site = get_current_site ( request ) <EOL> try : <EOL> site_url = request . build_absolute_uri ( script_name + '<STR_LIT:/>' ) <EOL> except KeyError : <EOL> site_url = site . domain + script_name <EOL> query_url = site_url <EOL> reverse_name = settings . QUERY_REVERSE_NAME <EOL> if reverse_name : <EOL> try : <EOL> query_url = reverse ( reverse_name , <EOL> kwargs = { '<STR_LIT>' : instance . pk } ) <EOL> try : <EOL> query_url = request . build_absolute_uri ( query_url ) <EOL> except KeyError : <EOL> query_url = site . domain + script_name + query_url <EOL> except NoReverseMatch : <EOL> log . warn ( "<STR_LIT>" . format ( reverse_name ) ) <EOL> else : <EOL> log . warn ( '<STR_LIT>' ) <EOL> title = SHARED_QUERY_EMAIL_TITLE . format ( query_name = instance . name , <EOL> site_name = site . name ) <EOL> body = SHARED_QUERY_EMAIL_BODY . format ( query_url = query_url ) <EOL> if self . cleaned_data . get ( '<STR_LIT:message>' ) : <EOL> body = '<STR_LIT>' . format ( <EOL> self . cleaned_data . get ( '<STR_LIT:message>' ) , body ) <EOL> utils . send_mail ( new_emails , title , body ) <EOL> for email in new_emails : <EOL> instance . share_with_user ( email ) <EOL> removed_emails = existing_emails - all_emails <EOL> for user in User . objects . filter ( email__in = removed_emails ) : <EOL> instance . shared_users . remove ( user ) <EOL> self . save_m2m ( ) <EOL> return instance <EOL> class Meta ( object ) : <EOL> model = DataQuery <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
