Commit d87e9ad0 authored by David Haynes's avatar David Haynes 🙆

Merge branch 'go-three' into 178-about-page-3.0-v2

parents bd1ea1ef 36bba023
......@@ -7,6 +7,7 @@ name = "pypi"
pylint = "*"
pylint-django = "*"
coverage = "*"
black = "*"
[packages]
django = "<2.1,>=2.0"
......
This diff is collapsed.
import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter, Route, Link } from 'react-router-dom';
import React from "react";
import ReactDOM from "react-dom";
import { HashRouter, Route, Link } from "react-router-dom";
import { Routes } from 'Utils';
import { Routes } from "Utils";
// Apply Global Masonstrap styling
import 'masonstrap/build/css/masonstrap.min.css';
import 'masonstrap/build/js/masonstrap.min.js';
import "masonstrap/build/css/masonstrap.min.css";
import "masonstrap/build/js/masonstrap.min.js";
ReactDOM.render(
<HashRouter>
<Routes />
</HashRouter>,
document.getElementById('root')
<HashRouter>
<Routes />
</HashRouter>,
document.getElementById("root")
);
import React from 'react';
import { Button } from 'reactstrap';
import React from "react";
import { Button } from "reactstrap";
export default class AuthButton extends React.Component {
constructor(props) {
super(props);
this.state = { is_auth: false };
}
class AuthButton extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, is_auth: false };
}
componentDidMount() {
this.setState(() => {
return { is_auth: window.django.user.is_authenticated == 'True' };
});
}
componentDidMount() {
fetch("/auth/status")
.then(res => res.json())
.then(
result => {
this.setState({
is_auth: result.is_authenticated
});
},
error => {
this.setState({
error
});
}
);
}
render() {
return (
<div>
{this.state.is_auth ? (
<Button color="info" href="/auth/logout">
Logout
</Button>
) : (
<Button color="info" href="/auth/login">
Login
</Button>
)}
</div>
);
render() {
const { is_auth, error } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else {
return (
<div>
{is_auth ? (
<Button color="info" href="/auth/logout">
Logout
</Button>
) : (
<Button color="info" href="/auth/login">
Login
</Button>
)}
</div>
);
}
}
}
export default AuthButton;
import AuthButton from './AuthButton';
import AuthButton from "./AuthButton";
export { AuthButton };
import React from 'react';
import { Navbar, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';
import React from "react";
import { Navbar, NavbarBrand, Nav, NavItem, NavLink } from "reactstrap";
export default class NavBar extends React.Component {
render() {
return (
<div>
<Navbar color="dark" dark expand="md">
<NavbarBrand href="#">Go</NavbarBrand>
render() {
return (
<div>
<Navbar color="dark" dark expand="md">
<NavbarBrand href="#">Go</NavbarBrand>
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink href="#/dhaynes" active={this.props.page == 'dhaynes'}>
Dhaynes
</NavLink>
</NavItem>
<NavItem>
<NavLink href="#/about" active={this.props.page == 'about'}>
About
</NavLink>
</NavItem>
</Nav>
</Navbar>
</div>
);
}
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink href="#/dhaynes" active={this.props.page == "dhaynes"}>
Dhaynes
</NavLink>
</NavItem>
<NavItem>
<NavLink href="#/about" active={this.props.page == "about"}>
About
</NavLink>
</NavItem>
</Nav>
</Navbar>
</div>
);
}
}
import NavBar from './NavBar';
import NavBar from "./NavBar";
export { NavBar };
import { PageTemplate } from './Templates';
import { HomePage, AboutPage, DhaynesPage } from './Pages';
import { NavBar } from './Organisms';
import { AuthButton } from './Molecules';
import { PageTemplate } from "./Templates";
import { HomePage, AboutPage, DhaynesPage } from "./Pages";
import { NavBar } from "./Organisms";
import { AuthButton } from "./Molecules";
export {
//Tempaltes
PageTemplate,
//Pages
HomePage,
AboutPage,
DhaynesPage,
//Organisms
NavBar,
//Molecules
AuthButton,
//Tempaltes
PageTemplate,
//Pages
HomePage,
AboutPage,
DhaynesPage,
//Organisms
NavBar,
//Molecules
AuthButton
};
import React from 'react';
import { Route } from 'react-router-dom';
import React from "react";
import { Route } from "react-router-dom";
import { HomePage, AboutPage, DhaynesPage } from 'Components';
import { HomePage, AboutPage, DhaynesPage } from "Components";
const Routes = () => (
<div>
<Route path="/" exact component={HomePage} />
<Route path="/dhaynes" component={DhaynesPage} />
<Route path="/about" component={AboutPage} />
</div>
<div>
<Route path="/" exact component={HomePage} />
<Route path="/dhaynes" component={DhaynesPage} />
<Route path="/about" component={AboutPage} />
</div>
);
export default Routes;
{% load static %}
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#006633" />
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#006633" />
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<body>
<!-- React injects itself here -->
<div id="root"></div>
</body>
<body>
<!-- React injects itself here -->
<div id="root"></div>
</body>
<script>
window.django = {
logout: "{% url "cas_logout" %}",
user: {
username: "{{ request.user.username }}",
full_name: "{{ request.user.get_full_name }}",
last_login: "{{ request.user.last_login }}",
is_authenticated: "{{ request.user.is_authenticated }}"
}
};
</script>
<script src="static/main.js"></script>
</html>
\ No newline at end of file
<script src="static/main.js"></script>
</html>
......@@ -4,17 +4,15 @@ settings/urls.py
The URLs of the project and their associated view that requests are routed to.
"""
# Django Imports
from django.urls import path, re_path, include
from django.urls import path, include
from django.contrib import admin
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
# App Imports
from . import views
from cas import views as cas_views
# Third Party
from rest_framework import routers
from cas import views as cas_views
# App Imports
from . import views
router = routers.DefaultRouter()
router.register(r"golinks", views.URLViewSet, base_name="golinks")
......@@ -32,7 +30,8 @@ urlpatterns = [
# /admin - Administrator interface.
path("admin/", admin.site.urls, name="go_admin"),
path("auth/", include("rest_framework.urls")),
path("auth/token/", views.CustomAuthToken.as_view())
path("auth/token/", views.CustomAuthToken.as_view()),
path("auth/status/", views.GetSessionInfo.as_view())
# # /view/<short> - View URL data. Cached for 15 minutes
# re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# cache_page(60 * 15)(go.views.view), name='view'),
......
......@@ -4,15 +4,16 @@ go/views.py
The functions that handle a request to a given URL. Get some data, manipulate
it, and return a rendered template.
"""
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import viewsets, permissions
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from .serializers import URLSerializer
from .models import URL
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
from rest_framework.authtoken.views import ObtainAuthToken
from .serializers import URLSerializer
from .models import URL
class URLPermission(permissions.BasePermission):
......@@ -46,11 +47,6 @@ class URLViewSet(viewsets.ModelViewSet):
serializer.save(owner=self.request.user.registereduser)
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
class CustomAuthToken(ObtainAuthToken):
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
......@@ -59,3 +55,20 @@ class CustomAuthToken(ObtainAuthToken):
token, created = Token.objects.get_or_create(user=request.user)
return Response({"token": token.key})
class GetSessionInfo(APIView):
"""Handy endpoint to return current user session status & information to the frontend."""
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, *args, **kwargs):
token, created = Token.objects.get_or_create(user=request.user)
session_info = {
"username": request.user.username,
# "full_name": f"{request.user.get_full_name}",
"last_login": request.user.last_login,
"is_authenticated": request.user.is_authenticated,
"token": token.key,
}
return Response(session_info)
This source diff could not be displayed because it is too large. You can view the blob instead.
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