<s> from django . contrib import messages <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . decorators import login_required , permission_required <EOL> from django . contrib . sites . shortcuts import get_current_site <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . utils import timezone <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . http import require_POST <EOL> from connect . accounts . models import AbuseReport <EOL> from connect . utils import send_connect_email <EOL> from connect . moderation . forms import ( <EOL> FilterLogsForm , InviteMemberForm , ModerateApplicationForm , <EOL> ModerateAbuseForm , ReInviteMemberForm , <EOL> ReportAbuseForm , RevokeInvitationForm <EOL> ) <EOL> from connect . moderation . models import ModerationLogMsg <EOL> from connect . moderation . utils import get_date_limits , log_moderator_event <EOL> User = get_user_model ( ) <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def moderation_home ( request , <EOL> invitation_form = None , <EOL> reinvitation_form = None , <EOL> revocation_form = None ) : <EOL> """<STR_LIT>""" <EOL> pending = User . objects . filter ( moderator = request . user , <EOL> registration_method = User . INVITED , <EOL> auth_token_is_used = False , <EOL> is_active = False ) <EOL> if not invitation_form : <EOL> invitation_form = InviteMemberForm ( ) <EOL> if not reinvitation_form : <EOL> reinvitation_form = ReInviteMemberForm ( moderator = request . user ) <EOL> if not revocation_form : <EOL> revocation_form = RevokeInvitationForm ( ) <EOL> context = { <EOL> '<STR_LIT>' : invitation_form , <EOL> '<STR_LIT>' : reinvitation_form , <EOL> '<STR_LIT>' : revocation_form , <EOL> '<STR_LIT>' : pending , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ require_POST <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def invite_user ( request ) : <EOL> """<STR_LIT>""" <EOL> moderator = request . user <EOL> site = get_current_site ( request ) <EOL> invitation_form = InviteMemberForm ( request . POST ) <EOL> if invitation_form . is_valid ( ) : <EOL> full_name = invitation_form . cleaned_data [ '<STR_LIT>' ] <EOL> email = invitation_form . cleaned_data [ '<STR_LIT:email>' ] <EOL> new_user = moderator . invite_new_user ( email , full_name ) <EOL> msg_type = ModerationLogMsg . INVITATION <EOL> log_comment = _ ( '<STR_LIT>' . format ( moderator . get_full_name ( ) , <EOL> new_user . get_full_name ( ) ) ) <EOL> log_moderator_event ( msg_type = msg_type , <EOL> user = new_user , <EOL> moderator = moderator , <EOL> comment = log_comment ) <EOL> subject = _ ( '<STR_LIT>' . format ( site . name ) ) <EOL> template = '<STR_LIT>' <EOL> token = new_user . auth_token <EOL> url = request . build_absolute_uri ( <EOL> reverse ( '<STR_LIT>' , args = [ token ] ) ) <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = new_user , <EOL> sender = moderator , <EOL> site = site , <EOL> url = url ) <EOL> messages . success ( request , _ ( '<STR_LIT>' . format ( <EOL> new_user . get_full_name ( ) , site . name ) ) ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> return moderation_home ( request , invitation_form = invitation_form ) <EOL> @ require_POST <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def reinvite_user ( request ) : <EOL> """<STR_LIT>""" <EOL> moderator = request . user <EOL> site = get_current_site ( request ) <EOL> reinvitation_form = ReInviteMemberForm ( request . POST , <EOL> moderator = request . user ) <EOL> if reinvitation_form . is_valid ( ) : <EOL> user_id = reinvitation_form . cleaned_data [ '<STR_LIT>' ] <EOL> user = User . objects . get ( id = user_id ) <EOL> if not user . auth_token_is_used : <EOL> email = reinvitation_form . cleaned_data [ '<STR_LIT:email>' ] <EOL> moderator . reinvite_user ( user , email ) <EOL> msg_type = ModerationLogMsg . REINVITATION <EOL> log_comment = _ ( '<STR_LIT>' . format ( <EOL> moderator . get_full_name ( ) , <EOL> user . get_full_name ( ) ) ) <EOL> log_moderator_event ( msg_type = msg_type , <EOL> user = user , <EOL> moderator = moderator , <EOL> comment = log_comment ) <EOL> url = request . build_absolute_uri ( <EOL> reverse ( '<STR_LIT>' , <EOL> args = [ user . auth_token ] ) ) <EOL> subject = _ ( '<STR_LIT>' . format ( site . name ) ) <EOL> template = '<STR_LIT>' <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = user , <EOL> sender = moderator , <EOL> site = site , <EOL> url = url ) <EOL> messages . success ( request , _ ( '<STR_LIT>' . format ( <EOL> user . get_full_name ( ) . title ( ) , site . name ) ) ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> return moderation_home ( request , reinvitation_form = reinvitation_form ) <EOL> @ require_POST <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def revoke_invitation ( request ) : <EOL> """<STR_LIT>""" <EOL> site = get_current_site ( request ) <EOL> revocation_form = RevokeInvitationForm ( request . POST ) <EOL> if revocation_form . is_valid ( ) : <EOL> user_id = revocation_form . cleaned_data [ '<STR_LIT>' ] <EOL> user = get_object_or_404 ( User , id = user_id ) <EOL> if user . is_invited_pending_activation and user . moderator == request . user : <EOL> messages . success ( request , _ ( <EOL> '<STR_LIT>' . format ( <EOL> user . get_full_name ( ) , site . name ) ) ) <EOL> user . delete ( ) <EOL> else : <EOL> raise PermissionDenied <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> return moderation_home ( request , revocation_form = revocation_form ) <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def review_applications ( request ) : <EOL> """<STR_LIT>""" <EOL> moderator = request . user <EOL> site = get_current_site ( request ) <EOL> pending = User . objects . filter ( registration_method = '<STR_LIT>' , <EOL> decision_datetime = None , <EOL> is_active = False ) <EOL> form = ModerateApplicationForm ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ModerateApplicationForm ( request . POST ) <EOL> user = get_object_or_404 ( User , id = request . POST [ '<STR_LIT>' ] ) <EOL> if form . is_valid ( ) : <EOL> decision = form . cleaned_data [ '<STR_LIT>' ] <EOL> comments = form . cleaned_data [ '<STR_LIT>' ] <EOL> if decision == '<STR_LIT>' : <EOL> confirmation_message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> user . get_full_name ( ) . title ( ) ) ) <EOL> moderator . approve_user_application ( user ) <EOL> msg_type = ModerationLogMsg . APPROVAL <EOL> url = request . build_absolute_uri ( <EOL> reverse ( '<STR_LIT>' , <EOL> args = [ user . auth_token ] ) ) <EOL> subject = _ ( '<STR_LIT>' . format ( site . name ) ) <EOL> template = '<STR_LIT>' <EOL> elif decision == '<STR_LIT>' : <EOL> confirmation_message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> user . get_full_name ( ) . title ( ) ) ) <EOL> moderator . reject_user_application ( user ) <EOL> msg_type = ModerationLogMsg . REJECTION <EOL> url = '<STR_LIT>' <EOL> subject = _ ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( site . name ) ) <EOL> template = '<STR_LIT>' <EOL> log_comment = '<STR_LIT:{}>' . format ( comments ) <EOL> log_moderator_event ( msg_type = msg_type , <EOL> user = user , <EOL> moderator = moderator , <EOL> comment = log_comment ) <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = user , <EOL> sender = moderator , <EOL> site = site , <EOL> url = url ) <EOL> messages . success ( request , confirmation_message ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> context = { <EOL> '<STR_LIT>' : pending , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> def report_abuse ( request , user_id ) : <EOL> """<STR_LIT>""" <EOL> logged_against = get_object_or_404 ( User , id = user_id ) <EOL> logged_by = request . user <EOL> if request . POST : <EOL> form = ReportAbuseForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> abuse_comment = form . cleaned_data [ '<STR_LIT>' ] <EOL> AbuseReport . objects . create ( <EOL> logged_by = logged_by , <EOL> logged_against = logged_against , <EOL> abuse_comment = abuse_comment , <EOL> ) <EOL> moderators = ( User . objects . filter ( is_moderator = True , is_active = True ) <EOL> . exclude ( id = logged_against . id ) <EOL> . exclude ( id = logged_by . id ) ) <EOL> site = get_current_site ( request ) <EOL> url = request . build_absolute_uri ( <EOL> reverse ( '<STR_LIT>' ) ) <EOL> subject = _ ( '<STR_LIT>' . format ( site . name ) ) <EOL> template = ( <EOL> '<STR_LIT>' <EOL> ) <EOL> for moderator in moderators : <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = moderator , <EOL> site = site , <EOL> url = url ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = ReportAbuseForm ( ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : logged_against , <EOL> '<STR_LIT>' : logged_by , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> @ permission_required ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def review_abuse ( request ) : <EOL> """<STR_LIT>""" <EOL> site = get_current_site ( request ) <EOL> undecided_reports = ( AbuseReport . objects . filter ( decision_datetime = None ) <EOL> . exclude ( logged_against__is_active = False ) <EOL> . exclude ( logged_against = request . user ) <EOL> . exclude ( logged_by = request . user ) <EOL> . select_related ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> reported_users = set ( [ report . logged_against <EOL> for report in undecided_reports ] ) <EOL> warnings = AbuseReport . objects . filter ( <EOL> logged_against__in = reported_users , <EOL> moderator_decision = '<STR_LIT>' <EOL> ) . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> form = ModerateAbuseForm ( ) <EOL> for report in undecided_reports : <EOL> accused_user = report . logged_against <EOL> report . prior_warnings = [ warning for warning in warnings <EOL> if warning . logged_against == accused_user ] <EOL> if request . POST : <EOL> abuse_report = get_object_or_404 ( AbuseReport , <EOL> id = request . POST [ '<STR_LIT>' ] ) <EOL> form = ModerateAbuseForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> decision = form . cleaned_data [ '<STR_LIT>' ] <EOL> comments = form . cleaned_data [ '<STR_LIT>' ] <EOL> moderator = request . user <EOL> user = abuse_report . logged_against <EOL> abuse_report . moderator = moderator <EOL> abuse_report . moderator_decision = decision <EOL> abuse_report . moderator_comment = comments <EOL> abuse_report . decision_datetime = timezone . now ( ) <EOL> abuse_report . save ( ) <EOL> logged_by = abuse_report . logged_by <EOL> logged_against = abuse_report . logged_against <EOL> def send_email_to_reporting_user ( subject , template ) : <EOL> """<STR_LIT>""" <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = logged_by , <EOL> logged_against = logged_against , <EOL> site = site , <EOL> comments = comments ) <EOL> def send_email_to_offending_user ( subject , template ) : <EOL> """<STR_LIT>""" <EOL> send_connect_email ( subject = subject , <EOL> template = template , <EOL> recipient = logged_against , <EOL> logged_against = logged_against , <EOL> site = site , <EOL> comments = comments ) <EOL> if decision == '<STR_LIT>' : <EOL> msg_type = ModerationLogMsg . DISMISSAL <EOL> confirmation_message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> user . get_full_name ( ) . title ( ) ) ) <EOL> subject = _ ( '<STR_LIT>' . format ( <EOL> site . name ) ) <EOL> template = '<STR_LIT>' <EOL> send_email_to_reporting_user ( subject , template ) <EOL> elif decision == '<STR_LIT>' : <EOL> msg_type = ModerationLogMsg . WARNING <EOL> confirmation_message = _ ( <EOL> "<STR_LIT>" . format ( <EOL> user . get_full_name ( ) . title ( ) ) ) <EOL> subject = _ ( <EOL> '<STR_LIT>' . format ( <EOL> logged_against . get_full_name ( ) , site . name ) ) <EOL> template = ( <EOL> '<STR_LIT>' <EOL> ) <EOL> send_email_to_reporting_user ( subject , template ) <EOL> subject = _ ( '<STR_LIT>' . format ( site . name ) ) <EOL> template = '<STR_LIT>' <EOL> send_email_to_offending_user ( subject , template ) <EOL> if decision == '<STR_LIT>' : <EOL> msg_type = ModerationLogMsg . BANNING <EOL> confirmation_message = _ ( "<STR_LIT>" . format ( <EOL> user . get_full_name ( ) . title ( ) , site . name ) ) <EOL> subject = _ ( '<STR_LIT>' . format ( <EOL> logged_against . get_full_name ( ) , <EOL> site . name ) ) <EOL> template = '<STR_LIT>' <EOL> send_email_to_reporting_user ( subject , template ) <EOL> subject = _ ( ( '<STR_LIT>' ) . format ( <EOL> site . name ) ) <EOL> template = '<STR_LIT>' <EOL> send_email_to_offending_user ( subject , template ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> log_moderator_event ( msg_type = msg_type , <EOL> user = user , <EOL> moderator = moderator , <EOL> comment = comments ) <EOL> messages . success ( request , confirmation_message ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> context = { <EOL> '<STR_LIT>' : undecided_reports , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def view_logs ( request ) : <EOL> logs = ModerationLogMsg . objects . exclude ( <EOL> pertains_to = request . user <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) . select_related ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> form = FilterLogsForm ( ) <EOL> local_tz = timezone . get_current_timezone ( ) <EOL> today = timezone . now ( ) . astimezone ( local_tz ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> form = FilterLogsForm ( request . GET ) <EOL> if form . is_valid ( ) : <EOL> msg_type = form . cleaned_data [ '<STR_LIT>' ] <EOL> period = form . cleaned_data [ '<STR_LIT>' ] <EOL> if period == '<STR_LIT>' : <EOL> start , end = get_date_limits ( start_date = today ) <EOL> elif period == '<STR_LIT>' : <EOL> yesterday = today - timezone . timedelta ( days = <NUM_LIT:1> ) <EOL> start , end = get_date_limits ( start_date = yesterday ) <EOL> elif period == '<STR_LIT>' : <EOL> start_date = today - timezone . timedelta ( days = <NUM_LIT:7> ) <EOL> end_date = today <EOL> start , end = get_date_limits ( start_date , end_date ) <EOL> elif period == '<STR_LIT>' : <EOL> start_date = form . cleaned_data [ '<STR_LIT>' ] <EOL> end_date = form . cleaned_data [ '<STR_LIT>' ] <EOL> start , end = get_date_limits ( start_date , end_date ) <EOL> if msg_type != '<STR_LIT>' : <EOL> logs = logs . filter ( msg_type = msg_type ) <EOL> if period != '<STR_LIT>' : <EOL> logs = logs . filter ( msg_datetime__gte = start , <EOL> msg_datetime__lte = end ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : logs , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) </s>
