Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View etianen's full-sized avatar

Dave Hall etianen

View GitHub Profile
from django_python3_ldap.ldap import connection
with connection() as c:
print(c.get_user(username="foo"))
from django_python3_ldap import ldap
from django.contrib.auth.decorators import login_required
@login_required
def ldap_seach(request):
# Because of login_required, this view is protected by the Django login screen.
# request.user will be the logged-in user.
if request.method == "POST":
# The user has submitted the search form of the user to look up.
# your_project/your_app/ldap.py
import django_python3_ldap.utils.clean_user_data
def clean_user_data(model_fields):
"""
Transforms the user data loaded from
LDAP into a form suitable for creating a user.
"""
# Call the default handler.
ldap3.Connection(
ldap3.Server(
"ldap://msldap.uct.ac.za",
allowed_referral_hosts=[("*", True)],
),
user="01421894@wf.uct.ac.za",
password="password",
auto_bind=ldap3.AUTO_BIND_NO_TLS,
raise_exceptions=True,
)
class YourSearchAdapter(watson.SearchAdapter):
def get_title(self, obj):
return super().get_title(obj).lower()
def get_description(self, obj):
return super().get_description(obj).lower()
def get_content(self, obj):
return super().get_content(obj).lower()
from django.apps import AppConfig
from watson import search as watson
search_engine_1 = watson.SearchEngine("engine_1")
search_engine_2 = watson.SearchEngine("engine_2")
class YourAppConfig(AppConfig):
name = "customer"
def ready(self):
search_engine_1.register(self.get_model("ModelA"))
from django.apps import AppConfig
from watson import search as watson
search_engine_1 = watson.SearchEngine("engine_1")
search_engine_2 = watson.SearchEngine("engine_2")
class YourAppConfig(AppConfig):
name = "customer"
def ready(self):
search_engine_1.register(self.get_model("ModelA"))
""" ldap_auth.py (custom methods for django_python3_ldap)
Create three additional groups in LDAP directory (e.g w/ prefix "django_"):
* cn=django_my_django_project,ou=groups,dc=example.com,dc=com
* cn=django_my_django_project_staff,ou=groups,dc=example.com,dc=com
* cn=django_my_django_project_superuser,ou=groups,dc=example.com,dc=com
1) "django_my_django_project"
Add all users to this group that should be able to use the Django APP.
The users will then be able to authenticate and their attributes will be synched.
@etianen
etianen / bootstrap.js
Created September 2, 2014 09:56
RequireJS setup for multi-page sites, using a single monolithic JS file.
/**
* Runs a per-page init function.
*
* The init function is chosen via the `page` config setting.
*
* This has to be defined in a separate module, since RequireJS
* does not allow passing config to the main module.
*/
define(["module"], function(module) {
class Node(models.Model):
parent = models.ForeignKey("self", blank=True, null=True)
reversion.register(Node)
with reversion.create_revision():
p = Node.objects.create() # Version saved.
with reversion.create_revision():
c = Node.objects.create() # Version saved.