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
import django.contrib.auth.models
@ -42,6 +42,7 @@ class Migration(migrations.Migration):
('summary', models.TextField(blank=True, null=True)),
('local', models.BooleanField(default=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/')),
('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)),
@ -174,7 +175,7 @@ class Migration(migrations.Migration):
name='Review',
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')),
('name', models.TextField()),
('name', models.CharField(max_length=255)),
('rating', models.IntegerField(default=0)),
('review_content', models.TextField()),
('book', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fedireads.Book')),

View file

@ -28,6 +28,8 @@ class User(AbstractUser):
blank=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)
# TODO: a field for if non-local users are readers or others
followers = models.ManyToManyField('self', symmetrical=False)
@ -131,7 +133,7 @@ class Review(Activity):
''' a book review '''
book = models.ForeignKey('Book', on_delete=models.PROTECT)
work = models.ForeignKey('Work', on_delete=models.PROTECT)
name = models.TextField()
name = models.CharField(max_length=255)
# TODO: validation
rating = models.IntegerField(default=0)
review_content = models.TextField()

View file

@ -119,4 +119,5 @@ USE_TZ = True
STATIC_URL = '/static/'
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 {
flex-grow: 0;
text-align: right;
}
#top-bar {
@ -88,10 +89,6 @@ h2 {
width: 90%;
}
form label {
display: block;
}
.review-form textarea {
width: 30rem;
height: 10rem;

View file

@ -13,15 +13,7 @@
<form class="review-form" name="review" action="/review/" method="post">
<input type="hidden" name="book" value="{{ book.openlibrary_key }}"></input>
<label for="name">Title:
<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>
{{ review_form.as_p }}
<button type="submit">Post review</button>
</form>
</div>

View file

@ -2,51 +2,13 @@
{% block content %}
<div id="content">
<div class="user-profile">
<h1>{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1>
<img class="user-pic" src="/static/images/profile.jpg">
<form name="avatar" action="/upload-avatar/" method="post" enctype="multipart/form-data">
<input type="file" name="avatar">
<form name="avatar" action="/edit_profile/" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</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>
<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>
{% endblock %}

View file

@ -47,7 +47,7 @@
{% for activity in activities %}
<div class="update">
<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 #}
<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>

View file

@ -27,20 +27,21 @@
<div id="account">
{% if user.is_authenticated %}
<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>
</form>
{% else %}
<form name="login" action="/login/" method="post">
<input type="text" name="username"></input>
<input type="password" name="password"></input>
{% for field in login_form %}
{{ field }}
{% endfor %}
<input type="submit" value="Log in"></input>
</form>
{% endif %}
</div>
<div id="search">
<form action="/search/">
<input type="text" name="q"></input>
<input type="text" name="q" placeholder="search"></input>
<input type="submit" value="🔍"></input>
</form>
</div>

View file

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

View file

@ -3,17 +3,7 @@
<div id="content">
<div>
<form name="register" method="post">
<label for="username">Username:
<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>
{{ register_form.as_p }}
<button type="submit">Create account</button>
</form>

View file

@ -2,8 +2,11 @@
{% block content %}
<div id="content">
<div class="user-profile">
<img class="user-pic" src="/static/images/profile.jpg">
<h1>{% if user.localname %}{{ user.localname }}{% else %}{{ user.username }}{% endif %}</h1>
<img class="user-pic" src="{% if user.avatar %}/images/{{ user.avatar }}{% else %}/static/images/default_avi.jpg{% endif %}">
<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>
<p>Since {{ user.created_date }}</p>
{% endif %}

View file

@ -31,6 +31,6 @@ urlpatterns = [
path('follow/', views.follow),
path('unfollow/', views.unfollow),
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)

View file

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