def deny_demo(request): """ Denies editing of demo account on demo server. """ messages.warning(request, _('You cannot change demo account on the demo server.')) return redirect('profile')
def deny_demo(request): """ Denies editing of demo account on demo server. """ messages.warning(request, _('You cannot change demo account on the demo server.')) return redirect_profile(request.POST.get('activetab'))
def deny_demo(request): """ Denies editing of demo account on demo server. """ messages.warning( request, _('You cannot change demo account on the demo server.') ) return redirect('profile')
def deny_demo(request): """ Denies editing of demo account on demo server. """ messages.warning( request, _('You cannot change demo account on the demo server.') ) return redirect_profile(request.POST.get('activetab'))
def add_host_key(request): """ Adds host key for a host. """ host = request.POST.get('host', '') port = request.POST.get('port', '') if len(host) == 0: messages.error(request, _('Invalid host name given!')) else: cmdline = ['ssh-keyscan'] if port: cmdline.extend(['-p', port]) cmdline.append(host) try: output = subprocess.check_output( cmdline, stderr=subprocess.STDOUT, env=get_clean_env(), ) keys = [] for key in output.decode('utf-8').splitlines(): key = key.strip() if not is_key_line(key): continue keys.append(key) host, keytype, fingerprint = parse_hosts_line(key) messages.warning( request, _( 'Added host key for %(host)s with fingerprint ' '%(fingerprint)s (%(keytype)s), ' 'please verify that it is correct.' ) % { 'host': host, 'fingerprint': fingerprint, 'keytype': keytype, } ) if len(keys) == 0: messages.error( request, _('Failed to fetch public key for a host!') ) with open(ssh_file(KNOWN_HOSTS), 'a') as handle: for key in keys: handle.write('%s\n' % key) except subprocess.CalledProcessError as exc: messages.error( request, _('Failed to get host key: %s') % exc.output ) except OSError as exc: messages.error( request, _('Failed to get host key: %s') % str(exc) )
def add_host_key(request): """ Adds host key for a host. """ host = request.POST.get('host', '') port = request.POST.get('port', '') if len(host) == 0: messages.error(request, _('Invalid host name given!')) else: cmdline = ['ssh-keyscan'] if port: cmdline.extend(['-p', port]) cmdline.append(host) try: output = subprocess.check_output( cmdline, stderr=subprocess.STDOUT, env=get_clean_env(), ) keys = [] for key in output.decode('utf-8').splitlines(): key = key.strip() if not is_key_line(key): continue keys.append(key) host, keytype, fingerprint = parse_hosts_line(key) messages.warning( request, _('Added host key for %(host)s with fingerprint ' '%(fingerprint)s (%(keytype)s), ' 'please verify that it is correct.') % { 'host': host, 'fingerprint': fingerprint, 'keytype': keytype, }) if len(keys) == 0: messages.error(request, _('Failed to fetch public key for a host!')) with open(ssh_file(KNOWN_HOSTS), 'a') as handle: for key in keys: handle.write('%s\n' % key) except subprocess.CalledProcessError as exc: messages.error(request, _('Failed to get host key: %s') % exc.output) except OSError as exc: messages.error(request, _('Failed to get host key: %s') % str(exc))
def home(request): """ Home page of Weblate showing list of projects, stats and user links if logged in. """ if 'show_set_password' in request.session: messages.warning( request, _('You have activated your account, now you should set ' 'the password to be able to login next time.')) return redirect('password') project_ids = Project.objects.get_acl_ids(request.user) suggestions = get_suggestions(request, request.user, project_ids) # Warn about not filled in username (usually caused by migration of # users from older system if not request.user.is_anonymous() and request.user.first_name == '': messages.warning(request, _('Please set your full name in your profile.')) # Some stats last_changes = Change.objects.last_changes(request.user) # Dashboard project/subproject view componentlists = ComponentList.objects.all() # dashboard_choices is dict with labels of choices as a keys dashboard_choices = dict(Profile.DASHBOARD_CHOICES) usersubscriptions = None userlanguages = None active_tab_id = Profile.DASHBOARD_SUGGESTIONS active_tab_slug = Profile.DASHBOARD_SLUGS.get(active_tab_id) if request.user.is_authenticated(): active_tab_id = request.user.profile.dashboard_view active_tab_slug = Profile.DASHBOARD_SLUGS.get(active_tab_id) if active_tab_id == Profile.DASHBOARD_COMPONENT_LIST: clist = request.user.profile.dashboard_component_list active_tab_slug = clist.tab_slug() dashboard_choices[active_tab_id] = clist.name # Ensure ACL filtering applies (user could have been removed # from the project meanwhile) subscribed_projects = request.user.profile.subscriptions.filter( id__in=project_ids) last_changes = last_changes.filter( subproject__project__in=subscribed_projects) components_by_language = Translation.objects.prefetch().filter( language__in=request.user.profile.languages.all(), ).order_by( 'subproject__project__name', 'subproject__name') usersubscriptions = components_by_language.filter( subproject__project__in=subscribed_projects) userlanguages = components_by_language.filter( subproject__project_id__in=project_ids) for componentlist in componentlists: componentlist.translations = components_by_language.filter( subproject__in=componentlist.components.all()) return render( request, 'index.html', { 'suggestions': suggestions, 'last_changes': last_changes[:10], 'last_changes_url': '', 'search_form': SiteSearchForm(), 'usersubscriptions': usersubscriptions, 'userlanguages': userlanguages, 'componentlists': componentlists, 'active_tab_slug': active_tab_slug, 'active_tab_label': dashboard_choices.get(active_tab_id) })
def search(translation, request): ''' Performs search or returns cached search results. ''' # Already performed search if 'sid' in request.GET: # Grab from session storage search_id = 'search_%s' % request.GET['sid'] # Check if we know the search if search_id not in request.session: messages.error(request, _('Invalid search string!')) return redirect(translation) return request.session[search_id] # Possible new search search_form = SearchForm(request.GET) review_form = ReviewForm(request.GET) search_query = None if 'date' in request.GET: if review_form.is_valid(): # Review allunits = translation.unit_set.review( review_form.cleaned_data['date'], request.user ) formatted_date = formats.date_format( review_form.cleaned_data['date'], 'SHORT_DATE_FORMAT' ) name = _('Review of translations since %s') % formatted_date else: show_form_errors(request, review_form) # Filtering by type allunits = translation.unit_set.all() name = _('All strings') elif search_form.is_valid(): # Apply search conditions allunits = translation.unit_set.search( translation, search_form.cleaned_data, ) search_query = search_form.cleaned_data['q'] name = search_form.get_name() else: # Error reporting show_form_errors(request, search_form) # Filtering by type allunits = translation.unit_set.all() name = _('All strings') # Grab unit IDs unit_ids = list(allunits.values_list('id', flat=True)) # Check empty search results if len(unit_ids) == 0: messages.warning(request, _('No string matched your search!')) return redirect(translation) # Checksum unit access offset = 0 if 'checksum' in request.GET: try: unit = allunits.filter(checksum=request.GET['checksum'])[0] offset = unit_ids.index(unit.id) except (Unit.DoesNotExist, IndexError): messages.warning(request, _('No string matched your search!')) return redirect(translation) # Remove old search results cleanup_session(request.session) # Store in cache and return search_id = str(uuid.uuid1()) search_result = { 'query': search_query, 'name': force_text(name) if name else None, 'ids': unit_ids, 'search_id': search_id, 'ttl': int(time.time()) + 86400, 'offset': offset, } request.session['search_%s' % search_id] = search_result return search_result
def home(request): """ Home page of Weblate showing list of projects, stats and user links if logged in. """ if 'show_set_password' in request.session: messages.warning( request, _( 'You have activated your account, now you should set ' 'the password to be able to login next time.' ) ) return redirect('password') project_ids = Project.objects.get_acl_ids(request.user) suggestions = get_suggestions( request, request.user, project_ids ) # Warn about not filled in username (usually caused by migration of # users from older system if not request.user.is_anonymous() and request.user.first_name == '': messages.warning( request, _('Please set your full name in your profile.') ) # Some stats last_changes = Change.objects.last_changes(request.user) # Dashboard project/subproject view componentlists = ComponentList.objects.all() # dashboard_choices is dict with labels of choices as a keys dashboard_choices = dict(Profile.DASHBOARD_CHOICES) usersubscriptions = None userlanguages = None active_tab_id = Profile.DASHBOARD_SUGGESTIONS active_tab_slug = Profile.DASHBOARD_SLUGS.get(active_tab_id) if request.user.is_authenticated(): active_tab_id = request.user.profile.dashboard_view active_tab_slug = Profile.DASHBOARD_SLUGS.get(active_tab_id) if active_tab_id == Profile.DASHBOARD_COMPONENT_LIST: clist = request.user.profile.dashboard_component_list active_tab_slug = clist.tab_slug() dashboard_choices[active_tab_id] = clist.name # Ensure ACL filtering applies (user could have been removed # from the project meanwhile) subscribed_projects = request.user.profile.subscriptions.filter( id__in=project_ids ) last_changes = last_changes.filter( subproject__project__in=subscribed_projects ) components_by_language = Translation.objects.prefetch().filter( language__in=request.user.profile.languages.all(), ).order_by( 'subproject__project__name', 'subproject__name' ) usersubscriptions = components_by_language.filter( subproject__project__in=subscribed_projects ) userlanguages = components_by_language.filter( subproject__project_id__in=project_ids ) for componentlist in componentlists: componentlist.translations = components_by_language.filter( subproject__in=componentlist.components.all() ) return render( request, 'index.html', { 'suggestions': suggestions, 'last_changes': last_changes[:10], 'last_changes_url': '', 'search_form': SiteSearchForm(), 'usersubscriptions': usersubscriptions, 'userlanguages': userlanguages, 'componentlists': componentlists, 'active_tab_slug': active_tab_slug, 'active_tab_label': dashboard_choices.get(active_tab_id) } )
def import_message(request, count, message_none, message_ok): if count == 0: messages.warning(request, message_none) else: messages.success(request, message_ok % count)
def upload_translation(request, project, subproject, lang): ''' Handling of translation uploads. ''' obj = get_translation(request, project, subproject, lang) if not can_upload_translation(request.user, obj): raise PermissionDenied() # Check method and lock if obj.is_locked(request.user): messages.error(request, _('Access denied.')) return redirect(obj) # Get correct form handler based on permissions form = get_upload_form(request.user, obj.subproject.project)( request.POST, request.FILES ) # Check form validity if not form.is_valid(): messages.error(request, _('Please fix errors in the form.')) return redirect(obj) # Create author name author = None if (can_author_translation(request.user, obj.subproject.project) and form.cleaned_data['author_name'] != '' and form.cleaned_data['author_email'] != ''): author = '%s <%s>' % ( form.cleaned_data['author_name'], form.cleaned_data['author_email'] ) # Check for overwriting overwrite = False if can_overwrite_translation(request.user, obj.subproject.project): overwrite = form.cleaned_data['overwrite'] # Do actual import try: ret, count = obj.merge_upload( request, request.FILES['file'], overwrite, author, merge_header=form.cleaned_data['merge_header'], merge_comments=form.cleaned_data['merge_comments'], method=form.cleaned_data['method'], fuzzy=form.cleaned_data['fuzzy'], ) import_message( request, count, _('No strings were imported from the uploaded file.'), ungettext( 'Processed %d string from the uploaded files.', 'Processed %d strings from the uploaded files.', count ) ) if not ret: messages.warning( request, _('There were no new strings in uploaded file!') ) except Exception as error: messages.error( request, _('File content merge failed: %s') % force_text(error) ) report_error(error, sys.exc_info(), request) return redirect(obj)
def search(translation, request): ''' Performs search or returns cached search results. ''' # Already performed search if 'sid' in request.GET: # Grab from session storage search_id = 'search_%s' % request.GET['sid'] # Check if we know the search if search_id not in request.session: messages.error(request, _('Invalid search string!')) return redirect(translation) search_result = copy.copy(request.session[search_id]) if 'params' in search_result: search_result['form'] = SearchForm(search_result['params']) else: search_result['form'] = SearchForm() return search_result # Possible new search search_form = SearchForm(request.GET) review_form = ReviewForm(request.GET) search_query = None if 'date' in request.GET: if review_form.is_valid(): # Review allunits = translation.unit_set.review( review_form.cleaned_data['date'], request.user) formatted_date = formats.date_format( review_form.cleaned_data['date'], 'SHORT_DATE_FORMAT') name = _('Review of translations since %s') % formatted_date else: show_form_errors(request, review_form) # Filtering by type allunits = translation.unit_set.all() name = _('All strings') elif search_form.is_valid(): # Apply search conditions allunits = translation.unit_set.search( translation, search_form.cleaned_data, ) search_query = search_form.cleaned_data['q'] name = search_form.get_name() else: # Error reporting show_form_errors(request, search_form) # Filtering by type allunits = translation.unit_set.all() name = _('All strings') # Grab unit IDs unit_ids = list(allunits.values_list('id', flat=True)) # Check empty search results if len(unit_ids) == 0: messages.warning(request, _('No string matched your search!')) return redirect(translation) # Checksum unit access offset = 0 if 'checksum' in request.GET: try: unit = allunits.filter(checksum=request.GET['checksum'])[0] offset = unit_ids.index(unit.id) except (Unit.DoesNotExist, IndexError): messages.warning(request, _('No string matched your search!')) return redirect(translation) # Remove old search results cleanup_session(request.session) # Store in cache and return search_id = str(uuid.uuid1()) search_result = { 'params': request.GET, 'query': search_query, 'name': force_text(name), 'ids': unit_ids, 'search_id': search_id, 'ttl': int(time.time()) + 86400, 'offset': offset, } request.session['search_%s' % search_id] = search_result search_result = copy.copy(search_result) search_result['form'] = search_form return search_result
def import_message(request, count, message_none, message_ok): if count == 0: messages.warning(request, message_none) else: messages.success(request, message_ok % count)
def upload_translation(request, project, subproject, lang): ''' Handling of translation uploads. ''' obj = get_translation(request, project, subproject, lang) if not can_upload_translation(request.user, obj): raise PermissionDenied() # Check method and lock if obj.is_locked(request.user): messages.error(request, _('Access denied.')) return redirect(obj) # Get correct form handler based on permissions form = get_upload_form(request.user, obj, request.POST, request.FILES) # Check form validity if not form.is_valid(): messages.error(request, _('Please fix errors in the form.')) show_form_errors(request, form) return redirect(obj) # Create author name author = None if (can_author_translation(request.user, obj.subproject.project) and form.cleaned_data['author_name'] != '' and form.cleaned_data['author_email'] != ''): author = '%s <%s>' % (form.cleaned_data['author_name'], form.cleaned_data['author_email']) # Check for overwriting overwrite = False if can_overwrite_translation(request.user, obj.subproject.project): overwrite = form.cleaned_data['overwrite'] # Do actual import try: ret, count = obj.merge_upload( request, request.FILES['file'], overwrite, author, merge_header=form.cleaned_data['merge_header'], merge_comments=form.cleaned_data['merge_comments'], method=form.cleaned_data['method'], fuzzy=form.cleaned_data['fuzzy'], ) import_message( request, count, _('No strings were imported from the uploaded file.'), ungettext('Processed %d string from the uploaded files.', 'Processed %d strings from the uploaded files.', count)) if not ret: messages.warning(request, _('There were no new strings in uploaded file!')) except Exception as error: messages.error(request, _('File content merge failed: %s') % force_text(error)) report_error(error, sys.exc_info(), request) return redirect(obj)