add new pages and user login

adam
aundus 2021-01-13 17:52:23 -05:00
parent 0a815dd42d
commit ce25b01928
17 changed files with 190 additions and 3 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*__pycahce__*
db.sqlite3

View File

@ -0,0 +1,13 @@
from django import forms
from .models import Book
class NewBookForm(forms.Form):
bookid = forms.CharField(label='Book ID', max_length=12)
def is_valid():
return Book.objects.get(self.bookid) != None

View File

@ -0,0 +1,25 @@
(function ($) {
let list
try{
list = JSON.parse(localStorage.getItem('booklist'))
}catch (e){
list = {}
}
list = { '218': true }
$('.bookshelf-entry').each( function() {
const bookId = $(this).attr('id').split('-')[1]
if(list[bookId]){
console.log(bookId)
$(this).append('<div class="in-booklist"></div>')
}
else {
$(this).append('<div class="out-booklist"></div>')
}
})
})(jQuery)

View File

@ -0,0 +1,15 @@
.out-booklist {
height: 10px;
width: 10px;
float: right;
background: red;
margin-right: 20%;
}
.in-booklist {
height: 10px;
width: 10px;
float: right;
background: green;
margin-right: 20%;
}

View File

@ -0,0 +1,23 @@
{% extends "generic_template.html" %}
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h2>All Bookshelves</h2>
<strong>Number of Bookshelves:</strong> <p>{{ total }}</p>
<h1>Book List</h1>
{% if bookshelves %}
<ul>
{% for bookshelf in bookshelves %}
<li class="bookshelf" id="bookshelf-{{ bookshelf.id }}">
<a href="/bookshelves/{{ bookshelf.id }}">{{ bookshelf.bookshelf }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No bookshelves found.</p>
{% endif %}
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}"></script>
{% endblock %}

View File

@ -1,16 +1,23 @@
{% extends "generic_template.html" %}
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h1>{{bookshelf}}</h1>
<h2>Bookshelf</h2>
<strong>Number of Books in Bookshelf:</strong> <p>{{ total }}</p>
<form action="/bookshelves/{{bookshelfId}}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Add Book">
</form>
<h1>Book List</h1>
{% if books %}
<ul>
{% for book in books %}
<li>
<li class="bookshelf-entry" id="book-{{ book.id }}">
<a href="https://www.gutenberg.org/ebooks/{{ book.id }}">{{ book.title }}</a>
</li>
{% endfor %}
@ -18,6 +25,9 @@
{% else %}
<p>There are no books in the bookshelf.</p>
{% endif %}
{% load static %}
<script src="{% static 'booklist.js' %}"></script>
<link rel="stylesheet" href="{% static 'style.css' %}"></script>
{% endblock %}

View File

@ -0,0 +1,37 @@
{% extends "generic_template.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}

View File

@ -3,13 +3,17 @@ from django.http import HttpResponse
from django.shortcuts import render
from .forms import NewBookForm
from django.views import generic
# class BookListView(generic.ListView):
# model = Book
def insertBookToBookshelf(bookId, bookshelfId):
newEntry = BookshelfToBook.objects.create(bookId, bookshelfId)
newEntry.save()
def booksInBookshelf(request, bookshelfId):
@ -17,11 +21,57 @@ def booksInBookshelf(request, bookshelfId):
idList = BookshelfToBook.objects.filter(fk_bookshelves=bookshelfId).values_list('fk_books', flat=True)
books = Book.objects.filter(id__in=idList)
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = (request.POST)
print(form)
# check whether it's valid:
if form.bookid != None:
insertBookToBookshelf(form.bookid, bookshelfId)
# redirect to a new URL:
return render(request, 'index.html', context=context)
# if a GET (or any other method) we'll create a blank form
else:
form = NewBookForm()
context = {
'books': books,
'total': len(books),
'bookshelf': bookshelfName,
'bookshelfId': bookshelfId,
'form': form
}
return render(request, 'index.html', context=context)
def bookshelfList(request):
bookshelves = Bookshelf.objects.all()
context = {
'bookshelves': bookshelves,
'total': len(bookshelves),
}
return render(request, 'bookshelfList.html', context=context)
def get_name(request):
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = (request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return
# if a GET (or any other method) we'll create a blank form
else:
form = NewBookForm()
return render(request, 'name.html', {'form': form})

View File

@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
@ -55,7 +56,7 @@ ROOT_URLCONF = 'bookshelf_management.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -122,4 +123,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'

View File

@ -17,12 +17,19 @@ from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from bookshelf_management.apps.mgmt import views
from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelves', views.bookshelfList, name='detail'),
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')
]
urlpatterns += [
path('accounts/', include('django.contrib.auth.urls')),
]
# class CustomAdminSite(admin.AdminSite):
# def get_urls(self):
# urls = super(CustomAdminSite, self).get_urls()