Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def rate_wrap(request, *args, **kwargs):
if request.method == 'POST' and not check_rate_limit(scope, request):
# Rotate session token
rotate_token(request)
# Logout user
do_logout = request.user.is_authenticated
if do_logout:
logout(request)
messages.error(
request,
render_to_string(
'ratelimit.html', {'do_logout': do_logout}
)
)
return redirect('login')
return function(request, *args, **kwargs)
return rate_wrap
def mail_admins_contact(request, subject, message, context, sender, to):
"""Send a message to the admins, as defined by the ADMINS setting."""
LOGGER.info('contact form from %s', sender)
if not to and settings.ADMINS:
to = [a[1] for a in settings.ADMINS]
elif not settings.ADMINS:
messages.error(request, _('Message could not be sent to administrator!'))
LOGGER.error('ADMINS not configured, can not send message!')
return
mail = EmailMultiAlternatives(
'{0}{1}'.format(settings.EMAIL_SUBJECT_PREFIX, subject % context),
'{}\n{}'.format(
message % context,
TEMPLATE_FOOTER.format(
address=get_ip_address(request),
agent=get_user_agent(request),
username=request.user.username,
),
),
to=to,
headers={'Reply-To': sender},
)
# Log error
if self.id:
Change.objects.create(
component=self,
action=action_failed,
target=error,
user=request.user if request else None,
details={"error": error, "status": status},
)
self.add_alert("MergeFailure", error=error)
# Reset repo back
method_func(abort=True)
# Tell user (if there is any)
messages.error(request, error_msg % force_text(self))
return False
if self.id:
Change.objects.create(
component=self,
action=action,
user=request.user if request else None,
)
# Run post update hook, this should be done with repo lock held
# to avoid posssible race with another update
vcs_post_update.send(
sender=self.__class__, component=self, previous_head=previous_head
)
self.delete_alert("MergeFailure")
def show_language(request, lang):
try:
obj = Language.objects.get(code=lang)
except Language.DoesNotExist:
obj = Language.objects.fuzzy_get(lang)
if isinstance(obj, Language):
return redirect(obj)
raise Http404('No Language matches the given query.')
if request.method == 'POST' and request.user.has_perm('language.edit'):
if obj.translation_set.exists():
messages.error(
request, _('Remove all translations using this language first.')
)
else:
obj.delete()
messages.success(request, _('Language %s removed.') % obj)
return redirect('languages')
last_changes = Change.objects.last_changes(request.user).filter(
translation__language=obj
)[:10]
projects = request.user.allowed_projects
dicts = projects.filter(dictionary__language=obj).distinct()
projects = projects.filter(component__translation__language=obj).distinct()
for project in projects:
project.language_stats = project.stats.get_single_language_stats(obj)
def state_change(request, project, component=None, lang=None):
obj, unit_set, context = parse_url(request, project, component, lang)
if not request.user.has_perm('translation.auto', obj):
raise PermissionDenied()
form = BulkStateForm(request.user, obj, request.POST)
if not form.is_valid():
messages.error(request, _('Failed to process form!'))
show_form_errors(request, form)
return redirect(obj)
matching = unit_set.filter_type(
form.cleaned_data['type'],
).exclude(
state=STATE_EMPTY
)
updated = 0
with transaction.atomic():
for unit in matching.select_for_update():
if not request.user.has_perm('unit.edit', unit):
continue
unit.translate(
request.user,
def auth_fail(request, message):
messages.error(request, message)
return redirect(reverse('login'))
def execute_locked(request, obj, message, call, *args, **kwargs):
"""Helper function to catch possible lock exception."""
try:
result = call(*args, **kwargs)
# With False the call is supposed to show errors on its own
if result is None or result:
messages.success(request, message)
except Timeout as error:
messages.error(
request,
_('Failed to lock the repository, another operation is in progress.')
)
report_error(error, request)
return redirect_param(obj, '#repository')
def change_project(request, project):
obj = get_project(request, project)
if not request.user.has_perm('project.edit', obj):
raise Http404()
if request.method == 'POST':
settings_form = ProjectSettingsForm(
request, request.POST, instance=obj
)
if settings_form.is_valid():
settings_form.save()
messages.success(request, _('Settings saved'))
return redirect('settings', project=obj.slug)
else:
messages.error(
request,
_('Invalid settings, please check the form for errors!')
)
else:
settings_form = ProjectSettingsForm(request, instance=obj)
return render(
request,
'project-settings.html',
{
'object': obj,
'settings_form': settings_form,
}