Skip to content

Commit

Permalink
what
Browse files Browse the repository at this point in the history
  • Loading branch information
benzkji committed Oct 18, 2017
1 parent 38134b6 commit 9c7d8c4
Show file tree
Hide file tree
Showing 22 changed files with 633 additions and 393 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ __pycache__/
# C extensions
*.so

# sqlite db
*.db
*.sqlite

# Distribution / packaging
.Python
env/
Expand Down
13 changes: 12 additions & 1 deletion .idea/django-separate-users.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

601 changes: 447 additions & 154 deletions .idea/workspace.xml

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

==== 0.xxx (not yet) ===
==== ongoing ===

- fix for djangocms-admin-styles
- ...


==== 0.0.1 (not yet) ===
==== 0.0.1 ===

- initial release

- support for django 1.9
- refactored tests, to use default manage.py test command

2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

if __name__ == "__main__":
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'separate_users.tests.settings_runtestapp')
'separate_users.tests.settings')

from django.core.management import execute_from_command_line

Expand Down
80 changes: 25 additions & 55 deletions separate_users/admin.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,31 @@
from django.conf import settings
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.db import models
from django.http import JsonResponse

from separate_users.models import FrontendUser, Editor

class DjangoLinkAdmin(admin.ModelAdmin):

def get_model_perms(self, request):
"""
http://stackoverflow.com/questions/2431727/django-admin-hide-a-model
Return empty perms dict thus hiding the model from admin index.
"""
return {}

def get_urls(self):
"""
add verify url.
"""
my_urls = [
url(
r'^verify/$',
self.admin_site.admin_view(self.verify),
name=self._get_verify_url_name()
),
]
return my_urls + super(DjangoLinkAdmin, self).get_urls()

def _get_verify_url_name(self):
return '{0}_{1}_verify'.format(self.model._meta.app_label,
self.model._meta.model_name)

def verify(self, request):
"""
verify data with modelform, send through data.
:param request:
:return:
"""
form = self.get_form(request, )(request.POST)
if form.is_valid():
verified_data = form.cleaned_data
obj = self.model(**verified_data)
link_value = ''
# prepopulate href
if (getattr(obj, 'get_link', None)):
link_value = obj.get_link()
# basic serialize only
for key, value in verified_data.items():
if isinstance(value, models.Model):
verified_data[key] = value.id
return_data = {"valid": 'true', 'data': verified_data, 'link_value': link_value}
else:
errors = form.errors
return_data = {"valid": 'false', 'errors': errors}
return JsonResponse(return_data)

def save_model(self, request, obj, form, change):
"""
no save!
"""
return False

# admin.site.unregister(get_user_model())


class FrontendUserAdmin(UserAdmin):
readonly_fields = ['date_joined', 'last_login', 'is_staff', 'is_superuser', 'groups', 'user_permissions', ]
list_filter = ['is_active', 'groups', ]
list_display = ['username', 'is_active', ]


admin.site.register(FrontendUser, FrontendUserAdmin)


class EditorAdmin(UserAdmin):
exclude = []
readonly_fields = ['date_joined', 'last_login', 'is_staff', 'is_superuser', 'user_permissions', ]
list_filter = ['is_active', 'groups', ]
list_display = ['username', 'is_active', ]


admin.site.register(Editor, EditorAdmin)
7 changes: 7 additions & 0 deletions separate_users/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _


class SeparateUsersConfig(AppConfig):
name = 'separate_users'
verbose_name = _("Users")
1 change: 1 addition & 0 deletions separate_users/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'benzkji'
Empty file.
28 changes: 28 additions & 0 deletions separate_users/management/commands/separate_users_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys

from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.apps import apps


class Command(BaseCommand):
help = "Fix permissions for proxy models."

def handle(self, *args, **options):
for model in apps.get_models():
opts = model._meta
sys.stdout.write('{}-{}\n'.format(opts.app_label, opts.object_name.lower()))
ctype, created = ContentType.objects.get_or_create(
app_label=opts.app_label,
model=opts.object_name.lower())

