<s> from __future__ import absolute_import <EOL> from django import forms <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from sentry . models import ( <EOL> AuditLogEntry , AuditLogEntryEvent , OrganizationMember , Project <EOL> ) <EOL> from sentry . signals import member_joined <EOL> from sentry . web . frontend . base import BaseView <EOL> ERR_INVITE_INVALID = _ ( '<STR_LIT>' ) <EOL> class AcceptInviteForm ( forms . Form ) : <EOL> pass <EOL> class AcceptOrganizationInviteView ( BaseView ) : <EOL> auth_required = False <EOL> def get_form ( self , request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> return AcceptInviteForm ( request . POST ) <EOL> return AcceptInviteForm ( ) <EOL> def handle ( self , request , member_id , token ) : <EOL> assert request . method in [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] <EOL> try : <EOL> om = OrganizationMember . objects . get ( pk = member_id ) <EOL> except OrganizationMember . DoesNotExist : <EOL> messages . add_message ( <EOL> request , messages . ERROR , <EOL> ERR_INVITE_INVALID , <EOL> ) <EOL> return self . redirect ( reverse ( '<STR_LIT>' ) ) <EOL> if not om . is_pending : <EOL> messages . add_message ( <EOL> request , messages . ERROR , <EOL> ERR_INVITE_INVALID , <EOL> ) <EOL> return self . redirect ( reverse ( '<STR_LIT>' ) ) <EOL> if om . token != token : <EOL> messages . add_message ( <EOL> request , messages . ERROR , <EOL> ERR_INVITE_INVALID , <EOL> ) <EOL> return self . redirect ( reverse ( '<STR_LIT>' ) ) <EOL> organization = om . organization <EOL> qs = Project . objects . filter ( <EOL> organization = organization , <EOL> ) <EOL> project_list = list ( qs . select_related ( '<STR_LIT>' ) [ : <NUM_LIT> ] ) <EOL> project_count = qs . count ( ) <EOL> context = { <EOL> '<STR_LIT>' : om . organization , <EOL> '<STR_LIT>' : project_list , <EOL> '<STR_LIT>' : project_count , <EOL> '<STR_LIT>' : not request . user . is_authenticated ( ) , <EOL> } <EOL> if not request . user . is_authenticated ( ) : <EOL> request . session [ '<STR_LIT>' ] = request . get_full_path ( ) <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return self . respond ( '<STR_LIT>' , context ) <EOL> form = self . get_form ( request ) <EOL> if form . is_valid ( ) : <EOL> if OrganizationMember . objects . filter ( organization = organization , user = request . user ) . exists ( ) : <EOL> messages . add_message ( <EOL> request , messages . SUCCESS , <EOL> _ ( '<STR_LIT>' ) % ( <EOL> organization . name . encode ( '<STR_LIT:utf-8>' ) , <EOL> ) <EOL> ) <EOL> om . delete ( ) <EOL> else : <EOL> om . user = request . user <EOL> om . email = None <EOL> om . save ( ) <EOL> AuditLogEntry . objects . create ( <EOL> organization = organization , <EOL> actor = request . user , <EOL> ip_address = request . META [ '<STR_LIT>' ] , <EOL> target_object = om . id , <EOL> target_user = request . user , <EOL> event = AuditLogEntryEvent . MEMBER_ACCEPT , <EOL> data = om . get_audit_log_data ( ) , <EOL> ) <EOL> messages . add_message ( <EOL> request , messages . SUCCESS , <EOL> _ ( '<STR_LIT>' ) % ( <EOL> organization . name . encode ( '<STR_LIT:utf-8>' ) , <EOL> ) <EOL> ) <EOL> member_joined . send ( member = om , sender = self ) <EOL> request . session . pop ( '<STR_LIT>' , None ) <EOL> return self . redirect ( reverse ( '<STR_LIT>' , args = [ organization . slug ] ) ) <EOL> context [ '<STR_LIT>' ] = form <EOL> return self . respond ( '<STR_LIT>' , context ) </s>
