forms forms forms

This commit is contained in:
Mouse Reeve 2020-01-28 23:23:05 -08:00
parent 3d09d355eb
commit 789b9a1dc0
13 changed files with 71 additions and 100 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.2 on 2020-01-29 02:56 # Generated by Django 3.0.2 on 2020-01-29 06:31
from django.conf import settings from django.conf import settings
import django.contrib.auth.models import django.contrib.auth.models
@ -42,6 +42,7 @@ class Migration(migrations.Migration):
('summary', models.TextField(blank=True, null=True)), ('summary', models.TextField(blank=True, null=True)),
('local', models.BooleanField(default=True)), ('local', models.BooleanField(default=True)),
('localname', models.CharField(blank=True, max_length=255, null=True, unique=True)), ('localname', models.CharField(blank=True, max_length=255, null=True, unique=True)),
('name', models.CharField(blank=True, max_length=100, null=True)),
('avatar', models.ImageField(blank=True, null=True, upload_to='uploads/')), ('avatar', models.ImageField(blank=True, null=True, upload_to='uploads/')),
('created_date', models.DateTimeField(auto_now_add=True)), ('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)), ('updated_date', models.DateTimeField(auto_now=True)),
@ -174,7 +175,7 @@ class Migration(migrations.Migration):
name='Review', name='Review',
fields=[ fields=[
('activity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='fedireads.Activity')), ('activity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='fedireads.Activity')),
('name', models.TextField()), ('name', models.CharField(max_length=255)),
('rating', models.IntegerField(default=0)), ('rating', models.IntegerField(default=0)),
('review_content', models.TextField()), ('review_content', models.TextField()),
('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Book')), ('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Book')),

View file

@ -28,6 +28,8 @@ class User(AbstractUser):
blank=True, blank=True,
unique=True unique=True
) )
# name is your display name, which you can change at will
name = models.CharField(max_length=100, blank=True, null=True)
avatar = models.ImageField(upload_to='uploads/', blank=True, null=True) avatar = models.ImageField(upload_to='uploads/', blank=True, null=True)
# TODO: a field for if non-local users are readers or others # TODO: a field for if non-local users are readers or others
followers = models.ManyToManyField('self', symmetrical=False) followers = models.ManyToManyField('self', symmetrical=False)
@ -131,7 +133,7 @@ class Review(Activity):
''' a book review ''' ''' a book review '''
book = models.ForeignKey('Book', on_delete=models.PROTECT) book = models.ForeignKey('Book', on_delete=models.PROTECT)
work = models.ForeignKey('Work', on_delete=models.PROTECT) work = models.ForeignKey('Work', on_delete=models.PROTECT)
name = models.TextField() name = models.CharField(max_length=255)
# TODO: validation # TODO: validation
rating = models.IntegerField(default=0) rating = models.IntegerField(default=0)
review_content = models.TextField() review_content = models.TextField()

View file

@ -119,4 +119,5 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
MEDIA_URL = '/images/' MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL) # TODO: this super doesn't work when I do a join on BASE_DIR
MEDIA_ROOT = '/Users/mouse/Personal/fedireads/images/'

View file

