def _test_tp_match(source_tp, target_tp, project=None, update=False): source_stores = [] for store in source_tp.stores.live(): source_stores.append(store.pootle_path) project_code = ( project and project.code or source_tp.project.code) store_path = "".join(split_pootle_path(store.pootle_path)[2:]) update_path = ( "/%s/%s/%s" % (target_tp.language.code, project_code, store_path)) updated = Store.objects.get(pootle_path=update_path) assert store.state == updated.state updated_units = updated.units for i, unit in enumerate(store.units): updated_unit = updated_units[i] assert unit.source == updated_unit.source assert unit.target == updated_unit.target assert unit.state == updated_unit.state assert unit.getcontext() == updated_unit.getcontext() assert unit.getlocations() == updated_unit.getlocations() assert unit.hasplural() == updated_unit.hasplural() for store in target_tp.stores.live(): store_path = "".join(split_pootle_path(store.pootle_path)[2:]) source_path = ( "/%s/%s/%s" % (source_tp.language.code, source_tp.project.code, store_path)) assert source_path in source_stores if not update: assert source_tp.stores.count() == target_tp.stores.count()
def _test_tp_match(source_tp, target_tp, project=None, update=False): source_stores = [] for store in source_tp.stores.live(): source_stores.append(store.pootle_path) project_code = (project and project.code or source_tp.project.code) store_path = "".join(split_pootle_path(store.pootle_path)[2:]) update_path = ("/%s/%s/%s" % (target_tp.language.code, project_code, store_path)) updated = Store.objects.get(pootle_path=update_path) assert store.state == updated.state updated_units = updated.units for i, unit in enumerate(store.units): updated_unit = updated_units[i] assert unit.source == updated_unit.source assert unit.target == updated_unit.target assert unit.state == updated_unit.state assert unit.getcontext() == updated_unit.getcontext() assert unit.getlocations() == updated_unit.getlocations() assert unit.hasplural() == updated_unit.hasplural() # # these tests dont work yet # assert unit.created_by == updated_unit.created_by # assert unit.submitted_by == updated_unit.submitted_by # assert unit.reviewed_by == updated_unit.reviewed_by for store in target_tp.stores.live(): store_path = "".join(split_pootle_path(store.pootle_path)[2:]) source_path = ( "/%s/%s/%s" % (source_tp.language.code, source_tp.project.code, store_path)) assert source_path in source_stores if not update: assert source_tp.stores.count() == target_tp.stores.count()
def test_split_pootle_path(): """Tests pootle path are properly split.""" assert split_pootle_path("") == (None, None, "", "") assert split_pootle_path("/projects/") == (None, None, "", "") assert split_pootle_path("/projects/tutorial/") == (None, "tutorial", "", "") assert split_pootle_path("/pt/tutorial/tutorial.po") == ("pt", "tutorial", "", "tutorial.po") assert split_pootle_path("/pt/tutorial/foo/tutorial.po") == ("pt", "tutorial", "foo/", "tutorial.po")
def test_split_pootle_path(): """Tests pootle path are properly split.""" assert split_pootle_path('') == (None, None, '', '') assert split_pootle_path('/projects/') == (None, None, '', '') assert split_pootle_path('/projects/tutorial/') == \ (None, 'tutorial', '', '') assert split_pootle_path('/pt/tutorial/tutorial.po') == \ ('pt', 'tutorial', '', 'tutorial.po') assert split_pootle_path('/pt/tutorial/foo/tutorial.po') == \ ('pt', 'tutorial', 'foo/', 'tutorial.po')
def parse_vfolder_rules(vf): languages = set() projects = set() new_rules = set() full_rules = [vf.location.strip() + rule.strip() for rule in vf.filter_rules.split(",")] for full_rule in full_rules: lang_code, proj_code, dir_path, filename = split_pootle_path(full_rule) if filename: new_rules.add(dir_path + filename) else: new_rules.add(dir_path + "*") languages.add(lang_code) projects.add(proj_code) if "{LANG}" in languages: languages = set() if "{PROJ}" in projects: projects = set() new_rules=",".join(new_rules) return languages, projects, new_rules
def edit_settings(request, translation_project): form = DescriptionForm(request.POST, instance=translation_project) response = {} rcode = 400 if form.is_valid(): form.save() rcode = 200 if translation_project.description: response["description"] = translation_project.description else: response["description"] = (u'<p class="placeholder muted">%s</p>' % _(u"No description yet.")) path_args = split_pootle_path(translation_project.pootle_path)[:2] context = { "form": form, "form_action": reverse('pootle-tp-admin-settings', args=path_args), } t = loader.get_template('admin/_settings_form.html') c = RequestContext(request, context) response['form'] = t.render(c) return HttpResponse(jsonify(response), status=rcode, mimetype="application/json")
def parse_vfolder_rules(self, location, old_rules): """Extract languages, projects and new rules from location and rules.""" languages = set() projects = set() new_rules = set() full_rules = [location + old_rule.strip() for old_rule in old_rules] for full_rule in full_rules: lang_code, proj_code, dir_path, fname = split_pootle_path(full_rule) if fname: new_rules.add(dir_path + fname) else: new_rules.add(dir_path + "*") languages.add(lang_code) projects.add(proj_code) if "{LANG}" in languages: languages = set() if "{PROJ}" in projects: projects = set() new_rules = ",".join(new_rules) return languages, projects, new_rules
def top_scorers(self): User = get_user_model() lang_code, proj_code = split_pootle_path(self.pootle_path)[:2] return User.top_scorers( project=proj_code, language=lang_code, limit=TOP_CONTRIBUTORS_CHUNK_SIZE + 1)
def _get_ctx_obj(self, path, user): lang_code, proj_code = split_pootle_path(path)[:2] if proj_code and not lang_code: try: return Project.objects.select_related('directory').get( code=proj_code) except Project.DoesNotExist: return None if lang_code: if proj_code: try: return TranslationProject.objects.get_for_user( user=user, language_code=lang_code, project_code=proj_code, select_related=['directory'], ) except TranslationProject.DoesNotExist: return None try: return Language.objects.select_related('directory').get( code=lang_code) except Language.DoesNotExist: return None return None
def create_by_path(self, pootle_path, project=None, create_tp=True, create_directory=True, **kwargs): from pootle_language.models import Language from pootle_project.models import Project (lang_code, proj_code, dir_path, filename) = split_pootle_path(pootle_path) ext = filename.split(".")[-1] if project is None: project = Project.objects.get(code=proj_code) elif project.code != proj_code: raise ValueError("Project must match pootle_path when provided") if ext not in project.filetype_tool.valid_extensions: raise ValueError("'%s' is not a valid extension for this Project" % ext) if create_tp: tp, created = project.translationproject_set.get_or_create(language=Language.objects.get(code=lang_code)) elif create_directory or not dir_path: tp = project.translationproject_set.get(language__code=lang_code) if dir_path: if not create_directory: parent = Directory.objects.get(pootle_path="/".join(["", lang_code, proj_code, dir_path])) else: parent = tp.directory for child in dir_path.strip("/").split("/"): parent, created = Directory.objects.get_or_create(tp=tp, name=child, parent=parent) else: parent = tp.directory store, created = self.get_or_create(name=filename, parent=parent, translation_project=tp, **kwargs) return store
def dispatch(self, request, *args, **kwargs): stats_form = self.get_form() if not stats_form.is_valid(): raise Http404( ValidationError(stats_form.errors).messages) offset = stats_form.cleaned_data['offset'] or 0 path = stats_form.cleaned_data['path'] language_code, project_code, dir_path, filename = \ split_pootle_path(path) kwargs.update( dict( language_code=language_code, project_code=project_code, dir_path=dir_path, filename=filename, offset=offset, path=path, ) ) view_class = ProjectsContributorsJSON if language_code and project_code: if filename: view_class = TPStoreContributorsJSON else: view_class = TPContributorsJSON elif language_code: view_class = LanguageContributorsJSON elif project_code: view_class = ProjectContributorsJSON return view_class.as_view()(request, *args, **kwargs)
def translate_url(self): return u"%s%s" % ( reverse( "pootle-tp-store-translate", args=split_pootle_path(self.pootle_path) ), "#unit=%s" % str(self.id), )
def revision_context(self): first_child = self.context.children.first() if not first_child: return return Directory.objects.get( pootle_path="/projects/%s/" % split_pootle_path(first_child.pootle_path)[1]).revisions
def decorated_f(request): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. """ pootle_path = request.GET.get("path", None) if pootle_path is None: raise Http400(_("Arguments missing.")) lang, proj, dir_path, filename = split_pootle_path(pootle_path) store = None if filename: try: store = Store.objects.select_related("parent").get(pootle_path=pootle_path) directory = store.parent except Store.DoesNotExist: raise Http404(_("File does not exist.")) else: directory = Directory.objects.get(pootle_path=pootle_path) _check_permissions(request, directory, permission_codes) path_obj = store or directory request.pootle_path = pootle_path return f(request, path_obj)
def decorated_f(request): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. """ pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) lang, proj, dir_path, filename = split_pootle_path(pootle_path) store = None if filename: try: store = Store.objects.select_related( 'parent', ).get(pootle_path=pootle_path) directory = store.parent except Store.DoesNotExist: raise Http404(_('Store does not exist.')) else: directory = Directory.objects.get(pootle_path=pootle_path) _check_permissions(request, directory, permission_codes) path_obj = store or directory request.pootle_path = pootle_path return f(request, path_obj)
def get_context_data(self, *args, **kwargs): filters = {} can_translate = False can_translate_stats = False User = get_user_model() if self.has_vfolders: filters['sort'] = 'priority' if self.request.user.is_superuser or self.language: can_translate = True can_translate_stats = True url_action_continue = self.object.get_translate_url( state='incomplete', **filters) url_action_fixcritical = self.object.get_critical_url( **filters) url_action_review = self.object.get_translate_url( state='suggestions', **filters) url_action_view_all = self.object.get_translate_url(state='all') else: if self.project: can_translate = True url_action_continue = None url_action_fixcritical = None url_action_review = None url_action_view_all = None ctx, cookie_data_ = self.sidebar_announcements ctx.update( super(PootleBrowseView, self).get_context_data(*args, **kwargs)) lang_code, proj_code = split_pootle_path(self.pootle_path)[:2] top_scorers = User.top_scorers( project=proj_code, language=lang_code, limit=TOP_CONTRIBUTORS_CHUNK_SIZE + 1, ) ctx.update( {'page': 'browse', 'stats_refresh_attempts_count': settings.POOTLE_STATS_REFRESH_ATTEMPTS_COUNT, 'stats': self.stats, 'translation_states': get_translation_states(self.object), 'checks': get_qualitycheck_list(self.object), 'can_translate': can_translate, 'can_translate_stats': can_translate_stats, 'url_action_continue': url_action_continue, 'url_action_fixcritical': url_action_fixcritical, 'url_action_review': url_action_review, 'url_action_view_all': url_action_view_all, 'table': self.table, 'is_store': self.is_store, 'top_scorers': top_scorers, 'top_scorers_data': get_top_scorers_data( top_scorers, TOP_CONTRIBUTORS_CHUNK_SIZE), 'browser_extends': self.template_extends}) return ctx
def _test_admin_view(response, project): request = response.wsgi_request qs = TranslationProject.objects.filter( project=project).order_by('pootle_path') page = paginate(request, qs) url_kwargs = {'project_code': project.code, 'dir_path': '', 'filename': ''} assert page.number == response.context["objects"].number assert page.start_index() == response.context["objects"].start_index() assert page.end_index() == response.context["objects"].end_index() assert (list(response.context["objects"].object_list.values_list( "pk", flat=True)) == list(qs.values_list("pk", flat=True))) assert (response.context["formset"].__class__.__name__ == "TranslationProjectFormFormSet") assert response.context["page"] == "admin-languages" assert response.context["browse_url"] == reverse('pootle-project-browse', kwargs=url_kwargs) assert response.context["translate_url"] == reverse( 'pootle-project-translate', kwargs=url_kwargs) assert (response.context['project'] == { 'code': project.code, 'name': project.fullname, 'treestyle': project.treestyle }) assert (response.context["formset_text"] == mark_safe( form_set_as_table( response.context["formset"], lambda tp: (u'<a href="%s">%s</a>' % (reverse('pootle-tp-admin-permissions', args=split_pootle_path(tp.pootle_path)[:2]), tp.language )), "language")))
def items(self): item_func = ( make_xlanguage_item if (self.kwargs['dir_path'] or self.kwargs['filename']) else make_language_item) dir_path, filename = split_pootle_path(self.pootle_path)[2:] if dir_path.count("/"): tp_prefix = "parent__" * dir_path.count("/") selects = [ "%stranslationproject" % tp_prefix, "%stranslationproject" % tp_prefix, "%stranslationproject__language" % tp_prefix] elif not filename: selects = ["language", "directory"] else: selects = None items = [ item_func(item) for item in self.object.get_children_for_user( self.request.profile, select_related=selects)] items.sort( lambda x, y: locale.strcoll(x['title'], y['title'])) return items
def _test_view_tp_children(view, obj): request = view.request if obj.tp_path == "/": data_obj = obj.tp else: data_obj = obj stats = StatsDisplay( data_obj, stats=data_obj.data_tool.get_stats(user=request.user)).stats assert stats == view.stats stores = view.tp.stores if obj.tp_path != "/": stores = stores.filter(tp_path__startswith=obj.tp_path) vf_stores = stores.filter(vfolders__isnull=False).exclude(parent=obj) dirs_with_vfolders = set( split_pootle_path(path)[2].split("/")[0] for path in vf_stores.values_list("pootle_path", flat=True)) directories = [ make_directory_item( child, **(dict( sort="priority") if child.name in dirs_with_vfolders else {})) for child in obj.get_children() if isinstance(child, Directory) ] stores = [ make_store_item(child) for child in obj.get_children() if isinstance(child, Store) ] items = directories + stores for item in items: if item["code"] in stats["children"]: item["stats"] = stats["children"][item["code"]] elif item["title"] in stats["children"]: item["stats"] = stats["children"][item["title"]] assert view.object_children == items
def parse_vfolder_rules(vf): languages = set() projects = set() new_rules = set() full_rules = [ vf.location.strip() + rule.strip() for rule in vf.filter_rules.split(",") ] for full_rule in full_rules: lang_code, proj_code, dir_path, filename = split_pootle_path(full_rule) if filename: new_rules.add(dir_path + filename) else: new_rules.add(dir_path + "*") languages.add(lang_code) projects.add(proj_code) if "{LANG}" in languages: languages = set() if "{PROJ}" in projects: projects = set() new_rules = ",".join(new_rules) return languages, projects, new_rules
def test_split_pootle_path(): """Tests pootle path are properly split.""" assert split_pootle_path("") == (None, None, "", "") assert split_pootle_path("/projects/") == (None, None, "", "") assert split_pootle_path("/projects/tutorial/") == (None, "tutorial", "", "") assert split_pootle_path("/pt/tutorial/tutorial.po") == ( "pt", "tutorial", "", "tutorial.po", ) assert split_pootle_path("/pt/tutorial/foo/tutorial.po") == ( "pt", "tutorial", "foo/", "tutorial.po", )
def get_drill_down_url_for_path(self, pootle_path): """Return this goal's drill down URL for the given path. :param pootle_path: A string with a valid pootle path. """ lang, proj, dir_path, filename = split_pootle_path(pootle_path) reverse_args = [lang, proj, self.slug, dir_path, filename] return reverse('pootle-tp-goal-drill-down', args=reverse_args)
def find(self): for pootle_path in pootle_paths: matched = dict() (matched['language_code'], __, matched['dir_path'], matched['filename']) = split_pootle_path(pootle_path) matched["ext"] = "po" matched['filename'] = os.path.splitext(matched["filename"])[0] yield plugin.get_fs_path(pootle_path), matched
def get_search_locations_url(self): (proj_code, dir_path, filename) = split_pootle_path(self.store.pootle_path)[1:] return u''.join([ reverse('pootle-project-translate', args=[proj_code, dir_path, filename]), get_editor_filter(search=self.locations, sfields='locations'), ])
def get_translate_url(self, **kwargs): return u"".join( [ reverse( "pootle-tp-translate", args=split_pootle_path(self.pootle_path)[:-1] ), get_editor_filter(**kwargs), ] )
def get_translate_url(self, pootle_path, **kwargs): """Get the translate URL for this virtual folder in the given path.""" adjusted_path = self.get_adjusted_pootle_path(pootle_path) lang, proj, dp, fn = split_pootle_path(adjusted_path) return u''.join([ reverse('pootle-tp-translate', args=[lang, proj, dp, fn]), get_editor_filter(**kwargs), ])
def get_fs_path(self, pootle_path): from pootle.core.url_helpers import split_pootle_path lang_code, proj_code_, dir_path, filename = split_pootle_path(pootle_path) parts = ["", lang_code] if dir_path: parts.append(dir_path.rstrip("/")) parts.append(filename) return os.sep.join(parts)
def unit_translate_url(self): if not self.unit: return store_url = u''.join([ reverse("pootle-tp-store-translate", args=split_pootle_path(self.unit_pootle_path)), get_editor_filter() ]) return ("%s%s" % (store_url, '#unit=%s' % unicode(self.unit)))
def pootle_path(self): (language_code, project_code, dir_path, filename) = split_pootle_path(self.path) if language_code and project_code: return ("/%s/%s/" % (language_code, project_code)) elif language_code: return "/%s/" % language_code elif project_code: return "/projects/%s/" % project_code
def get_fs_path(self, pootle_path): from pootle.core.url_helpers import split_pootle_path lang_code, proj_code_, dir_path, filename = split_pootle_path(pootle_path) parts = ["", lang_code] if dir_path: parts.append(dir_path.rstrip("/")) parts.append(filename) return "/".join(parts)
def get_translate_url_for_path(self, pootle_path, **kwargs): """Return this goal's translate URL for the given path. :param pootle_path: A string with a valid pootle path. """ lang, proj, dir_path, fn = split_pootle_path(pootle_path) return u''.join([ reverse('pootle-tp-translate', args=[lang, proj, dir_path, fn]), get_editor_filter(goal=self.slug, **kwargs), ])
def unit_translate_url(self): if not self.unit: return store_url = u"".join([ reverse( "pootle-tp-store-translate", args=split_pootle_path(self.unit_pootle_path), ), get_editor_filter(), ]) return "%s%s" % (store_url, "#unit=%s" % str(self.unit))
def get_absolute_url(self): proj_code = split_pootle_path(self.pootle_path)[1] if proj_code is not None: pattern_name = 'pootle-project-browse' pattern_args = [proj_code, ''] else: pattern_name = 'pootle-projects-browse' pattern_args = [] return reverse(pattern_name, args=pattern_args)
def get_absolute_url(self): lang, proj, dir, fn = split_pootle_path(self.pootle_path) if proj is not None: pattern_name = 'pootle-project-browse' pattern_args = [proj, ''] else: pattern_name = 'pootle-projects-browse' pattern_args = [] return reverse(pattern_name, args=pattern_args)
def pootle_path(self): (language_code, project_code, dir_path, filename) = split_pootle_path(self.path) if language_code and project_code: return ( "/%s/%s/" % (language_code, project_code)) elif language_code: return "/%s/" % language_code elif project_code: return "/projects/%s/" % project_code
def reverse_match(self, pootle_path): lang_code, __, dir_path, filename = split_pootle_path(pootle_path) lang_code = self.lang_mapper.get_upstream_code(lang_code) fileparts = os.path.splitext(filename) fs_path = self.get_finder().reverse_match( lang_code, filename=fileparts[0], extension=fileparts[1].lstrip("."), dir_path=dir_path.strip("/")) if fs_path: return "/%s" % self.relative_path(fs_path).lstrip("/")
def unit_translate_url(self): if not self.unit: return store_url = u''.join( [reverse("pootle-tp-store-translate", args=split_pootle_path(self.unit_pootle_path)), get_editor_filter()]) return ( "%s%s" % (store_url, '#unit=%s' % unicode(self.unit)))
def get_parent_paths(self, pootle_paths): paths = set(["/projects/"]) projects = set() for pootle_path in pootle_paths: lang_code, proj_code, dir_path, __ = split_pootle_path(pootle_path) paths.add("/projects/%s/" % proj_code) projects.add(proj_code) tps = TranslationProject.objects.filter( project__code__in=projects).values_list("language__code", flat=True) for lang_code in tps.iterator(): paths.add("/%s/" % lang_code) return paths
def invalidate_resources_cache(sender, instance, **kwargs): if instance.__class__.__name__ not in ['Directory', 'Store']: return # Don't invalidate if the save didn't create new objects if (('created' in kwargs and 'raw' in kwargs) and (not kwargs['created'] or kwargs['raw'])): return lang, proj, dir, fn = split_pootle_path(instance.pootle_path) if proj is not None: cache.delete(make_method_key(Project, 'resources', proj))
def score_context(self): (language_code, project_code, dir_path, filename) = split_pootle_path(self.path) if language_code and project_code: return self.object.translationproject elif language_code: return Language.objects.get(code=language_code) elif project_code: return Project.objects.get(code=project_code) return ProjectSet( Project.objects.for_user( self.request.user).select_related("directory"))
def setup_store(pootle_path): from pootle.core.url_helpers import split_pootle_path from pootle_translationproject.models import TranslationProject from .factories import StoreDBFactory (lang_code, proj_code, dir_path, filename) = split_pootle_path(pootle_path) tp = TranslationProject.objects.get( language__code=lang_code, project__code=proj_code) return StoreDBFactory( translation_project=tp, parent=tp.directory, name=filename)
def score_context(self): (language_code, project_code, dir_path, filename) = split_pootle_path(self.path) if language_code and project_code: return self.object.translationproject elif language_code: return Language.objects.get(code=language_code) elif project_code: return Project.objects.get(code=project_code) return ProjectSet( Project.objects.for_user(self.request.user) .select_related("directory"))
def commit_to_vcs(request, path_obj, **kwargs): if (path_obj.abs_real_path and check_permission('commit', request) and hasversioning(path_obj.abs_real_path)): link = reverse('pootle-vcs-commit', args=split_pootle_path(path_obj.pootle_path)) text = _('Commit to VCS') return { 'icon': 'icon-vcs-commit', 'href': link, 'text': text, }
def make_empty_directories_obsolete(apps, schema_editor): Directory = apps.get_model("pootle_app", "Directory") from pootle.core.url_helpers import split_pootle_path for d in Directory.objects.filter(child_stores__isnull=True, child_dirs__isnull=True, obsolete=False): lang_code, prj_code, dir_path, fname = split_pootle_path(d.pootle_path) # makeobsolete translation project directories and lower # and do not touch language and project directories if lang_code and prj_code: make_dir_obsolete(d)
def commit_dir_to_vcs(request, path_obj, **kwargs): if (path_obj.get_real_path() and check_permission('commit', request) and hasversioning(path_obj.get_real_path())): link = reverse('pootle-vcs-commit', args=split_pootle_path(path_obj.pootle_path)) # Translators: "all" here refers to all files and sub directories in a directory/project. text = _('Commit all to VCS') return { 'icon': 'icon-vcs-commit', 'href': link, 'text': text, }