Commit 34623c47 authored by David Haynes's avatar David Haynes 🙆

Merge branch '173-api-create-links' into 'go-three'

Resolve "API endpoint for creating links"

See merge request !123
parents 02560ef1 84f7d732
Pipeline #2638 passed with stage
in 51 seconds
......@@ -36,4 +36,3 @@ services:
- GO_EMAIL_HOST_PASSWORD=
- GO_EMAIL_FROM=
- GO_EMAIL_TO=
- SUPERUSER=dhaynes3
......@@ -8,11 +8,9 @@ export GO_SECRET_KEY
export GO_CREATE_SUPERUSER
GO_SECRET_KEY=$(dd if=/dev/urandom count=100 | tr -dc "A-Za-z0-9" | fold -w 60 | head -n1 2>/dev/null)
GO_CREATE_SUPERUSER="from django.contrib.auth import get_user_model; User = get_user_model(); me = User.objects.get(username='$SUPERUSER'); me.first_name = 'David'; me.last_name = 'Haynes'; me.save(); "
python go/manage.py makemigrations
python go/manage.py makemigrations go
python go/manage.py migrate
python go/manage.py createsuperuser --noinput --username="$SUPERUSER" --email="$SUPERUSER$GO_EMAIL_DOMAIN"
echo "$GO_CREATE_SUPERUSER" | python go/manage.py shell
python go/manage.py runserver 0.0.0.0:8000
\ No newline at end of file
......@@ -5,6 +5,8 @@ Parse the CAS/PF responses and create users in the database.
"""
# Other Imports
import requests
import os
# Django Imports
from django.conf import settings
from django.contrib.auth.models import User
......@@ -77,36 +79,32 @@ def create_user(tree: list):
Create a django user based off of the peoplefinder info we parsed earlier.
"""
print("Parsing CAS information.")
try:
username = tree[0][0].text
user, user_created = User.objects.get_or_create(username=username)
except Exception as ex:
print("CAS callback unsuccessful:", ex)
# error handling in pfinfo function
info_name = pfinfo(username)
# set and save the user's email
email_str = "%s%s" % (username, settings.EMAIL_DOMAIN)
# error handling in pfinfo function
info_name = pfinfo(username)
user, user_created = User.objects.get_or_create(
username=username,
email=email_str,
first_name=info_name[0],
last_name=info_name[1]
)
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user.set_password('cas_used_instead')
if os.environ['GO_ENV'] != 'production':
user.is_staff = True
user.is_superuser = True
user.save()
try:
if user_created:
print("Created user object %s." % username)
# set and save the user's email
email_str = "%s%s" % (username, settings.EMAIL_DOMAIN)
user.email = email_str
# Password is a required User object field, though doesn't matter for our
# purposes because all user auth is handled through CAS, not Django's login.
user.set_password('cas_used_instead')
user.save()
print("Added user's email, %s." % email_str)
user.first_name = info_name[0]
user.last_name = info_name[1]
user.save()
print("Added user's name, %s %s." % (info_name[0], info_name[1]))
print("User object creation process completed.")
else:
print("User object already exists.")
print("CAS callback successful.")
except Exception as ex:
print("Unhandled user creation error:", ex)
print("CAS callback unsuccessful:", ex)
......@@ -90,7 +90,7 @@ def handle_reguser_creation(sender, instance, created, **kwargs):
is called to create an associated RegisteredUser.
"""
if created:
RegisteredUser.objects.create(user=instance)
RegisteredUser.objects.create(user=instance, full_name=instance.get_full_name())
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
......
......@@ -3,16 +3,13 @@ go/serializers.py
Define how data is translated from the database to json/API representation.
"""
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
# App Imports
from .models import URL, RegisteredUser
# Third Party Imports
from rest_framework import serializers
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class URLSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
model = URL
fields = ('destination', 'short', 'date_expires')
This diff is collapsed.
......@@ -11,23 +11,31 @@ from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
# App Imports
from go import views
from go import views
from cas import views as cas_views
# Third Party
from rest_framework import routers
router = routers.DefaultRouter()
router.register('users', views.UserViewSet)
router.register('groups', views.GroupViewSet)
router.register(r'golinks', views.URLViewSet, base_name="golinks")
# This function attempts to import an admin module in each installed
# application. Such modules are expected to register models with the admin.
admin.autodiscover()
urlpatterns = [
# # / - Homepage url. Cached for 1 second (this is the page you see after
# # logging in, so having it show as not logged in is strange)
# path('', cache_page(1)(go.views.index), name='index'),
# Root API URL
path("", include(router.urls)),
# Authentication URLs
path('auth/login/', cas_views.login, name='cas_login'),
path('auth/logout/', cas_views.logout, {'next_page': '/'}, name='cas_logout'),
# /admin - Administrator interface.
path('admin/', admin.site.urls, name='go_admin'),
path('auth/', include('rest_framework.urls'))
# # /view/<short> - View URL data. Cached for 15 minutes
# re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
......@@ -35,44 +43,9 @@ urlpatterns = [
# re_path(r'^view/(?P<short>[-\w]+)$',
# cache_page(60 * 15)(go.views.view), name='view'),
# # /about - About page. Cached for 15 minutes
# path('about', cache_page(60 * 15)
# (TemplateView.as_view(template_name='core/about.html')), name='about'),
# # /signup - Signup page for access. Cached for 15 minutes
# path('signup', cache_page(60 * 15)(go.views.signup), name='signup'),
# # /new - Create a new Go Link
# path('new', go.views.new_link, name='new_link'),
# # /my - My-Links page, view and review links.
# path('my', go.views.my_links, name='my_links'),
# # /edit/<short> - Edit link form
# path('edit/<slug:short>', go.views.edit, name='edit'),
# # /delete/<short> - Delete a link, no content display.
# path('delete/<slug:short>', go.views.delete, name='delete'),
# # /registered - registration complete page. Cached for 15 minutes
# path('registered', cache_page(60 * 15)
# (TemplateView.as_view(template_name='registered.html')), name='registered'),
# # /admin - Administrator interface.
# path('admin', admin.site.urls, name='go_admin'),
# # /manage - user approval interface
# path('manage', go.views.useradmin, name='useradmin'),
# Authentication URLs
path('login', django.contrib.auth.views.login, name='go_login'),
path('logout', django.contrib.auth.views.logout, {'next_page': '/'}, name='go_logout'),
# # Redirection regex.
# re_path(r'^(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# go.views.redirection, name='redirection'),
# re_path(r'^(?P<short>[-\w]+)$',
# go.views.redirection, name='redirection'),
path("", include(router.urls)),
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment