Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Store activity log and notify
AuditLog.objects.create(user, request, 'removed')
# Remove any email validation codes
invalidate_reset_codes(user)
# Change username
user.username = 'deleted-{0}'.format(user.pk)
user.email = 'noreply+{}@weblate.org'.format(user.pk)
while User.objects.filter(username=user.username).exists():
user.username = 'deleted-{0}-{1}'.format(
user.pk,
binascii.b2a_hex(os.urandom(5))
)
while User.objects.filter(email=user.email).exists():
user.email = 'noreply+{0}-{1}@weblate.org'.format(
user.pk,
binascii.b2a_hex(os.urandom(5))
)
# Remove user information
user.full_name = 'Deleted User'
# Disable the user
user.is_active = False
user.set_unusable_password()
user.save()
# Remove all social auth associations
user.social_auth.all().delete()
def handle(self, *args, **options):
"""Create admin account with admin password.
This is useful mostly for setup inside appliances, when user wants
to be able to login remotely and change password then.
"""
try:
user = User.objects.filter(
Q(username=options['username']) | Q(email=options['email'])
).get()
except User.DoesNotExist:
user = None
except User.MultipleObjectsReturned:
raise CommandError('Multiple users matched given parameters!')
if user and not options['update']:
raise CommandError('User exists, specify --update to update existing')
if options['no_password']:
password = None
elif options['password']:
password = options['password']
else:
password = make_password(13)
self.stdout.write('Using generated password: {}'.format(password))
if user and options['update']:
self.stdout.write('Updating user {}'.format(user.username))
user.email = options['email']
def try_get_user(username, list_all=False):
"""Wrapper to get User object for authentication."""
if list_all:
method = User.objects.filter
else:
method = User.objects.get
if '@' in username:
return method(email=username)
return method(username=username)
@receiver(post_save, sender=User)
@disable_for_loaddata
def create_profile_callback(sender, instance, created=False, **kwargs):
"""Automatically create token and profile for user."""
if created:
# Create API token
Token.objects.create(user=instance, key=get_random_string(40))
# Create profile
Profile.objects.create(user=instance)
# Create subscriptions
if not instance.is_anonymous:
create_default_notifications(instance)
def authenticate(self, request, username=None, password=None, **kwargs):
"""Prohibit login for anonymous user and allows to login by e-mail."""
if username == settings.ANONYMOUS_USER_NAME or username is None:
return None
try:
user = try_get_user(username)
if user.check_password(password):
return user
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
return None
@python_2_unicode_compatible
class Billing(models.Model):
STATE_ACTIVE = 0
STATE_TRIAL = 1
STATE_EXPIRED = 2
STATE_TERMINATED = 3
EXPIRING_STATES = (STATE_TRIAL,)
plan = models.ForeignKey(
Plan, on_delete=models.deletion.CASCADE, verbose_name=_('Billing plan')
)
projects = models.ManyToManyField(
Project, blank=True, verbose_name=_('Billed projects')
)
owners = models.ManyToManyField(User, blank=True, verbose_name=_('Billing owners'))
state = models.IntegerField(
choices=(
(STATE_ACTIVE, _('Active')),
(STATE_TRIAL, _('Trial')),
(STATE_EXPIRED, _('Expired')),
(STATE_TERMINATED, _('Terminated')),
),
default=STATE_ACTIVE,
verbose_name=_('Billing state'),
)
expiry = models.DateTimeField(
blank=True,
null=True,
default=None,
verbose_name=_('Trial expiry date'),
help_text='After expiry removal with 15 days grace period is scheduled.',
def delete_user(request, project):
"""Remove user from a project."""
obj, form = check_user_form(request, project, True)
if form is not None:
owners = User.objects.all_admins(obj)
user = form.cleaned_data['user']
is_owner = owners.filter(pk=user.pk).exists()
if is_owner and owners.count() <= 1:
messages.error(request, _('You can not remove last owner!'))
else:
obj.remove_user(user)
Change.objects.create(
project=obj,
action=Change.ACTION_REMOVE_USER,
user=request.user,
details={'username': user.username},
)
messages.success(
request, _('User has been removed from this project.')
)
from weblate.accounts.avatar import get_user_display
from weblate.accounts.data import create_default_notifications
from weblate.accounts.notifications import FREQ_CHOICES, NOTIFICATIONS, SCOPE_CHOICES
from weblate.accounts.tasks import notify_auditlog
from weblate.auth.models import User
from weblate.lang.models import Language
from weblate.utils import messages
from weblate.utils.decorators import disable_for_loaddata
from weblate.utils.fields import JSONField
from weblate.utils.render import validate_editor
from weblate.utils.request import get_ip_address, get_user_agent
@python_2_unicode_compatible
class Subscription(models.Model):
user = models.ForeignKey(User, on_delete=models.deletion.CASCADE)
notification = models.CharField(
choices=[n.get_choice() for n in NOTIFICATIONS], max_length=100
)
scope = models.IntegerField(choices=SCOPE_CHOICES)
frequency = models.IntegerField(choices=FREQ_CHOICES)
project = models.ForeignKey(
'trans.Project', on_delete=models.deletion.CASCADE, null=True
)
component = models.ForeignKey(
'trans.Component', on_delete=models.deletion.CASCADE, null=True
)
class Meta(object):
unique_together = [('notification', 'scope', 'project', 'component', 'user')]
def __str__(self):