upload avatars (nonfunctional)

This commit is contained in:
Mouse Reeve 2020-01-27 22:49:56 -08:00
parent dae9b5c023
commit 5080de9c6b
8 changed files with 49 additions and 6 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.2 on 2020-01-28 04:47
# Generated by Django 3.0.2 on 2020-01-28 06:21
from django.conf import settings
import django.contrib.auth.models
@ -38,6 +38,7 @@ class Migration(migrations.Migration):
('actor', models.CharField(max_length=255)),
('local', models.BooleanField(default=True)),
('localname', models.CharField(blank=True, max_length=255, null=True, unique=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)),
('followers', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),

View file

@ -21,6 +21,7 @@ class User(AbstractUser):
blank=True,
unique=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)
created_date = models.DateTimeField(auto_now_add=True)

View file

@ -128,3 +128,5 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL)

View file

@ -26,7 +26,7 @@
<div id="account">
{% if user.is_authenticated %}
<form name="logout" action="/logout/" method="post">
Welcome, {{ request.user.username }}
Welcome, {% if request.user.localname %}{{ request.user.localname }}{% else %}{{ request.user.username }}{% endif %}
<input type="submit" value="Log out"></input>
</form>
{% else %}

View file

@ -3,8 +3,10 @@
<div id="main">
<div class="user-profile">
<img class="user-pic" src="/static/images/profile.jpg">
<h1>{{ user.username }}</h1>
<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">

View file

@ -15,7 +15,8 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from fedireads import federation, openlibrary, views
from fedireads import federation, views, settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
@ -23,6 +24,7 @@ urlpatterns = [
path('login/', views.user_login),
path('logout/', views.user_logout),
path('user/<str:username>', views.user_profile),
path('user/<str:username>/edit/', views.user_profile_edit),
path('book/<str:book_identifier>', views.book_page),
path('review/', views.review),
@ -30,9 +32,10 @@ urlpatterns = [
path('follow/', views.follow),
path('unfollow/', views.unfollow),
path('search/', views.search),
path('upload-avatar/', views.upload_avatar),
path('api/u/<str:username>', federation.get_actor),
path('api/u/<str:username>/inbox', federation.inbox),
path('api/u/<str:username>/outbox', federation.outbox),
path('.well-known/webfinger', federation.webfinger),
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -5,6 +5,7 @@ from django.db.models import Avg, FilteredRelation, Q
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseNotFound
from fedireads import models, openlibrary
from fedireads import federation as api
from fedireads.settings import DOMAIN
@ -67,7 +68,14 @@ def user_logout(request):
@login_required
def user_profile(request, username):
''' profile page for a user '''
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
try:
user = models.User.objects.get(username=username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
books = models.Book.objects.filter(shelves__user=user)
data = {
'user': user,
@ -77,6 +85,31 @@ def user_profile(request, username):
return TemplateResponse(request, 'user.html', data)
@login_required
def user_profile_edit(request, username):
''' profile page for a user '''
try:
user = models.User.objects.get(localname=username)
except models.User.DoesNotExist:
return HttpResponseNotFound()
data = {
'user': user,
}
return TemplateResponse(request, 'edit_user.html', data)
@csrf_exempt
@login_required
def upload_avatar(request):
''' les get fancy with images '''
if not request.method == 'POST':
return redirect('/')
request.user.avatar = request.FILES['avatar']
request.user.save()
return redirect('/user/%s' % request.user.localname)
@login_required
def book_page(request, book_identifier):
''' info about a book '''

View file

@ -1,4 +1,5 @@
Django==3.0.2
Pillow-7.0.0
psycopg2==2.8.4
pycryptodome==3.9.4
requests==2.22.0