Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.user = User.objects.create_user(
username='bill',
password='kill',
email='noreply@example.net'
)
self.plan = Plan.objects.create(
name='test', limit_projects=1, price=1.0
)
self.billing = Billing.objects.create(plan=self.plan)
self.billing.owners.add(self.user)
self.invoice = Invoice.objects.create(
billing=self.billing,
start=timezone.now().date() - timedelta(days=2),
end=timezone.now().date() + timedelta(days=2),
amount=10,
ref='00000',
)
def audit(self, request):
orig = User.objects.get(pk=self.instance.pk)
for attr in ('username', 'full_name', 'email'):
orig_attr = getattr(orig, attr)
new_attr = getattr(self.instance, attr)
if orig_attr != new_attr:
AuditLog.objects.create(
orig,
request,
attr,
old=orig_attr,
new=new_attr,
)
def get_last_author(self, email=False):
"""Return last autor of change done in Weblate."""
if not self.stats.last_author:
return None
from weblate.auth.models import User
return User.objects.get(pk=self.stats.last_author).get_author_name(email)
request,
fileobj,
overwrite,
author_name=None,
author_email=None,
method='translate',
fuzzy='',
):
"""Top level handler for file uploads."""
# Optionally set authorship
orig_user = None
if author_email:
from weblate.auth.models import User
orig_user = request.user
request.user = User.objects.get_or_create(
email=author_email,
defaults={
'username': author_email,
'is_active': False,
'full_name': author_name or author_email,
},
)[0]
if method == 'replace':
return self.handle_replace(request, fileobj)
filecopy = fileobj.read()
fileobj.close()
# Strip possible UTF-8 BOM
if filecopy[:3] == codecs.BOM_UTF8:
def is_admin(self, user, project):
if project is None:
return False
if project.pk not in self.perm_cache:
self.perm_cache[project.pk] = User.objects.all_admins(project).values_list(
'pk', flat=True
)
return user.pk in self.perm_cache[project.pk]
internal=True,
pk=int(request.POST.get('group', '')),
)
except (Group.DoesNotExist, ValueError):
group = None
action = request.POST.get('action')
user = form.cleaned_data['user'] if form else None
if group is None or form is None:
code = 400
message = _('Invalid parameters!')
status = None
elif action == 'remove':
owners = User.objects.all_admins(obj)
if group.name.endswith('@Administration') and owners.count() <= 1:
code = 400
message = _('You can not remove last owner!')
else:
code = 200
message = ''
user.groups.remove(group)
Change.objects.create(
project=obj,
action=Change.ACTION_REMOVE_USER,
user=request.user,
details={'username': user.username, 'group': group.name},
)
status = user.groups.filter(pk=group.pk).exists()
else:
user.groups.add(group)
def clean(self, value):
if not value:
return None
try:
return User.objects.get(Q(username=value) | Q(email=value))
except User.DoesNotExist:
raise ValidationError(_('No matching user found.'))
except User.MultipleObjectsReturned:
raise ValidationError(_('More users matched.'))
class ProjectRenameForm(SettingsBaseForm):
"""Project rename form."""
class Meta(object):
model = Project
fields = ['slug']
class ProjectCreateForm(SettingsBaseForm):
"""Project creation form."""
# This is fake field with is either hidden or configured
# in the view
billing = forms.ModelChoiceField(
label=_('Billing'),
queryset=User.objects.none(),
required=True,
empty_label=None,
)
class Meta(object):
model = Project
fields = ('name', 'slug', 'web', 'mail', 'instructions')
class ProjectAccessForm(forms.ModelForm):
"""Project access control settings form."""
class Meta(object):
model = Project
fields = (
'access_control',
'enable_review',
class ProjectRenameForm(SettingsBaseForm):
"""Project rename form."""
class Meta(object):
model = Project
fields = ['slug']
class ProjectCreateForm(SettingsBaseForm):
"""Project creation form."""
# This is fake field with is either hidden or configured
# in the view
billing = forms.ModelChoiceField(
label=_('Billing'),
queryset=User.objects.none(),
required=True,
empty_label=None,
)
class Meta(object):
model = Project
fields = ('name', 'slug', 'web', 'mail', 'instructions')
class ProjectAccessForm(forms.ModelForm):
"""Project access control settings form."""
class Meta(object):
model = Project
fields = ('access_control', 'enable_review')
def _get_queryset_user(self):
"""Filtering by user"""
if 'user' in self.request.GET:
try:
self.user = User.objects.get(username=self.request.GET['user'])
except User.DoesNotExist:
messages.error(self.request, _('Failed to find matching user!'))