@ -35,6 +35,7 @@ h2 {
} }
#actions { #actions {
flex-grow: 0; flex-grow: 0;
text-align: right;
} }
#top-bar { #top-bar {
@ -88,10 +89,6 @@ h2 {
width: 90%; width: 90%;
} }
form label {
display: block;
}
.review-form textarea { .review-form textarea {
width: 30rem; width: 30rem;
height: 10rem; height: 10rem;

View file

@ -13,15 +13,7 @@
<form class="review-form" name="review" action="/review/" method="post"> <form class="review-form" name="review" action="/review/" method="post">
<input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input> <input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input>
<label for="name">Title: {{ review_form.as_p }}
<input type="text" name="name"></input>
</label>
<label for="content">
<textarea name="content"></textarea>
</label>
<label for="rating">Your rating (1-5)
<input type="number" name="rating"></input>
</label>
<button type="submit">Post review</button> <button type="submit">Post review</button>
</form> </form>
</div> </div>

View file

@ -2,51 +2,13 @@
{% block content %} {% block content %}
<div id="content"> <div id="content">
<div class="user-profile"> <div class="user-profile">
<h1>{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1>
<img class="user-pic" src="/static/images/profile.jpg"> <img class="user-pic" src="/static/images/profile.jpg">
<form name="avatar" action="/upload-avatar/" method="post" enctype="multipart/form-data"> <form name="avatar" action="/edit_profile/" method="post" enctype="multipart/form-data">
<input type="file" name="avatar"> {% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button> <button type="submit">Upload</button>
</form> </form>
<h1>{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1>
{% if is_self %}<a href="/user/{{ user.localname }}/edit">Edit profile</a>
<p>Since {{ user.created_date }}</p>
{% endif %}
{% if not is_self %}
{% if not following %}
<form action="/follow/" method="post">
<input type="hidden" name="user" value="{{ user.id }}"></input>
<input type="submit" value="Follow"></input>
</form>
{% else %}
<form action="/unfollow/" method="post">
<input type="hidden" name="user" value="{{ user.id }}"></input>
<input type="submit" value="Unfollow"></input>
</form>
{% endif %}
{% endif %}
</div> </div>
<div>
<h2>Books</h2>
{% for book in books.all %}
<div class="book">
{{ book.data.title }} by {{ book.authors.first.data.name }}
</div>
{% endfor %}
</div>
<div>
<h2>Followers</h2>
{% for follower in user.followers.all %}
<div>
<a href="{{ follower.actor }}">{{ follower.username }}</a>
<form action="/follow/" method="post">
<input type="hidden" name="user" value="{{ follower.id }}"></input>
<input type="submit" value="Follow"></input>
</form>
</div>
{% endfor %}
</div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -47,7 +47,7 @@
{% for activity in activities %} {% for activity in activities %}
<div class="update"> <div class="update">
<h2> <h2>
<img class="user-pic" src="static/images/profile.jpg"> <img class="user-pic" src="{% if activity.user.avatar %}/images/{{ activity.user.avatar }}{% else %}/static/images/default_avi.jpg{% endif %}">
{# TODO: a helper function for displaying a username #} {# TODO: a helper function for displaying a username #}
<a href="/user/{% if activity.user.localname %}{{ activity.user.localname }}{% else %}{{ activity.user.username }}{% endif %}" class="user"> <a href="/user/{% if activity.user.localname %}{{ activity.user.localname }}{% else %}{{ activity.user.username }}{% endif %}" class="user">
{% if activity.user.localname %}{{ activity.user.localname }}{% else %}{{ activity.user.username }}{% endif %}</a> {% if activity.user.localname %}{{ activity.user.localname }}{% else %}{{ activity.user.username }}{% endif %}</a>

View file

@ -27,20 +27,21 @@
<div id="account"> <div id="account">
{% if user.is_authenticated %} {% if user.is_authenticated %}
<form name="logout" action="/logout/" method="post"> <form name="logout" action="/logout/" method="post">
Welcome, {% if request.user.localname %}{{ request.user.localname }}{% else %}{{ request.user.username }}{% endif %} Welcome, <a href="{{ request.user.actor }}">{% if request.user.localname %}{{ request.user.localname }}{% else %}{{ request.user.username }}{% endif %}</a>
<input type="submit" value="Log out"></input> <input type="submit" value="Log out"></input>
</form> </form>
{% else %} {% else %}
<form name="login" action="/login/" method="post"> <form name="login" action="/login/" method="post">
<input type="text" name="username"></input> {% for field in login_form %}
<input type="password" name="password"></input> {{ field }}
{% endfor %}
<input type="submit" value="Log in"></input> <input type="submit" value="Log in"></input>
</form> </form>
{% endif %} {% endif %}
</div> </div>
<div id="search"> <div id="search">
<form action="/search/"> <form action="/search/">
<input type="text" name="q"></input> <input type="text" name="q" placeholder="search"></input>
<input type="submit" value="🔍"></input> <input type="submit" value="🔍"></input>
</form> </form>
</div> </div>

View file

@ -3,13 +3,7 @@
<div id="content"> <div id="content">
<div> <div>
<form name="login" method="post"> <form name="login" method="post">
<label for="username">Username: {{ login_form.as_p }}
<input type="text" name="username"></input>
</label>
<label for="password">Password:
<input type="password" name="password"></input>
</label>
<button type="submit">Log in</button> <button type="submit">Log in</button>
</form> </form>
<a href="/register/">Create a new account</a> <a href="/register/">Create a new account</a>

View file

@ -3,17 +3,7 @@
<div id="content"> <div id="content">
<div> <div>
<form name="register" method="post"> <form name="register" method="post">
<label for="username">Username: {{ register_form.as_p }}
<input type="text" name="username"></input>
</label>
<label for="email">Email address:
<input type="text" name="email"></input>
</label>
<label for="password">Password:
<input type="password" name="password"></input>
</label>
<button type="submit">Create account</button> <button type="submit">Create account</button>
</form> </form>

View file

@ -2,8 +2,11 @@
{% block content %} {% block content %}
<div id="content"> <div id="content">
<div class="user-profile"> <div class="user-profile">
<img class="user-pic" src="/static/images/profile.jpg"> <img class="user-pic" src="{% if user.avatar %}/images/{{ user.avatar }}{% else %}/static/images/default_avi.jpg{% endif %}">
<h1>{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1> <h1>{% if user.name %}{{ user.name }}<br>{% endif %}{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1>
{% if user.summary %}
<blockquote>{{ user.summary }}</blockquote>
{% endif %}
{% if is_self %}<a href="/user/{{ user.localname }}/edit">Edit profile</a> {% if is_self %}<a href="/user/{{ user.localname }}/edit">Edit profile</a>
<p>Since {{ user.created_date }}</p> <p>Since {{ user.created_date }}</p>
{% endif %} {% endif %}

View file

@ -31,6 +31,6 @@ urlpatterns = [
path('follow/', views.follow), path('follow/', views.follow),
path('unfollow/', views.unfollow), path('unfollow/', views.unfollow),
path('search/', views.search), path('search/', views.search),
path('upload-avatar/', views.upload_avatar), path('edit_profile/', views.edit_profile),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -8,7 +8,7 @@ from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import re import re
from fedireads import models, openlibrary, outgoing as api from fedireads import forms, models, openlibrary, outgoing as api
from fedireads.settings import DOMAIN from fedireads.settings import DOMAIN
@ -32,12 +32,14 @@ def home(request):
'-created_date' '-created_date'
)[:10] )[:10]
login_form = forms.LoginForm()
data = { data = {
'user': request.user, 'user': request.user,
'shelves': shelves, 'shelves': shelves,
'recent_books': recent_books, 'recent_books': recent_books,
'user_books': user_books, 'user_books': user_books,
'activities': activities, 'activities': activities,
'login_form': login_form,
} }
return TemplateResponse(request, 'feed.html', data) return TemplateResponse(request, 'feed.html', data)
@ -47,12 +49,17 @@ def user_login(request):
''' authentication ''' ''' authentication '''
# send user to the login page # send user to the login page
if request.method == 'GET': if request.method == 'GET':
return TemplateResponse(request, 'login.html') form = forms.LoginForm()
return TemplateResponse(request, 'login.html', {'login_form': form})
# authenticate user # authenticate user
username = request.POST['username'] form = forms.LoginForm(request.POST)
if not form.is_valid():
return TemplateResponse(request, 'login.html')
username = form.data['username']
username = '%s@%s' % (username, DOMAIN) username = '%s@%s' % (username, DOMAIN)
password = request.POST['password'] password = form.data['password']
user = authenticate(request, username=username, password=password) user = authenticate(request, username=username, password=password)
if user is not None: if user is not None:
login(request, user) login(request, user)
@ -72,12 +79,20 @@ def user_logout(request):
def register(request): def register(request):
''' join the server ''' ''' join the server '''
if request.method == 'GET': if request.method == 'GET':
return TemplateResponse(request, 'register.html') form = forms.RegisterForm()
return TemplateResponse(
request,
'register.html',
{'register_form': form}
)
username = request.POST['username'] form = forms.RegisterForm(request.POST)
password = request.POST['password'] if not form.is_valid():
email = request.POST['email'] return redirect('/register/')
password = request.POST['password']
username = form.data['username']
email = form.data['email']
password = form.data['password']
user = models.User.objects.create_user(username, email, password) user = models.User.objects.create_user(username, email, password)
login(request, user) login(request, user)
@ -112,7 +127,9 @@ def user_profile_edit(request, username):
except models.User.DoesNotExist: except models.User.DoesNotExist:
return HttpResponseNotFound() return HttpResponseNotFound()
form = forms.EditUserForm()
data = { data = {
'form': form,
'user': user, 'user': user,
} }
return TemplateResponse(request, 'edit_user.html', data) return TemplateResponse(request, 'edit_user.html', data)
@ -120,11 +137,17 @@ def user_profile_edit(request, username):
@csrf_exempt @csrf_exempt
@login_required @login_required
def upload_avatar(request): def edit_profile(request):
''' les get fancy with images ''' ''' les get fancy with images '''
if not request.method == 'POST': if not request.method == 'POST':
return redirect('/user/%s' % request.user.localname)
form = forms.EditUserForm(request.POST, request.FILES)
if not form.is_valid():
return redirect('/') return redirect('/')
request.user.avatar = request.FILES['avatar'] request.user.name = form.data['name']
if 'avatar' in form.files:
request.user.avatar = form.files['avatar']
request.user.summary = form.data['summary']
request.user.save() request.user.save()
return redirect('/user/%s' % request.user.localname) return redirect('/user/%s' % request.user.localname)
@ -137,10 +160,12 @@ def book_page(request, book_identifier):
Q(work=book.works.first()) | Q(book=book) Q(work=book.works.first()) | Q(book=book)
) )
rating = reviews.aggregate(Avg('rating')) rating = reviews.aggregate(Avg('rating'))
review_form = forms.ReviewForm()
data = { data = {
'book': book, 'book': book,
'reviews': reviews, 'reviews': reviews,
'rating': rating['rating__avg'], 'rating': rating['rating__avg'],
'review_form': review_form,
} }
return TemplateResponse(request, 'book.html', data) return TemplateResponse(request, 'book.html', data)
@ -160,14 +185,17 @@ def shelve(request, shelf_id, book_id):
@login_required @login_required
def review(request): def review(request):
''' create a book review note ''' ''' create a book review note '''
# TODO: error handling form = forms.ReviewForm(request.POST)
# TODO: better failure behavior
if not form.is_valid():
return redirect('/')
book_identifier = request.POST.get('book') book_identifier = request.POST.get('book')
book = openlibrary.get_or_create_book(book_identifier) book = openlibrary.get_or_create_book(book_identifier)
# TODO: validation, htmlification # TODO: validation, htmlification
name = request.POST.get('name') name = form.data.get('name')
content = request.POST.get('content') content = form.data.get('review_content')
rating = request.POST.get('rating') rating = form.data.get('rating')
api.handle_review(request.user, book, name, content, rating) api.handle_review(request.user, book, name, content, rating)
return redirect(book_identifier) return redirect(book_identifier)