def overview(request, translation_project, dir_path, filename=None, goal=None): from django.utils import dateformat from staticpages.models import StaticPage from .actions import action_groups if filename: ctx = { 'store_tags': request.store.tag_like_objects, } template_name = "translation_projects/store_overview.html" else: ctx = { 'tp_tags': translation_project.tag_like_objects, } template_name = "browser/overview.html" if (check_permission('translate', request) or check_permission('suggest', request) or check_permission('overwrite', request)): ctx.update({ 'upload_form': _handle_upload_form(request, translation_project), }) can_edit = check_permission('administrate', request) project = translation_project.project language = translation_project.language resource_obj = request.store or request.directory #TODO enable again some actions when drilling down a goal. if goal is None: actions = action_groups(request, resource_obj) else: actions = [] action_output = '' # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime tp_goals = translation_project.all_goals ctx.update(get_overview_context(request)) ctx.update({ 'resource_obj': request.store or request.directory, # Dirty hack. 'translation_project': translation_project, 'description': translation_project.description, 'project': project, 'language': language, 'tp_goals': tp_goals, 'goal': goal, 'feed_path': request.directory.pootle_path[1:], 'action_groups': actions, 'action_output': action_output, 'can_edit': can_edit, 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'announcement_displayed': display_announcement, }) tp_pootle_path = translation_project.pootle_path if request.store is None: table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] if goal is not None: # Then show the drill down view for the specified goal. continue_url = goal.get_translate_url_for_path(request.pootle_path, state='incomplete') critical_url = goal.get_critical_url_for_path(request.pootle_path) review_url = goal.get_translate_url_for_path(request.pootle_path, state='suggestions') all_url = goal.get_translate_url_for_path(request.pootle_path) ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_goal_parent(request.directory, goal), 'items': get_goal_children(request.directory, goal), }, 'url_action_continue': continue_url, 'url_action_fixcritical': critical_url, 'url_action_review': review_url, 'url_action_view_all': all_url, }) else: # Then show the files tab. ctx.update({ 'table': { 'id': 'tp-files', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': get_children(request.directory), }, }) if can_edit: if request.store is None: add_tag_action_url = reverse('pootle-xhr-tag-tp', args=[language.code, project.code]) else: add_tag_action_url = reverse('pootle-xhr-tag-store', args=[resource_obj.pk]) ctx.update({ 'form': DescriptionForm(instance=translation_project), 'form_action': reverse('pootle-tp-admin-settings', args=[language.code, project.code]), 'add_tag_form': TagForm(), 'add_tag_action_url': add_tag_action_url, }) if goal is not None: ctx.update({ 'goal_form': GoalForm(instance=goal), 'goal_form_action': reverse('pootle-xhr-edit-goal', args=[goal.slug]), }) response = render(request, template_name, ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None, goal=None, in_goal_overview=False): if filename: ctx = { 'store_tags': request.store.tag_like_objects, } template_name = "translation_projects/store_overview.html" else: ctx = { 'tp_tags': translation_project.tag_like_objects, } template_name = "browser/overview.html" if (check_permission('translate', request) or check_permission('suggest', request) or check_permission('overwrite', request)): ctx.update({ 'upload_form': _handle_upload_form(request, translation_project), }) can_edit = check_permission('administrate', request) project = translation_project.project language = translation_project.language resource_obj = request.store or request.directory #TODO enable again some actions when drilling down a goal. if goal is None: actions = action_groups(request, resource_obj) else: actions = [] action_output = '' running = request.GET.get(EXTDIR, '') #TODO enable the following again when drilling down a goal. if running and goal is None: if request.store: act = StoreAction else: act = TranslationProjectAction try: action = act.lookup(running) except KeyError: messages.error(request, _("Unable to find '%(action)s' in '%(extdir)s'") % {'action': act, 'extdir': running}) else: if not getattr(action, 'nosync', False): (request.store or translation_project).sync() if action.is_active(request): vcs_dir = settings.VCS_DIRECTORY po_dir = settings.PODIRECTORY tp_dir = request.directory.get_real_path() store_fn = '*' if request.store: tp_dir_slash = add_trailing_slash(tp_dir) if request.store.file.name.startswith(tp_dir_slash): # Note: store_f used below in reverse() call. store_f = request.store.file.name[len(tp_dir_slash):] store_fn = store_f.replace('/', os.sep) # Clear possibly stale output/error (even from other # resource_obj). action.set_output('') action.set_error('') try: action.run(path=resource_obj, root=po_dir, tpdir=tp_dir, project=project.code, language=language.code, store=store_fn, style=translation_project.file_style, vc_root=vcs_dir) except StandardError: err = (_("Error while running '%s' extension action") % action.title) logging.exception(err) if (action.error): messages.error(request, action.error) else: messages.error(request, err) else: if (action.error): messages.warning(request, action.error) action_output = action.output if getattr(action, 'get_download', None): export_path = action.get_download(resource_obj) if export_path: import mimetypes abs_path = absolute_real_path(export_path) filename = os.path.basename(export_path) mimetype, encoding = mimetypes.guess_type(filename) mimetype = mimetype or 'application/octet-stream' with open(abs_path, 'rb') as f: response = HttpResponse(f.read(), mimetype=mimetype) response['Content-Disposition'] = ( 'attachment; filename="%s"' % filename) return response if not action_output: if not request.store: rev_args = [language.code, project.code, ''] overview_url = reverse('pootle-tp-overview', args=rev_args) else: slash = store_f.rfind('/') store_d = '' if slash > 0: store_d = store_f[:slash] store_f = store_f[slash + 1:] elif slash == 0: store_f = store_f[1:] rev_args = [language.code, project.code, store_d, store_f] overview_url = reverse('pootle-tp-overview', args=rev_args) return HttpResponseRedirect(overview_url) if goal is None: description = translation_project.description else: description = goal.description # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime ctx.update(get_overview_context(request)) ctx.update({ 'resource_obj': request.store or request.directory, # Dirty hack. 'translation_project': translation_project, 'description': description, 'project': project, 'language': language, 'feed_path': request.directory.pootle_path[1:], 'action_groups': actions, 'action_output': action_output, 'can_edit': can_edit, 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'announcement_displayed': display_announcement, }) tp_pootle_path = translation_project.pootle_path if request.store is None: resource_obj_goals = Goal.get_goals_for_path(resource_obj.pootle_path) resource_obj_has_goals = len(resource_obj_goals) > 0 if in_goal_overview and resource_obj_has_goals: # Then show the goals tab. table_fields = ['name', 'progress', 'priority', 'total', 'need-translation', 'suggestions'] items = [make_goal_item(resource_obj_goal, resource_obj.pootle_path) for resource_obj_goal in resource_obj_goals] ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': items, }, 'resource_obj_has_goals': True, }) elif goal in resource_obj_goals: # Then show the drill down view for the specified goal. table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_goal_parent(request.directory, goal), 'items': get_goal_children(request.directory, goal), }, 'goal': goal, 'goal_url': goal.get_drill_down_url_for_path(tp_pootle_path), 'resource_obj_has_goals': True, }) else: # Then show the files tab. table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp-files', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': get_children(request.directory), }, 'resource_obj_has_goals': resource_obj_has_goals, }) elif goal is not None: ctx.update({ 'goal': goal, 'goal_url': goal.get_drill_down_url_for_path(tp_pootle_path), }) if can_edit: if request.store is None: url_kwargs = { 'language_code': language.code, 'project_code': project.code, } add_tag_action_url = reverse('pootle-xhr-tag-tp', kwargs=url_kwargs) else: add_tag_action_url = reverse('pootle-xhr-tag-store', args=[resource_obj.pk]) if goal is None: edit_form = DescriptionForm(instance=translation_project) edit_form_action = reverse('pootle-tp-admin-settings', args=[language.code, project.code]) else: edit_form = GoalForm(instance=goal) edit_form_action = reverse('pootle-xhr-edit-goal', args=[goal.slug]) ctx.update({ 'form': edit_form, 'form_action': edit_form_action, 'add_tag_form': TagForm(), 'add_tag_action_url': add_tag_action_url, }) response = render_to_response(template_name, ctx, context_instance=RequestContext(request)) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/projects/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None has_announcement = announcement is not None has_sidebar = has_announcement is_sidebar_open = True stored_mtime = None new_mtime = None cookie_data = {} if SIDEBAR_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[SIDEBAR_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: is_sidebar_open = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if has_announcement: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: is_sidebar_open = True new_mtime = ann_mtime ctx = get_overview_context(request) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated( ): from import_export.views import handle_upload_form from pootle_app.models.permissions import check_permission ctx.update(handle_upload_form(request)) has_download = (check_permission('translate', request) or check_permission('suggest', request)) ctx.update({ 'display_download': has_download, }) has_sidebar = True if store is None: table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(directory), 'items': get_children(directory), } }) ctx.update({ 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(request.resource_obj.get_stats()), 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'is_sidebar_open': is_sidebar_open, 'has_sidebar': has_sidebar, }) response = render(request, 'browser/overview.html', ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None, goal=None): from django.utils import dateformat from staticpages.models import StaticPage from .actions import action_groups if filename: ctx = { 'store_tags': request.store.tag_like_objects, } template_name = "translation_projects/store_overview.html" else: ctx = { 'tp_tags': translation_project.tag_like_objects, } template_name = "browser/overview.html" if (check_permission('translate', request) or check_permission('suggest', request) or check_permission('overwrite', request)): ctx.update({ 'upload_form': _handle_upload_form(request, translation_project), }) can_edit = check_permission('administrate', request) project = translation_project.project language = translation_project.language resource_obj = request.store or request.directory #TODO enable again some actions when drilling down a goal. if goal is None: actions = action_groups(request, resource_obj) else: actions = [] action_output = '' # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime tp_goals = translation_project.all_goals ctx.update(get_overview_context(request)) ctx.update({ 'resource_obj': request.store or request.directory, # Dirty hack. 'translation_project': translation_project, 'description': translation_project.description, 'project': project, 'language': language, 'tp_goals': tp_goals, 'goal': goal, 'feed_path': request.directory.pootle_path[1:], 'action_groups': actions, 'action_output': action_output, 'can_edit': can_edit, 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'announcement_displayed': display_announcement, }) tp_pootle_path = translation_project.pootle_path if request.store is None: table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] if goal is not None: # Then show the drill down view for the specified goal. continue_url = goal.get_translate_url_for_path(request.pootle_path, state='incomplete') critical_url = goal.get_critical_url_for_path(request.pootle_path) review_url = goal.get_translate_url_for_path(request.pootle_path, state='suggestions') all_url = goal.get_translate_url_for_path(request.pootle_path) ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_goal_parent(request.directory, goal), 'items': get_goal_children(request.directory, goal), }, 'url_action_continue': continue_url, 'url_action_fixcritical': critical_url, 'url_action_review': review_url, 'url_action_view_all': all_url, }) else: # Then show the files tab. ctx.update({ 'table': { 'id': 'tp-files', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': get_children(request.directory), }, }) if can_edit: if request.store is None: add_tag_action_url = reverse('pootle-xhr-tag-tp', args=[language.code, project.code]) else: add_tag_action_url = reverse('pootle-xhr-tag-store', args=[resource_obj.pk]) ctx.update({ 'form': DescriptionForm(instance=translation_project), 'form_action': reverse('pootle-tp-admin-settings', args=[language.code, project.code]), 'add_tag_form': TagForm(), 'add_tag_action_url': add_tag_action_url, }) if goal is not None: ctx.update({ 'goal_form': GoalForm(instance=goal), 'goal_form_action': reverse('pootle-xhr-edit-goal', args=[goal.slug]), }) response = render(request, template_name, ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None, goal=None, in_goal_overview=False): if filename: ctx = { 'store_tags': request.store.tag_like_objects, } template_name = "translation_projects/store_overview.html" else: ctx = { 'tp_tags': translation_project.tag_like_objects, } template_name = "browser/overview.html" if (check_permission('translate', request) or check_permission('suggest', request) or check_permission('overwrite', request)): ctx.update({ 'upload_form': _handle_upload_form(request, translation_project), }) can_edit = check_permission('administrate', request) project = translation_project.project language = translation_project.language resource_obj = request.store or request.directory #TODO enable again some actions when drilling down a goal. if goal is None: actions = action_groups(request, resource_obj) else: actions = [] action_output = '' running = request.GET.get(EXTDIR, '') #TODO enable the following again when drilling down a goal. if running and goal is None: if request.store: act = StoreAction else: act = TranslationProjectAction try: action = act.lookup(running) except KeyError: messages.error(request, _("Unable to find '%(action)s' in '%(extdir)s'") % {'action': act, 'extdir': running}) else: if not getattr(action, 'nosync', False): (request.store or translation_project).sync() if action.is_active(request): vcs_dir = settings.VCS_DIRECTORY po_dir = settings.PODIRECTORY tp_dir = request.directory.get_real_path() store_fn = '*' if request.store: tp_dir_slash = add_trailing_slash(tp_dir) if request.store.file.name.startswith(tp_dir_slash): # Note: store_f used below in reverse() call. store_f = request.store.file.name[len(tp_dir_slash):] store_fn = store_f.replace('/', os.sep) # Clear possibly stale output/error (even from other # resource_obj). action.set_output('') action.set_error('') try: action.run(path=resource_obj, root=po_dir, tpdir=tp_dir, project=project.code, language=language.code, store=store_fn, style=translation_project.file_style, vc_root=vcs_dir) except StandardError: err = (_("Error while running '%s' extension action") % action.title) logging.exception(err) if (action.error): messages.error(request, action.error) else: messages.error(request, err) else: if (action.error): messages.warning(request, action.error) action_output = action.output if getattr(action, 'get_download', None): export_path = action.get_download(resource_obj) if export_path: import mimetypes abs_path = absolute_real_path(export_path) filename = os.path.basename(export_path) mimetype, encoding = mimetypes.guess_type(filename) mimetype = mimetype or 'application/octet-stream' with open(abs_path, 'rb') as f: response = HttpResponse(f.read(), mimetype=mimetype) response['Content-Disposition'] = ( 'attachment; filename="%s"' % filename) return response if not action_output: if not request.store: rev_args = [language.code, project.code, ''] overview_url = reverse('pootle-tp-overview', args=rev_args) else: slash = store_f.rfind('/') store_d = '' if slash > 0: store_d = store_f[:slash] store_f = store_f[slash + 1:] elif slash == 0: store_f = store_f[1:] rev_args = [language.code, project.code, store_d, store_f] overview_url = reverse('pootle-tp-overview', args=rev_args) return HttpResponseRedirect(overview_url) if goal is None: description = translation_project.description else: description = goal.description # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = simplejson.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime ctx.update(get_overview_context(request)) ctx.update({ 'resource_obj': request.store or request.directory, # Dirty hack. 'translation_project': translation_project, 'description': description, 'project': project, 'language': language, 'feed_path': request.directory.pootle_path[1:], 'action_groups': actions, 'action_output': action_output, 'can_edit': can_edit, 'browser_extends': 'translation_projects/base.html', 'browser_body_id': 'tpoverview', 'announcement': announcement, 'announcement_displayed': display_announcement, }) tp_pootle_path = translation_project.pootle_path if request.store is None: resource_obj_goals = Goal.get_goals_for_path(resource_obj.pootle_path) resource_obj_has_goals = len(resource_obj_goals) > 0 if in_goal_overview and resource_obj_has_goals: # Then show the goals tab. table_fields = ['name', 'progress', 'priority', 'total', 'need-translation', 'suggestions'] items = [make_goal_item(resource_obj_goal, resource_obj.pootle_path) for resource_obj_goal in resource_obj_goals] ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': items, }, 'resource_obj_has_goals': True, }) elif goal in resource_obj_goals: # Then show the drill down view for the specified goal. table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp-goals', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_goal_parent(request.directory, goal), 'items': get_goal_children(request.directory, goal), }, 'goal': goal, 'goal_url': goal.get_drill_down_url_for_path(tp_pootle_path), 'resource_obj_has_goals': True, }) else: # Then show the files tab. table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp-files', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(request.directory), 'items': get_children(request.directory), }, 'resource_obj_has_goals': resource_obj_has_goals, }) elif goal is not None: ctx.update({ 'goal': goal, 'goal_url': goal.get_drill_down_url_for_path(tp_pootle_path), }) if can_edit: if request.store is None: url_kwargs = { 'language_code': language.code, 'project_code': project.code, } add_tag_action_url = reverse('pootle-xhr-tag-tp', kwargs=url_kwargs) else: add_tag_action_url = reverse('pootle-xhr-tag-store', args=[resource_obj.pk]) if goal is None: edit_form = DescriptionForm(instance=translation_project) edit_form_action = reverse('pootle-tp-admin-settings', args=[language.code, project.code]) else: edit_form = GoalForm(instance=goal) edit_form_action = reverse('pootle-xhr-edit-goal', args=[goal.slug]) ctx.update({ 'form': edit_form, 'form_action': edit_form_action, 'add_tag_form': TagForm(), 'add_tag_action_url': add_tag_action_url, }) response = render_to_response(template_name, ctx, context_instance=RequestContext(request)) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(simplejson.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime ctx = get_overview_context(request) ctx.update({ 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(request.resource_obj.get_stats()), 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'announcement_displayed': display_announcement, }) if store is None: table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(directory), 'items': get_children(directory), } }) response = render(request, 'browser/overview.html', ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store is_admin = check_permission('administrate', request) # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/projects/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None has_announcement = announcement is not None has_sidebar = has_announcement is_sidebar_open = True stored_mtime = None new_mtime = None cookie_data = {} if SIDEBAR_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[SIDEBAR_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: is_sidebar_open = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if has_announcement: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: is_sidebar_open = True new_mtime = ann_mtime ctx = get_overview_context(request) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated(): from import_export.views import handle_upload_form ctx.update(handle_upload_form(request)) has_download = (check_permission('translate', request) or check_permission('suggest', request)) ctx.update({ 'display_download': has_download, }) has_sidebar = True stats = request.resource_obj.get_stats() if store is None: table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(directory), 'items': get_children(directory), } }) vfolders = get_vfolders(directory) if len(vfolders) > 0: table_fields = ['name', 'priority', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'activity'] ctx.update({ 'vfolders': { 'id': 'vfolders', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': get_vfolders(directory, all_vfolders=is_admin), }, }) #FIXME: set vfolders stats in the resource, don't inject them here. stats['vfolders'] = VirtualFolder.get_stats_for( directory.pootle_path, all_vfolders=is_admin ) ctx.update({ 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(stats), 'is_admin': is_admin, 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'is_sidebar_open': is_sidebar_open, 'has_sidebar': has_sidebar, }) response = render(request, 'browser/overview.html', ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store # TODO: cleanup and refactor, retrieve from cache try: ann_virtual_path = 'announcements/projects/' + project.code announcement = StaticPage.objects.live(request.user).get( virtual_path=ann_virtual_path, ) except StaticPage.DoesNotExist: announcement = None display_announcement = True stored_mtime = None new_mtime = None cookie_data = {} if ANN_COOKIE_NAME in request.COOKIES: json_str = unquote(request.COOKIES[ANN_COOKIE_NAME]) cookie_data = json.loads(json_str) if 'isOpen' in cookie_data: display_announcement = cookie_data['isOpen'] if project.code in cookie_data: stored_mtime = cookie_data[project.code] if announcement is not None: ann_mtime = dateformat.format(announcement.modified_on, 'U') if ann_mtime != stored_mtime: display_announcement = True new_mtime = ann_mtime ctx = get_overview_context(request) ctx.update({ 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(request.resource_obj.get_stats()), 'browser_extends': 'translation_projects/base.html', 'announcement': announcement, 'announcement_displayed': display_announcement, }) if store is None: table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'parent': get_parent(directory), 'items': get_children(directory), } }) response = render(request, 'browser/overview.html', ctx) if new_mtime is not None: cookie_data[project.code] = new_mtime cookie_data = quote(json.dumps(cookie_data)) response.set_cookie(ANN_COOKIE_NAME, cookie_data) return response
def browse(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store is_admin = check_permission('administrate', request) ctx, cookie_data = get_sidebar_announcements_context( request, (project, language, translation_project, ), ) ctx.update(get_browser_context(request)) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS: if request.user.is_authenticated(): if check_permission('translate', request): ctx.update(handle_upload_form(request, project)) ctx.update({'display_download': True, 'has_sidebar': True}) stats = request.resource_obj.get_stats() if store is None: table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': get_children(directory), } }) if 'virtualfolder' in settings.INSTALLED_APPS: vfolders = get_vfolders(directory, all_vfolders=is_admin) if len(vfolders) > 0: table_fields = ['name', 'priority', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'vfolders': { 'id': 'vfolders', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': vfolders, }, }) # FIXME: set vfolders stats in the resource, don't inject them # here. stats['vfolders'] = {} for vfolder_treeitem in directory.vf_treeitems.iterator(): if request.user.is_superuser or vfolder_treeitem.is_visible: stats['vfolders'][vfolder_treeitem.code] = \ vfolder_treeitem.get_stats(include_children=False) ctx.update({ 'parent': get_parent(directory if store is None else store), 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(stats), 'is_admin': is_admin, 'is_store': store is not None, 'browser_extends': 'translation_projects/base.html', }) response = render(request, 'browser/index.html', ctx) if cookie_data: response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response
def overview(request, translation_project, dir_path, filename=None, goal=None, in_goal_overview=False): if filename: ctx = {"store_tags": request.store.tag_like_objects} template_name = "translation_projects/store_overview.html" else: ctx = {"tp_tags": translation_project.tag_like_objects} template_name = "browser/overview.html" if ( check_permission("translate", request) or check_permission("suggest", request) or check_permission("overwrite", request) ): ctx.update({"upload_form": _handle_upload_form(request, translation_project)}) can_edit = check_permission("administrate", request) project = translation_project.project language = translation_project.language resource_obj = request.store or request.directory # TODO enable again some actions when drilling down a goal. if goal is None: actions = action_groups(request, resource_obj) else: actions = [] action_output = "" running = request.GET.get(EXTDIR, "") # TODO enable the following again when drilling down a goal. if running and goal is None: if request.store: act = StoreAction else: act = TranslationProjectAction try: action = act.lookup(running) except KeyError: messages.error( request, _("Unable to find '%(action)s' in '%(extdir)s'") % {"action": act, "extdir": running} ) else: if not getattr(action, "nosync", False): (request.store or translation_project).sync() if action.is_active(request): vcs_dir = settings.VCS_DIRECTORY po_dir = settings.PODIRECTORY tp_dir = request.directory.get_real_path() store_fn = "*" if request.store: tp_dir_slash = add_trailing_slash(tp_dir) if request.store.file.name.startswith(tp_dir_slash): # Note: store_f used below in reverse() call. store_f = request.store.file.name[len(tp_dir_slash) :] store_fn = store_f.replace("/", os.sep) # Clear possibly stale output/error (even from other # resource_obj). action.set_output("") action.set_error("") try: action.run( path=resource_obj, root=po_dir, tpdir=tp_dir, project=project.code, language=language.code, store=store_fn, style=translation_project.file_style, vc_root=vcs_dir, ) except StandardError: err = _("Error while running '%s' extension action") % action.title logging.exception(err) if action.error: messages.error(request, action.error) else: messages.error(request, err) else: if action.error: messages.warning(request, action.error) action_output = action.output if getattr(action, "get_download", None): export_path = action.get_download(resource_obj) if export_path: import mimetypes abs_path = absolute_real_path(export_path) filename = os.path.basename(export_path) mimetype, encoding = mimetypes.guess_type(filename) mimetype = mimetype or "application/octet-stream" with open(abs_path, "rb") as f: response = HttpResponse(f.read(), mimetype=mimetype) response["Content-Disposition"] = 'attachment; filename="%s"' % filename return response if not action_output: if not request.store: rev_args = [language.code, project.code, ""] overview_url = reverse("pootle-tp-overview", args=rev_args) else: slash = store_f.rfind("/") store_d = "" if slash > 0: store_d = store_f[:slash] store_f = store_f[slash + 1 :] elif slash == 0: store_f = store_f[1:] rev_args = [language.code, project.code, store_d, store_f] overview_url = reverse("pootle-tp-overview", args=rev_args) return HttpResponseRedirect(overview_url) if goal is None: description = translation_project.description else: description = goal.description ctx.update(get_overview_context(request)) ctx.update( { "resource_obj": request.store or request.directory, # Dirty hack. "translation_project": translation_project, "description": description, "project": project, "language": language, "feed_path": request.directory.pootle_path[1:], "action_groups": actions, "action_output": action_output, "can_edit": can_edit, "browser_extends": "translation_projects/base.html", "browser_body_id": "tpoverview", } ) tp_pootle_path = translation_project.pootle_path if request.store is None: resource_obj_goals = Goal.get_goals_for_path(resource_obj.pootle_path) resource_obj_has_goals = len(resource_obj_goals) > 0 if in_goal_overview and resource_obj_has_goals: # Then show the goals tab. table_fields = ["name", "progress", "priority", "total", "need-translation", "suggestions"] items = [ make_goal_item(resource_obj_goal, resource_obj.pootle_path) for resource_obj_goal in resource_obj_goals ] ctx.update( { "table": { "id": "tp-goals", "fields": table_fields, "headings": get_table_headings(table_fields), "parent": get_parent(request.directory), "items": items, }, "resource_obj_has_goals": True, } ) elif goal in resource_obj_goals: # Then show the drill down view for the specified goal. table_fields = ["name", "progress", "total", "need-translation", "suggestions", "critical", "activity"] ctx.update( { "table": { "id": "tp-goals", "fields": table_fields, "headings": get_table_headings(table_fields), "parent": get_goal_parent(request.directory, goal), "items": get_goal_children(request.directory, goal), }, "goal": goal, "goal_url": goal.get_drill_down_url_for_path(tp_pootle_path), "resource_obj_has_goals": True, } ) else: # Then show the files tab. table_fields = ["name", "progress", "total", "need-translation", "suggestions", "critical", "activity"] ctx.update( { "table": { "id": "tp-files", "fields": table_fields, "headings": get_table_headings(table_fields), "parent": get_parent(request.directory), "items": get_children(request.directory), }, "resource_obj_has_goals": resource_obj_has_goals, } ) elif goal is not None: ctx.update({"goal": goal, "goal_url": goal.get_drill_down_url_for_path(tp_pootle_path)}) if can_edit: if request.store is None: url_kwargs = {"language_code": language.code, "project_code": project.code} add_tag_action_url = reverse("pootle-xhr-tag-tp", kwargs=url_kwargs) else: add_tag_action_url = reverse("pootle-xhr-tag-store", args=[resource_obj.pk]) if goal is None: edit_form = DescriptionForm(instance=translation_project) edit_form_action = reverse("pootle-tp-admin-settings", args=[language.code, project.code]) else: edit_form = GoalForm(instance=goal) edit_form_action = reverse("pootle-xhr-edit-goal", args=[goal.slug]) ctx.update( { "form": edit_form, "form_action": edit_form_action, "add_tag_form": TagForm(), "add_tag_action_url": add_tag_action_url, } ) return render_to_response(template_name, ctx, context_instance=RequestContext(request))
def browse(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store is_admin = check_permission('administrate', request) ctx, cookie_data = get_sidebar_announcements_context( request, ( project, language, translation_project, ), ) ctx.update(get_browser_context(request)) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS: if request.user.is_authenticated(): if check_permission('translate', request): ctx.update(handle_upload_form(request, project)) ctx.update({'display_download': True, 'has_sidebar': True}) stats = request.resource_obj.get_stats() if store is None: table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': get_children(directory), } }) if 'virtualfolder' in settings.INSTALLED_APPS: vfolders = get_vfolders(directory, all_vfolders=is_admin) if len(vfolders) > 0: table_fields = [ 'name', 'priority', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] ctx.update({ 'vfolders': { 'id': 'vfolders', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': vfolders, }, }) # FIXME: set vfolders stats in the resource, don't inject them # here. stats['vfolders'] = {} for vfolder_treeitem in directory.vf_treeitems.iterator(): if request.user.is_superuser or vfolder_treeitem.is_visible: stats['vfolders'][vfolder_treeitem.code] = \ vfolder_treeitem.get_stats(include_children=False) ctx.update({ 'parent': get_parent(directory if store is None else store), 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(stats), 'is_admin': is_admin, 'is_store': store is not None, 'browser_extends': 'translation_projects/base.html', }) response = render(request, 'browser/index.html', ctx) if cookie_data: response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response
def _test_browse_view(tp, request, response, kwargs): cookie_data = json.loads( unquote(response.cookies[SIDEBAR_COOKIE_NAME].value)) assert cookie_data["foo"] == "bar" assert "announcements_projects_%s" % tp.project.code in cookie_data assert "announcements_%s" % tp.language.code in cookie_data assert ( "announcements_%s_%s" % (tp.language.code, tp.project.code) in cookie_data) ctx = response.context kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs pootle_path = "%s%s" % (tp.pootle_path, resource_path) if not (kwargs["dir_path"] or kwargs.get("filename")): ob = tp.directory elif not kwargs.get("filename"): ob = Directory.objects.get( pootle_path=pootle_path) else: ob = Store.objects.get( pootle_path=pootle_path) if not kwargs.get("filename"): vftis = ob.vf_treeitems.select_related("vfolder") if not ctx["is_admin"]: vftis = vftis.filter(vfolder__is_public=True) vfolders = [ make_vfolder_treeitem_dict(vfolder_treeitem) for vfolder_treeitem in vftis.order_by('-vfolder__priority') if (ctx["is_admin"] or vfolder_treeitem.is_visible)] stats = {"vfolders": {}} for vfolder_treeitem in vfolders or []: stats['vfolders'][ vfolder_treeitem['code']] = vfolder_treeitem["stats"] del vfolder_treeitem["stats"] if stats["vfolders"]: stats.update(ob.get_stats()) else: stats = ob.get_stats() stats = jsonify(stats) else: stats = jsonify(ob.get_stats()) vfolders = None filters = {} if vfolders: filters['sort'] = 'priority' if resource_path: resource_obj = ob else: resource_obj = tp if not kwargs.get("filename"): table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] table = {'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': get_children(ob)} else: table = None assertions = dict( page="browse", translation_project=tp, language=tp.language, project=tp.project, resource_obj=resource_obj, is_admin=False, is_store=(kwargs.get("filename") and True or False), browser_extends="translation_projects/base.html", pootle_path=pootle_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), translation_states=get_translation_states(ob), check_categories=get_qualitycheck_schema(ob), url_action_continue=ob.get_translate_url( state='incomplete', **filters), url_action_fixcritical=ob.get_critical_url(**filters), url_action_review=ob.get_translate_url( state='suggestions', **filters), url_action_view_all=ob.get_translate_url(state='all'), stats=stats, parent=get_parent(ob)) if table: assertions["table"] = table sidebar = get_sidebar_announcements_context( request, (tp.project, tp.language, tp)) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[0][k] view_context_test(ctx, **assertions) if vfolders: for vfolder in ctx["vfolders"]["items"]: assert vfolder["is_grayed"] is False assert ( ctx["vfolders"]["items"] == vfolders)
def overview(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store is_admin = check_permission('administrate', request) ctx, cookie_data = get_sidebar_announcements_context(request, project.code, language.code) ctx.update(get_overview_context(request)) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated(): from import_export.views import handle_upload_form ctx.update(handle_upload_form(request)) has_download = (check_permission('translate', request) or check_permission('suggest', request)) ctx.update({ 'display_download': has_download, 'has_sidebar': True, }) stats = request.resource_obj.get_stats() if store is None: table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] ctx.update({ 'table': { 'id': 'tp', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': get_children(directory), } }) vfolders = get_vfolders(directory, all_vfolders=is_admin) if len(vfolders) > 0: table_fields = ['name', 'priority', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'activity'] ctx.update({ 'vfolders': { 'id': 'vfolders', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': vfolders, }, }) #FIXME: set vfolders stats in the resource, don't inject them here. stats['vfolders'] = VirtualFolder.get_stats_for( directory.pootle_path, all_vfolders=is_admin ) ctx.update({ 'parent': get_parent(directory if store is None else store), 'translation_project': translation_project, 'project': project, 'language': language, 'stats': jsonify(stats), 'is_admin': is_admin, 'browser_extends': 'translation_projects/base.html', }) response = render(request, 'browser/overview.html', ctx) if cookie_data: response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response
def browse(request, translation_project, dir_path, filename=None): project = translation_project.project language = translation_project.language directory = request.directory store = request.store is_admin = check_permission("administrate", request) ctx, cookie_data = get_sidebar_announcements_context(request, project.code, language.code) ctx.update(get_browser_context(request)) # TODO improve plugin logic if "import_export" in settings.INSTALLED_APPS and request.user.is_authenticated(): from import_export.views import handle_upload_form ctx.update(handle_upload_form(request)) has_download = not translation_project.is_terminology_project and ( check_permission("translate", request) or check_permission("suggest", request) ) ctx.update({"display_download": has_download, "has_sidebar": True}) stats = request.resource_obj.get_stats() if store is None: table_fields = [ "name", "progress", "total", "need-translation", "suggestions", "critical", "last-updated", "activity", ] ctx.update( { "table": { "id": "tp", "fields": table_fields, "headings": get_table_headings(table_fields), "items": get_children(directory), } } ) if "virtualfolder" in settings.INSTALLED_APPS: vfolders = get_vfolders(directory, all_vfolders=is_admin) if len(vfolders) > 0: table_fields = [ "name", "priority", "progress", "total", "need-translation", "suggestions", "critical", "activity", ] ctx.update( { "vfolders": { "id": "vfolders", "fields": table_fields, "headings": get_table_headings(table_fields), "items": vfolders, } } ) # FIXME: set vfolders stats in the resource, don't inject them here. stats["vfolders"] = {} for vfolder_treeitem in directory.vf_treeitems.iterator(): if request.user.is_superuser or vfolder_treeitem.is_visible: stats["vfolders"][vfolder_treeitem.code] = vfolder_treeitem.get_stats(include_children=False) ctx.update( { "parent": get_parent(directory if store is None else store), "translation_project": translation_project, "project": project, "language": language, "stats": jsonify(stats), "is_admin": is_admin, "browser_extends": "translation_projects/base.html", } ) response = render(request, "browser/index.html", ctx) if cookie_data: response.set_cookie(SIDEBAR_COOKIE_NAME, cookie_data) return response