for codename, name in _get_all_permissions(opts):
sys.stdout.write(' --{}\n'.format(codename))
p, created = Permission.objects.get_or_create(
codename=codename,
content_type=ctype,
defaults={'name': name})
if created:
sys.stdout.write('Adding permission {}\n'.format(p))
47 changes: 47 additions & 0 deletions separate_users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.conf import settings
from django.contrib.auth.models import UserManager
from django.db import models
from django.contrib.auth import get_user_model


UserModel = get_user_model()


class FrontendUserManager(UserManager):

def get_queryset(self):
return super(FrontendUserManager, self)\
.get_queryset()\
.filter(is_staff=False, is_superuser=False)


class EditorManager(UserManager):

def get_queryset(self):
return super(EditorManager, self)\
.get_queryset()\
.filter(is_staff=True, is_superuser=False)


class FrontendUser(UserModel):

objects = FrontendUserManager()

def save(self, *args, **kwargs):
self.is_staff = False
super(FrontendUser, self).save(*args, **kwargs)

class Meta:
proxy = True


class Editor(UserModel):

objects = EditorManager()

def save(self, *args, **kwargs):
self.is_staff = True
super(Editor, self).save(*args, **kwargs)

class Meta:
proxy = True
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,7 @@

DEBUG = True

logging.getLogger("factory").setLevel(logging.WARN)

# from selenium.webdriver.firefox import webdriver
from selenium.webdriver.phantomjs import webdriver
SELENIUM_WEBDRIVER = webdriver


separate_users_MODEL = 'separate_users.tests.test_app.models.LinkModel'
separate_users_IFRAME_URL = reverse_lazy('admin:test_app_linkmodel_add')
separate_users_VERIFY_URL = reverse_lazy('admin:test_app_linkmodel_verify')

CKEDITOR_CONFIGS = {
'default': {
'djangolinkIframeURL': separate_users_IFRAME_URL,
'djangolinkVerifyURL': separate_users_VERIFY_URL,
'linkShowAdvancedTab': False,
'extraPlugins': ','.join(
[
# your extra plugins here
'djangolink',
'autolink',
'autoembed',
'embedsemantic',
'autogrow',
'devtools',
'widget',
'lineutils',
'clipboard',
'dialog',
'dialogui',
'elementspath'
]),
'toolbar': 'Custom',
'toolbar_Custom': [
['Bold', 'Underline'],
['DjangoLink', 'Unlink'],
['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
['Link', 'Unlink'],
['RemoveFormat', 'Source']
]
}
}
# logging.getLogger("factory").setLevel(logging.WARN)

SITE_ID = 1

Expand All @@ -60,10 +19,14 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
'NAME': 'test.db',
}
}

MIGRATION_MODULES = {
'separate_users': 'separate_users.tests.test_app.migrations',
}

LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', 'ENGLISHS' ),
Expand Down Expand Up @@ -93,19 +56,14 @@

EXTERNAL_APPS = (
'django.contrib.admin',
# 'django.contrib.admindocs',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.sites',
'ckeditor',
# 'djangocms_text_ckeditor',
# 'cms',
# 'treebeard',
# 'menus',
'django_extensions',
)

TEMPLATES = [
Expand Down
13 changes: 0 additions & 13 deletions separate_users/tests/settings_runtestapp.py

This file was deleted.

18 changes: 0 additions & 18 deletions separate_users/tests/test_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
from django import forms
from django.contrib import admin
from django.forms.extras.widgets import SelectDateWidget

from separate_users.admin import DjangoLinkAdmin

from .models import TestModel, LinkModel


class LinkModelForm(forms.ModelForm):
when = forms.DateField(widget=SelectDateWidget, required=False)


class CKLinkModelAdmin(DjangoLinkAdmin):
form = LinkModelForm

admin.site.register(TestModel)
admin.site.register(LinkModel, CKLinkModelAdmin)
1 change: 0 additions & 1 deletion separate_users/tests/test_app/fixtures/test_app.json

This file was deleted.

Loading

0 comments on commit 9c7d8c4

Please sign in to comment.