本文整理汇总了Python中transifex.projects.permissions.project.ProjectPermission.maintain方法的典型用法代码示例。如果您正苦于以下问题:Python ProjectPermission.maintain方法的具体用法?Python ProjectPermission.maintain怎么用?Python ProjectPermission.maintain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transifex.projects.permissions.project.ProjectPermission
的用法示例。
在下文中一共展示了ProjectPermission.maintain方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_user_perms
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def _get_user_perms(self, user, project, resource, language,
team, checksum, is_maintainer):
"""
Get permissions for a user.
Args:
user: A User instance
project: A Project instance
resource: A Resource instance
language: A Language instance
team: A Team instance
checksum: An md5 checksum representing a source entity
is_maintiner: A boolean
Returns:
A dictionary containing various user permissions
"""
check = ProjectPermission(user)
can_review = check.proofread(project, language)
can_submit_translations = check.submit_translations(
team or resource.project)
accept_translations = resource.accept_translations
return {
'can_review': can_review,
'can_submit_translations': can_submit_translations,
'accept_translations': accept_translations,
'is_maintainer': check.maintain(project),
}
示例2: _update
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def _update(self, request, project_slug, resource_slug, lang_code=None):
# Permissions handling
try:
resource = Resource.objects.get(
slug=resource_slug, project__slug=project_slug
)
except Resource.DoesNotExist:
return rc.NOT_FOUND
if lang_code == "source":
language = resource.source_language
else:
try:
language = Language.objects.by_code_or_alias(lang_code)
except Language.DoesNotExist:
logger.error("Weird! Selected language code (%s) does "
"not match with any language in the database."
% lang_code)
return BAD_REQUEST(
"Selected language code (%s) does not match with any"
"language in the database." % lang_code
)
team = Team.objects.get_or_none(resource.project, lang_code)
check = ProjectPermission(request.user)
if (not check.submit_translations(team or resource.project) or\
not resource.accept_translations) and not\
check.maintain(resource.project):
return rc.FORBIDDEN
try:
t = Translation.get_object("create", request, resource, language)
res = t.create()
except BadRequestError, e:
return BAD_REQUEST(unicode(e))
示例3: exit
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def exit(request, project_slug, lang_code, resource_slug=None, *args, **kwargs):
"""
Exiting Lotte
"""
if request.method != 'POST':
return HttpResponse(status=405)
# Permissions handling
# Project should always be available
project = get_object_or_404(Project, slug=project_slug)
team = Team.objects.get_or_none(project, lang_code)
check = ProjectPermission(request.user)
if not check.submit_translations(team or project) and not\
check.maintain(project):
return permission_denied(request)
language = Language.objects.by_code_or_alias(lang_code)
resources = []
if resource_slug:
resources = Resource.objects.filter(slug=resource_slug, project=project)
if not resources:
raise Http404
else:
resources = Resource.objects.filter(project=project)
data = simplejson.loads(request.raw_post_data)
if data.get('updated'):
modified = True
# ActionLog & Notification
for resource in resources:
nt = 'project_resource_translated'
context = {'project': project,
'resource': resource,
'language': language,
'sender': request.user}
object_list = [project, resource, language]
if team:
object_list.append(team)
action_logging(request.user, object_list, nt, context=context)
else:
modified = False
lotte_done.send(None, request=request, resources=resources,
language=language, modified=modified)
redirect_url = reverse('team_detail', args=[project_slug, language.code])
if request.is_ajax():
json = simplejson.dumps(dict(redirect=redirect_url))
return HttpResponse(json, mimetype='application/json')
return HttpResponseRedirect(redirect_url)
示例4: project_delete_permission_request
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def project_delete_permission_request(request, project_slug, permission_pk):
"""
View for deleting a request of permission of a user.
This view is an abstraction of a txpermissions.views method.
"""
project, permission=_get_project_and_permission(project_slug, permission_pk)
# It's necessary to distinguish between maintainer and normal users that
# did the request
if request.user.id==permission.user.id:
notice_type = 'project_submit_access_request_withdrawn'
sendto = project.maintainers.all()
else:
notice_type = 'project_submit_access_request_denied'
sendto = [permission.user]
notice = {
'type': notice_type,
'object': project,
'sendto': sendto,
'extra_context': {'project': project,
'user_request': permission.user,
'user_action': request.user,
},
}
check = ProjectPermission(request.user)
if check.maintain(project) or \
request.user.has_perm('authority.delete_permission') or \
request.user.pk == permission.creator.pk:
return delete_permission_or_request(request, permission, False,
extra_context={ 'notice': notice },)
check = ProjectPermission(request.user)
if check.maintain(project) or \
request.user.has_perm('authority.delete_permission') or \
request.user.pk == permission.creator.pk:
return delete_permission_or_request(request, permission, False)
return permission_denied(request)
示例5: update
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def update(self, request, project_slug, resource_slug,
language_code, api_version=2):
"""
Update existing translations for multiple source entities
of a resource at one go by permitted user(s).
"""
try:
project, resource, language = \
self._requested_objects(
project_slug, resource_slug, language_code)
self._validate_language_is_not_source_language(
project.source_language, language)
translations = request.data
self._validate_translations_json_data(translations)
team = Team.objects.get_or_none(project, language.code)
check = ProjectPermission(request.user)
# User must be a member of the project
is_maintainer = check.maintain(project)
# Allow only project members to issue this update request
if not is_maintainer and not (check.submit_translations(
team or project) or check.proofread(project, language)):
return FORBIDDEN_REQUEST(
"You are not allowed to update translations.")
trans_obj_dict = self._translations_as_dict(
translations, resource, language)
updated_translations = []
se_ids = []
for translation in translations:
# All permission checks for a user is done here and
# updated translations are collected in updated_tranlsations
# and source_entity.id in se_ids
self._process_translation_dict(translation, project, resource,
language, team, check, is_maintainer, request.user,
se_ids, updated_translations, trans_obj_dict)
self._update_translations(updated_translations)
keys = ['key', 'context', 'translation',
'reviewed', 'pluralized', 'wordcount',
'last_update', 'user', 'position', 'occurrences',]
field_map, fields = self._get_update_fieldmap_and_fields(keys)
return self._generate_translations_dict(
Translation.objects.filter( source_entity__id__in=se_ids,
language=language).values(*fields), field_map)
except NotFoundError, e:
return NOT_FOUND_REQUEST(unicode(e))
示例6: exit
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def exit(request, project_slug, lang_code, resource_slug=None, *args, **kwargs):
"""
Exiting Lotte
"""
# Permissions handling
# Project should always be available
project = get_object_or_404(Project, slug=project_slug)
team = Team.objects.get_or_none(project, lang_code)
check = ProjectPermission(request.user)
if not check.submit_translations(team or project) and not\
check.maintain(project):
return permission_denied(request)
language = Language.objects.by_code_or_alias(lang_code)
resources = []
if resource_slug:
resources = Resource.objects.filter(slug=resource_slug, project=project)
if not resources:
raise Http404
url = reverse('resource_detail', args=[project_slug, resource_slug])
else:
resources = Resource.objects.filter(project=project)
url = reverse('project_detail', args=[project_slug])
if request.POST.get('updated', None) == 'true':
modified = True
# ActionLog & Notification
for resource in resources:
nt = 'project_resource_translated'
context = {'project': project,
'resource': resource,
'language': language}
object_list = [project, resource, language]
action_logging(request.user, object_list, nt, context=context)
if settings.ENABLE_NOTICES:
txnotification.send_observation_notices_for(project,
signal=nt, extra_context=context)
else:
modified = False
lotte_done.send(None, request=request, resources=resources,
language=language, modified=modified)
if request.is_ajax():
json = simplejson.dumps(dict(redirect=url))
return HttpResponse(json, mimetype='application/json')
return HttpResponseRedirect(url)
示例7: update_translation
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def update_translation(request, project_slug, resource_slug, lang_code=None):
"""Ajax view that gets an uploaded translation as a file and saves it.
If the language is not specified, the translation does not exist yet.
Othewise, this is an update.
Returns:
Either an error message, or nothing for success.
"""
resource = get_object_or_404(
Resource.objects.select_related('project'),
project__slug=project_slug, slug=resource_slug
)
if lang_code is None:
lang_code = request.POST.get('language_code', None)
target_language = get_object_or_404(Language, code=lang_code)
project = resource.project
# Get the team if exists to use it for permissions and links
team = Team.objects.get_or_none(project, lang_code)
check = ProjectPermission(request.user)
if (not check.submit_translations(team or resource.project) or\
not resource.accept_translations) and not\
check.maintain(resource.project):
return HttpResponse(
simplejson.dumps({
'msg': _("You are not allowed to upload a translation."),
'status': 403,
}),
status=403, content_type='text/plain'
)
content = content_from_uploaded_file(request.FILES)
try:
_save_translation(resource, target_language, request.user, content)
except FormatsBackendError, e:
return HttpResponse(
simplejson.dumps({
'msg': unicode(e),
'status': 400,
}),
status=400, content_type='text/plain'
)
示例8: add_edit_developer_comment_extra
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def add_edit_developer_comment_extra(request, project_slug, *args, **kwargs):
"""
View for handling AJAX calls from Lotte in order to add/edit the
developer comment for a source entity.
Only maintainers can edit it.
"""
# Permissions handling
project = get_object_or_404(Project, slug=project_slug)
check = ProjectPermission(request.user)
if not check.maintain(project):
content = {'error': True, 'message': _('Permission error.')}
elif not request.POST:
content = {'error': True, 'message': _('Bad request.')}
else:
previous_comment = None
try:
se = SourceEntity.objects.get(
id=request.POST.get('source_entity_id', None),
resource__project=project)
previous_comment_extra = se.developer_comment_extra
se.developer_comment_extra = request.POST.get('comment_extra', '')
se.save()
content = {
'error': False,
'comment': se.developer_comment,
'comment_extra': se.developer_comment_extra,
}
except SourceEntity.DoesNotExist:
content = {
'error': True,
'message': _('No such Source Entity for the given project.'),
}
except Exception, e:
logger.error('Lotte: Error while editing developer comment: %s' %
(e.message or str(e)))
content = {
'error': True,
'message': _('Ops! Something weird happened. The admins '
'were notified about it.'),
}
示例9: push_translation
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def push_translation(request, project_slug, lang_code, *args, **kwargs):
"""
Client pushes an id and a translation string.
Id is considered to be of the source translation string and the string is
in the target_lang.
FIXME: Document in detail the form of the 'strings' POST variable.
"""
logger.debug("POST data when saving translation: %s" % request.POST)
# Permissions handling
# Project should always be available
project = get_object_or_404(Project, slug=project_slug)
team = Team.objects.get_or_none(project, lang_code)
check = ProjectPermission(request.user)
if not check.submit_translations(team or project) and not\
check.maintain(project):
return permission_denied(request)
if not request.POST:
return HttpResponseBadRequest()
data = simplejson.loads(request.raw_post_data)
strings = data["strings"]
try:
target_language = Language.objects.by_code_or_alias(lang_code)
except Language.DoesNotExist:
raise Http404
# This dictionary will hold the results of the save operation and will map
# status code for each translation pushed, to indicate the result on each
# translation push separately.
push_response_dict = {}
# Form the strings dictionary, get as Json object
# The fields are the following:
# id-> source_entity id
# translations-> translation strings (includes all plurals)
# context-> source_entity context
# occurrence-> occurrence (not yet well supported)
# Iterate through all the row data that have been sent.
for row in strings:
source_id = int(row['id'])
try:
source_string = Translation.objects.select_related(depth=1).get(
id=source_id
)
except Translation.DoesNotExist:
# TODO: Log or inform here
push_response_dict[source_id] = { 'status':400,
'message':_("Source string cannot be identified in the DB")}
# If the source_string cannot be identified in the DB then go to next
# translation pair.
continue
if not source_string.resource.accept_translations:
push_response_dict[source_id] = { 'status':400,
'message':_("The resource of this source string is not "
"accepting translations.") }
# If the translated source string is pluralized check that all the
# source language supported rules have been filled in, else return error
# and donot save the translations.
if source_string.source_entity.pluralized:
error_flag = False
for rule in target_language.get_pluralrules():
if rule in row['translations'] and row['translations'][rule] != "":
continue
else:
error_flag = True
if error_flag:
error_flag = False
# Check also if all of them are "". If yes, delete all the plurals!
for rule in target_language.get_pluralrules():
if rule in row['translations'] and row['translations'][rule] == "":
continue
else:
error_flag = True
if error_flag:
push_response_dict[source_id] = { 'status':400,
'message':(_("Cannot save unless plural translations are either "
"completely specified or entirely empty!"))}
# Skip the save as we hit on an error.
continue
try:
msgs = _save_translation(
source_string, row['translations'],
target_language, request.user
)
if not msgs:
push_response_dict[source_id] = {'status': 200}
else:
push_response_dict[source_id] = {
'status': 200, 'message': msgs[-1]
}
except LotteBadRequestError, e:
push_response_dict[source_id] = {
'status': 400, 'message': e.message
#.........这里部分代码省略.........
示例10: translate
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def translate(request, project_slug, lang_code, resource_slug=None,
*args, **kwargs):
"""
Main lotte view.
"""
# Permissions handling
# Project should always be available
project = get_object_or_404(Project, slug=project_slug)
team = Team.objects.get_or_none(project, lang_code)
check = ProjectPermission(request.user)
if not check.submit_translations(team or project) and not\
check.maintain(project):
return permission_denied(request)
resources = []
if resource_slug:
resource_list = [get_object_or_404(Resource, slug=resource_slug,
project=project)]
else:
resource_list = Resource.objects.filter(project=project)
# Return a page explaining that the project has multiple source langs and
# cannot be translated as a whole.
if resource_list.values('source_language').distinct().count() > 1:
messages.info(request,_(
"There are multiple source languages for this project. "
"You will only be able to translate resources for one "
"source language at a time."))
return HttpResponseRedirect(reverse('project_detail',
args=[project_slug]),)
# Filter resources that are not accepting translations
for resource in resource_list:
if resource.accept_translations:
resources.append(resource)
# If no resource accepting translations, raise a 403
if not resources:
return permission_denied(request)
target_language = Language.objects.by_code_or_alias_or_404(lang_code)
# If it is an attempt to edit the source language, redirect the user to
# resource_detail and show him a message explaining the reason.
if target_language == get_source_language(resources):
messages.error(request,_(
"Cannot edit the source language because this would "
"result in translation mismatches! If you want to "
"update the source strings consider using the transifex "
"command-line client."))
if resource_slug:
return HttpResponseRedirect(reverse('resource_detail',
args=[project_slug,
resource_slug]),)
else:
return HttpResponseRedirect(reverse('project_detail',
args=[project_slug]),)
total_strings = SourceEntity.objects.filter(
resource__in = resources).count()
translated_strings = Translation.objects.filter(
resource__in = resources,
language = target_language,
source_entity__pluralized=False,
rule = 5).count()
# Include counting of pluralized entities
for pluralized_entity in SourceEntity.objects.filter(resource__in = resources,
pluralized=True):
plurals_translated = Translation.objects.filter(
language=target_language,
source_entity=pluralized_entity).count()
if plurals_translated == len(target_language.get_pluralrules()):
translated_strings += 1
if len(resources) > 1:
translation_resource = None
else:
translation_resource = resources[0]
contributors = User.objects.filter(pk__in=Translation.objects.filter(
resource__in = resources,
language = target_language,
rule = 5).values_list("user", flat=True))
lotte_init.send(None, request=request, resources=resources,
language=target_language)
if target_language in [team.language for team in project.available_teams]:
team_language = True
else:
team_language = False
GtModel = get_model('gtranslate', 'Gtranslate')
try:
auto_translate = GtModel.objects.get(project=project)
except GtModel.DoesNotExist:
auto_translate = None
#.........这里部分代码省略.........
示例11: resource_translation_toggle_watch
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def resource_translation_toggle_watch(request, project_slug, resource_slug, language_code):
"""Add/Remove a TranslationWatch for a specific user."""
if request.method != 'POST':
return json_error(_('Must use POST to activate'))
if not settings.ENABLE_NOTICES:
return json_error(_('Notification is not enabled'))
resource = get_object_or_404(Resource, slug=resource_slug,
project__slug=project_slug)
project = resource.project
language = get_object_or_404(Language, code=language_code)
team = Team.objects.get_or_none(project, language_code)
check = ProjectPermission(request.user)
if not check.submit_translations(team or project) and not \
check.maintain(project) and not \
request.user.has_perm('watches.add_translationwatch') and not \
request.user.has_perm('watches.delete_translationwatch'):
return permission_denied(request)
url = reverse('resource_translation_toggle_watch', args=(project_slug,
resource_slug, language_code))
try:
twatch = TranslationWatch.objects.get(resource=resource,
language=language)
result = {
'style': 'watch_add',
'title': _('Watch it'),
'id': twatch.id,
'url': url,
'error': None,
}
notification.stop_observing(twatch, request.user,
signal='project_resource_translation_changed')
except (TranslationWatch.DoesNotExist,
notification.ObservedItem.DoesNotExist):
try:
twatch = TranslationWatch.objects.get_or_create(resource=resource,
language=language)[0]
result = {
'style': 'watch_remove',
'title': _('Stop watching'),
'id': twatch.id,
'url': url,
'error': None,
}
notification.observe(twatch, request.user,
'project_resource_translation_changed',
signal='project_resource_translation_changed')
except WatchException, e:
return json_error(e.message, result)
示例12: update
# 需要导入模块: from transifex.projects.permissions.project import ProjectPermission [as 别名]
# 或者: from transifex.projects.permissions.project.ProjectPermission import maintain [as 别名]
def update(self, request, project_slug, resource_slug, language_code=None, api_version=1):
"""
Update resource translations of a project by the UUID of a StorageFile.
"""
try:
project = Project.objects.get(slug=project_slug)
resource = Resource.objects.get(slug=resource_slug, project=project)
except (Project.DoesNotExist, Resource.DoesNotExist):
return rc.NOT_FOUND
# Permissions handling
team = Team.objects.get_or_none(project, language_code)
check = ProjectPermission(request.user)
if (not check.submit_translations(team or project) or not resource.accept_translations) and not check.maintain(
project
):
return rc.FORBIDDEN
if "application/json" in request.content_type:
if "uuid" in request.data:
uuid = request.data["uuid"]
storagefile = StorageFile.objects.get(uuid=uuid)
language = storagefile.language
logger.debug(
"Going to insert strings from %s (%s) to %s/%s"
% (storagefile.name, storagefile.uuid, project_slug, resource.slug)
)
strings_added, strings_updated = 0, 0
parser = storagefile.find_parser()
language = storagefile.language
fhandler = parser(filename=storagefile.get_storage_path())
fhandler.set_language(language)
fhandler.bind_resource(resource)
fhandler.contents_check(fhandler.filename)
try:
fhandler.parse_file()
strings_added, strings_updated = fhandler.save2db(user=request.user)
except Exception, e:
logger.error(e.message, exc_info=True)
return BAD_REQUEST("Error importing file: %s" % e)
else:
messages = []
if strings_added > 0:
messages.append(_("%i strings added") % strings_added)
if strings_updated > 0:
messages.append(_("%i strings updated") % strings_updated)
retval = {
"strings_added": strings_added,
"strings_updated": strings_updated,
"redirect": reverse("resource_detail", args=[project_slug, resource.slug]),
}
logger.debug("Extraction successful, returning: %s" % retval)
# Set StorageFile to 'bound' status, which means that it is
# bound to some translation resource
storagefile.bound = True
storagefile.save()
# If any string added/updated
if retval["strings_added"] > 0 or retval["strings_updated"] > 0:
modified = True
else:
modified = False
post_submit_translation.send(
None, request=request, resource=resource, language=language, modified=modified
)
return HttpResponse(simplejson.dumps(retval), mimetype="application/json")
else:
return BAD_REQUEST("Missing request data.")