<s> from __future__ import unicode_literals <EOL> import json <EOL> import logging <EOL> from django . conf import settings <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template . context import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from djblets . cache . forwarding_backend import DEFAULT_FORWARD_CACHE_ALIAS <EOL> from djblets . siteconfig . models import SiteConfiguration <EOL> from djblets . siteconfig . views import site_settings as djblets_site_settings <EOL> from reviewboard . accounts . models import Profile <EOL> from reviewboard . admin . cache_stats import get_cache_stats <EOL> from reviewboard . admin . forms import SSHSettingsForm <EOL> from reviewboard . admin . security_checks import SecurityCheckRunner <EOL> from reviewboard . admin . support import get_support_url , serialize_support_data <EOL> from reviewboard . admin . widgets import ( dynamic_activity_data , <EOL> primary_widgets , <EOL> secondary_widgets ) <EOL> from reviewboard . ssh . client import SSHClient <EOL> from reviewboard . ssh . utils import humanize_key <EOL> @ staff_member_required <EOL> def dashboard ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> profile = Profile . objects . get_or_create ( user = request . user ) [ <NUM_LIT:0> ] <EOL> if profile . extra_data is None : <EOL> profile . extra_data = { } <EOL> profile . save ( update_fields = ( '<STR_LIT>' , ) ) <EOL> profile_data = profile . extra_data <EOL> selected_primary_widgets = [ ] <EOL> unselected_primary_widgets = [ ] <EOL> primary_widget_selections = profile_data . get ( '<STR_LIT>' ) <EOL> if primary_widget_selections : <EOL> for p in primary_widgets : <EOL> if primary_widget_selections [ p . widget_id ] == "<STR_LIT:1>" : <EOL> selected_primary_widgets . append ( p ) <EOL> else : <EOL> unselected_primary_widgets . append ( p ) <EOL> else : <EOL> selected_primary_widgets = primary_widgets <EOL> unselected_primary_widgets = None <EOL> selected_secondary_widgets = [ ] <EOL> unselected_secondary_widgets = [ ] <EOL> secondary_widget_selections = profile_data . get ( <EOL> '<STR_LIT>' ) <EOL> if secondary_widget_selections : <EOL> for s in secondary_widgets : <EOL> if secondary_widget_selections [ s . widget_id ] == "<STR_LIT:1>" : <EOL> selected_secondary_widgets . append ( s ) <EOL> else : <EOL> unselected_secondary_widgets . append ( s ) <EOL> else : <EOL> selected_secondary_widgets = secondary_widgets <EOL> unselected_secondary_widgets = None <EOL> primary_widget_positions = profile_data . get ( '<STR_LIT>' ) <EOL> if primary_widget_positions : <EOL> sorted_primary_widgets = sorted ( <EOL> selected_primary_widgets , <EOL> key = ( lambda y : primary_widget_positions [ y . widget_id ] or <EOL> len ( primary_widget_positions ) ) ) <EOL> else : <EOL> sorted_primary_widgets = selected_primary_widgets <EOL> secondary_widget_positions = profile_data . get ( '<STR_LIT>' ) <EOL> if secondary_widget_positions : <EOL> sorted_secondary_widgets = sorted ( <EOL> selected_secondary_widgets , <EOL> key = ( lambda y : secondary_widget_positions [ y . widget_id ] or <EOL> len ( secondary_widget_positions ) ) ) <EOL> else : <EOL> sorted_secondary_widgets = selected_secondary_widgets <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> '<STR_LIT>' : primary_widgets , <EOL> '<STR_LIT>' : settings . SITE_ROOT + '<STR_LIT>' , <EOL> '<STR_LIT>' : secondary_widgets , <EOL> '<STR_LIT>' : sorted_primary_widgets , <EOL> '<STR_LIT>' : sorted_secondary_widgets , <EOL> '<STR_LIT>' : serialize_support_data ( request , True ) , <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : unselected_primary_widgets , <EOL> '<STR_LIT>' : unselected_secondary_widgets , <EOL> } ) ) <EOL> @ staff_member_required <EOL> def cache_stats ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> cache_stats = get_cache_stats ( ) <EOL> cache_info = settings . CACHES [ DEFAULT_FORWARD_CACHE_ALIAS ] <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> '<STR_LIT>' : cache_stats , <EOL> '<STR_LIT>' : cache_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : settings . SITE_ROOT + "<STR_LIT>" <EOL> } ) ) <EOL> @ staff_member_required <EOL> def security ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> runner = SecurityCheckRunner ( ) <EOL> results = runner . run ( ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> } ) ) <EOL> @ csrf_protect <EOL> @ staff_member_required <EOL> def site_settings ( request , form_class , template_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return djblets_site_settings ( request , form_class , template_name , { <EOL> '<STR_LIT>' : settings . SITE_ROOT + "<STR_LIT>" <EOL> } ) <EOL> @ csrf_protect <EOL> @ staff_member_required <EOL> def ssh_settings ( request , template_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> client = SSHClient ( ) <EOL> key = client . get_user_key ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = SSHSettingsForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> if form . did_request_delete ( ) and client . get_user_key ( ) is not None : <EOL> try : <EOL> form . delete ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT:.>' ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' % e ) <EOL> else : <EOL> try : <EOL> form . create ( request . FILES ) <EOL> return HttpResponseRedirect ( '<STR_LIT:.>' ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' % e ) <EOL> else : <EOL> form = SSHSettingsForm ( ) <EOL> if key : <EOL> fingerprint = humanize_key ( key ) <EOL> else : <EOL> fingerprint = None <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : fingerprint , <EOL> '<STR_LIT>' : client . get_public_key ( key ) , <EOL> '<STR_LIT>' : form , <EOL> } ) ) <EOL> def manual_updates_required ( <EOL> request , updates , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> '<STR_LIT>' : [ render_to_string ( update_template_name , <EOL> RequestContext ( request , extra_context ) ) <EOL> for ( update_template_name , extra_context ) in updates ] , <EOL> } ) ) <EOL> def widget_toggle ( request ) : <EOL> """<STR_LIT>""" <EOL> collapsed = request . GET . get ( '<STR_LIT>' , None ) <EOL> widget = request . GET . get ( '<STR_LIT>' , None ) <EOL> if widget and collapsed : <EOL> siteconfig = SiteConfiguration . objects . get_current ( ) <EOL> widget_settings = siteconfig . get ( "<STR_LIT>" , { } ) <EOL> widget_settings [ widget ] = collapsed <EOL> siteconfig . set ( "<STR_LIT>" , widget_settings ) <EOL> siteconfig . save ( ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> def widget_move ( request ) : <EOL> """<STR_LIT>""" <EOL> profile = Profile . objects . get_or_create ( user = request . user ) [ <NUM_LIT:0> ] <EOL> profile_data = profile . extra_data <EOL> widget_type = request . POST . get ( '<STR_LIT:type>' ) <EOL> if widget_type == '<STR_LIT>' : <EOL> positions_key = '<STR_LIT>' <EOL> widgets = primary_widgets <EOL> else : <EOL> positions_key = '<STR_LIT>' <EOL> widgets = secondary_widgets <EOL> positions = profile_data . setdefault ( positions_key , { } ) <EOL> for widget in widgets : <EOL> widget_position = request . POST . get ( widget . widget_id ) <EOL> if widget_position is not None : <EOL> positions [ widget . widget_id ] = widget_position <EOL> else : <EOL> positions [ widget . widget_id ] = str ( len ( widgets ) ) <EOL> profile . save ( ) <EOL> return HttpResponse ( ) <EOL> def widget_select ( request ) : <EOL> """<STR_LIT>""" <EOL> profile = Profile . objects . get_or_create ( user = request . user ) [ <NUM_LIT:0> ] <EOL> profile_data = profile . extra_data <EOL> widget_type = request . POST . get ( '<STR_LIT:type>' ) <EOL> if widget_type == '<STR_LIT>' : <EOL> selections_key = '<STR_LIT>' <EOL> widgets = primary_widgets <EOL> else : <EOL> selections_key = '<STR_LIT>' <EOL> widgets = secondary_widgets <EOL> initial_selections = { } <EOL> for widget in widgets : <EOL> initial_selections [ widget . widget_id ] = "<STR_LIT:1>" <EOL> selections = profile_data . setdefault ( selections_key , initial_selections ) <EOL> for widget in widgets : <EOL> widget_selection = request . POST . get ( widget . widget_id ) <EOL> if widget_selection is not None : <EOL> selections [ widget . widget_id ] = widget_selection <EOL> profile . save ( ) <EOL> return HttpResponse ( ) <EOL> def widget_activity ( request ) : <EOL> """<STR_LIT>""" <EOL> activity_data = dynamic_activity_data ( request ) <EOL> return HttpResponse ( json . dumps ( activity_data ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> def support_redirect ( request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return HttpResponseRedirect ( get_support_url ( request ) ) </s>
