Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 739cfd9
Showing
528 changed files
with
42,053 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Database | ||
*.sqlite3 | ||
|
||
# MacOs | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[MASTER] | ||
|
||
[MESSAGES CONTROL] | ||
disable=import-error,missing-docstring,no-member,invalid-name,too-few-public-methods,no-name-in-module,no-self-use,too-many-ancestors,C0330,C0301,R0914,R0913,R0912,too-many-statements,too-many-lines,broad-except,too-many-return-statements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default_app_config = 'LocalUsers.apps.LocalUsersConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.contrib import admin | ||
from django.contrib.auth.admin import UserAdmin | ||
from django.contrib.auth.models import User | ||
from LocalUsers.models import Region, RegionMembership, Entity, SwordphishUser | ||
|
||
# Register your models here. | ||
|
||
|
||
class SwordphishUserAdmin(admin.StackedInline): | ||
model = SwordphishUser | ||
|
||
|
||
class MyUserAdmin(UserAdmin): | ||
inlines = (SwordphishUserAdmin, ) | ||
list_display = ('username', 'first_name', 'last_name', 'is_active', 'date_joined', 'is_staff') | ||
|
||
|
||
admin.site.unregister(User) | ||
admin.site.register(User, MyUserAdmin) | ||
admin.site.register(Region) | ||
admin.site.register(RegionMembership) | ||
admin.site.register(Entity) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from LocalUsers.models import Entity, Region | ||
|
||
|
||
def get_user_entities(user): | ||
entities = [] | ||
if user is not None: | ||
entities = Entity.objects.filter(admins__username=user.username) | ||
return entities | ||
|
||
|
||
def get_user_regions(user): | ||
regions = [] | ||
if user is not None: | ||
regions = Region.objects.filter(members__username=user.username) | ||
entities = get_user_entities(user) | ||
|
||
for entity in entities: | ||
subregions = Region.objects.filter(entity_name=entity.name) | ||
for region in subregions: | ||
if region not in regions: | ||
regions.add(region) | ||
|
||
return regions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.apps import AppConfig | ||
|
||
|
||
class LocalUsersConfig(AppConfig): | ||
name = "LocalUsers" | ||
|
||
def ready(self): | ||
from . import signals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
from django.forms import ModelForm, CharField, PasswordInput, ValidationError, ChoiceField | ||
from django.contrib.auth.models import User | ||
from django.utils.translation import ugettext as _ | ||
from LocalUsers.models import SwordphishUser, Entity, Region | ||
|
||
|
||
class SwordphishUserForm(ModelForm): | ||
class Meta: | ||
model = SwordphishUser | ||
fields = ['phone_number'] | ||
|
||
|
||
class UserForm(ModelForm): | ||
password = CharField(widget=PasswordInput(), required=False) | ||
password_confirmation = CharField(widget=PasswordInput(), required=False) | ||
|
||
class Meta: | ||
model = User | ||
fields = ['first_name', 'last_name', 'email'] | ||
|
||
def clean_first_name(self): | ||
data = self.cleaned_data['first_name'] | ||
if data == "": | ||
raise ValidationError(_("First name must be provided")) | ||
return data | ||
|
||
def clean_last_name(self): | ||
data = self.cleaned_data['last_name'] | ||
if data == "": | ||
raise ValidationError(_("First name must be provided")) | ||
return data | ||
|
||
def clean_password_confirmation(self): | ||
passw = self.cleaned_data['password'] | ||
conf = self.cleaned_data['password_confirmation'] | ||
|
||
if passw != conf: | ||
raise ValidationError(_("Password and confirmation are not equal")) | ||
return passw | ||
|
||
|
||
class CreateUserForm(ModelForm): | ||
class Meta: | ||
model = User | ||
fields = ['first_name', 'last_name', 'email'] | ||
|
||
def clean_first_name(self): | ||
data = self.cleaned_data['first_name'].capitalize() | ||
if data == "": | ||
raise ValidationError(_("First name must be provided")) | ||
return data | ||
|
||
def clean_last_name(self): | ||
data = self.cleaned_data['last_name'].upper() | ||
if data == "": | ||
raise ValidationError(_("First name must be provided")) | ||
return data | ||
|
||
def clean_email(self): | ||
data = self.cleaned_data['email'].lower() | ||
if data == "": | ||
raise ValidationError(_("Email must be provided")) | ||
return data | ||
|
||
|
||
class LostpasswordForm(ModelForm): | ||
class Meta: | ||
model = User | ||
fields = ['email'] | ||
|
||
def clean_email(self): | ||
data = self.cleaned_data['email'].lower() | ||
if data == "": | ||
raise ValidationError(_("Email must be provided")) | ||
return data | ||
|
||
|
||
class ChangePasswordForm(UserForm): | ||
def __init__(self, *args, **kwargs): | ||
super(ChangePasswordForm, self).__init__(*args, **kwargs) | ||
self.fields["password"].widget.attrs['required'] = True | ||
self.fields["password_confirmation"].widget.attrs['required'] = True | ||
self.fields["email"].widget.attrs['readonly'] = 'readonly' | ||
|
||
|
||
class EditMyProfileForm(UserForm): | ||
old_password = CharField(widget=PasswordInput(), required=True) | ||
|
||
class Meta: | ||
model = User | ||
fields = [ | ||
'first_name', | ||
'last_name', | ||
'email', | ||
'old_password', | ||
'password', | ||
'password_confirmation' | ||
] | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(EditMyProfileForm, self).__init__(*args, **kwargs) | ||
self.fields["email"].widget.attrs['readonly'] = 'readonly' | ||
|
||
def clean_old_password(self): | ||
if self.instance.check_password(self.cleaned_data["old_password"]): | ||
return self.cleaned_data['old_password'] | ||
raise ValidationError(_("The current password is not correct")) | ||
|
||
|
||
class EntityForm(ModelForm): | ||
class Meta: | ||
model = Entity | ||
fields = ['name'] | ||
|
||
|
||
class AddAdminForm(ModelForm): | ||
users = ChoiceField() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(AddAdminForm, self).__init__(*args, **kwargs) | ||
self.fields["name"].widget.attrs['readonly'] = 'readonly' | ||
self.fields["users"].choices = self.get_admin_list() | ||
|
||
def get_admin_list(self): | ||
result = [] | ||
swordphishusers = SwordphishUser.objects.all() | ||
instanceadmins = self.instance.admins.all() | ||
for user in swordphishusers: | ||
if user not in instanceadmins: | ||
result.append((user.id, "%s %s (%s)" % (user.user.last_name, | ||
user.user.first_name, | ||
user.user.email) | ||
)) | ||
return result | ||
|
||
def clean_users(self): | ||
user = self.cleaned_data['users'] | ||
if user == "": | ||
raise ValidationError(_("User must be provided")) | ||
|
||
testuser = SwordphishUser.objects.get(pk=user) | ||
|
||
if testuser is None: | ||
raise ValidationError(_("This user doesn't exist")) | ||
|
||
instanceadmins = self.instance.admins.all() | ||
for users in instanceadmins: | ||
if users.id == user: | ||
raise ValidationError(_("User is already admin for this entity")) | ||
|
||
return user | ||
|
||
class Meta: | ||
model = Entity | ||
fields = ["name"] | ||
|
||
|
||
class RegionForm(ModelForm): | ||
entity = ChoiceField() | ||
|
||
def __init__(self, *args, **kwargs): | ||
current_user = kwargs.pop('current_user', None) | ||
super(RegionForm, self).__init__(*args, **kwargs) | ||
self.fields["entity"].choices = self.get_entities_list(current_user) | ||
|
||
def get_entities_list(self, current_user): | ||
result = [] | ||
if current_user.is_staff: | ||
entities = Entity.objects.all() | ||
else: | ||
entities = current_user.swordphishuser.entities() | ||
for entity in entities: | ||
result.append((entity.id, "%s" % (entity.name))) | ||
return result | ||
|
||
def clean_entity(self): | ||
entity = self.cleaned_data['entity'] | ||
if entity == "": | ||
raise ValidationError(_("Entity must be provided")) | ||
|
||
testentity = Entity.objects.get(pk=entity) | ||
|
||
if testentity is None: | ||
raise ValidationError(_("This entity doesn't exist")) | ||
|
||
return entity | ||
|
||
class Meta: | ||
model = Region | ||
fields = ['name'] | ||
|
||
|
||
class AddUserInRegionForm(ModelForm): | ||
users = ChoiceField() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(AddUserInRegionForm, self).__init__(*args, **kwargs) | ||
self.fields["name"].widget.attrs['readonly'] = 'readonly' | ||
self.fields["users"].choices = self.get_users_list() | ||
|
||
def get_users_list(self): | ||
result = [] | ||
swordphishusers = SwordphishUser.objects.all() | ||
for user in swordphishusers: | ||
result.append((user.id, "%s %s (%s)" % (user.user.last_name, | ||
user.user.first_name, | ||
user.user.email) | ||
)) | ||
return result | ||
|
||
def clean_users(self): | ||
user = self.cleaned_data['users'] | ||
if user == "": | ||
raise ValidationError(_("User must be provided")) | ||
|
||
testuser = SwordphishUser.objects.get(pk=user) | ||
|
||
if testuser is None: | ||
raise ValidationError(_("This user doesn't exist")) | ||
|
||
instancemembers = self.instance.members.all() | ||
for users in instancemembers: | ||
if users.id == user: | ||
raise ValidationError(_("User is already in this region")) | ||
|
||
instanceadmins = self.instance.entity.admins.all() | ||
for users in instanceadmins: | ||
if users.id == user: | ||
raise ValidationError(_("User is admin of the related entity")) | ||
|
||
return user | ||
|
||
class Meta: | ||
model = Region | ||
fields = ["name"] |
Oops, something went wrong.