Added file upload
This commit is contained in:
parent
4dd61c825a
commit
244f689afc
6 changed files with 45 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
coriplus.sqlite
|
||||
__pycache__/
|
||||
__pycache__/
|
||||
uploads/
|
||||
34
app.py
34
app.py
|
|
@ -1,9 +1,9 @@
|
|||
from flask import (
|
||||
Flask, Markup, abort, flash, g, jsonify, redirect, render_template, request,
|
||||
session, url_for)
|
||||
send_from_directory, session, url_for)
|
||||
import hashlib
|
||||
from peewee import *
|
||||
import datetime, time, re
|
||||
import datetime, time, re, os
|
||||
from functools import wraps
|
||||
|
||||
DATABASE = 'coriplus.sqlite'
|
||||
|
|
@ -88,9 +88,21 @@ class Relationship(BaseModel):
|
|||
)
|
||||
|
||||
|
||||
UPLOAD_DIRECTORY = 'uploads/'
|
||||
class Upload(BaseModel):
|
||||
# the extension of the media
|
||||
type = TextField()
|
||||
# the message bound to this media
|
||||
message = ForeignKeyField(Message, backref='uploads')
|
||||
# helper to retrieve contents
|
||||
def filename(self):
|
||||
return str(self.id) + '.' + self.type
|
||||
|
||||
def create_tables():
|
||||
with database:
|
||||
database.create_tables([User, Message, Relationship])
|
||||
database.create_tables([User, Message, Relationship, Upload])
|
||||
if not os.path.isdir(UPLOAD_DIRECTORY):
|
||||
os.makedirs(UPLOAD_DIRECTORY)
|
||||
|
||||
_forbidden_extensions = 'com net org txt'.split()
|
||||
|
||||
|
|
@ -221,7 +233,8 @@ def private_timeline():
|
|||
user = get_current_user()
|
||||
messages = (Message
|
||||
.select()
|
||||
.where(Message.user << user.following())
|
||||
.where((Message.user << user.following())
|
||||
| (Message.user == user))
|
||||
.order_by(Message.pub_date.desc()))
|
||||
return object_list('private_messages.html', messages, 'message_list')
|
||||
|
||||
|
|
@ -328,11 +341,24 @@ def create():
|
|||
user=user,
|
||||
text=request.form['text'],
|
||||
pub_date=datetime.datetime.now())
|
||||
file = request.files.get('file')
|
||||
if file:
|
||||
print(file.name)
|
||||
ext = file.name.split('.')[-1]
|
||||
upload = Upload.create(
|
||||
type=ext,
|
||||
message=message
|
||||
)
|
||||
file.save(UPLOAD_DIRECTORY + str(upload.id) + '.' + ext)
|
||||
flash('Your message has been posted successfully')
|
||||
return redirect(url_for('user_detail', username=user.username))
|
||||
|
||||
return render_template('create.html')
|
||||
|
||||
@app.route('/uploads/<id>.jpg')
|
||||
def uploads(id, type='jpg'):
|
||||
return send_from_directory(UPLOAD_DIRECTORY, id + '.' + type)
|
||||
|
||||
@app.route('/ajax/username_availability/<username>')
|
||||
def username_availability(username):
|
||||
if session.get('logged_in'):
|
||||
|
|
|
|||
|
|
@ -82,4 +82,9 @@ function requestUsernameAvailability(u, callback){
|
|||
}
|
||||
}
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function attachFileInput(){
|
||||
var fileInput = document.getElementById('fileInputContainer');
|
||||
fileInput.innerHTML = '<input type="file" accept="image/*" name="file">';
|
||||
}
|
||||
|
|
@ -7,3 +7,4 @@ body{margin:0}
|
|||
.metanav{float:right}
|
||||
.header h1{margin:0;display:inline-block}
|
||||
.flash{background-color:#ff9;border:yellow 1px solid}
|
||||
.message-visual img{max-width:100%;max-height:8em}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
{% extends "base.html" %}
|
||||
{% block body %}
|
||||
<h2>Create</h2>
|
||||
<form action="{{ url_for('create') }}" method=post>
|
||||
<form action="{{ url_for('create') }}" method="POST" enctype="multipart/form-data">
|
||||
<dl>
|
||||
<dt>Message:</dt>
|
||||
<dd><textarea name="text" placeholder="What's happening?"></textarea></dd>
|
||||
<dd id="fileInputContainer"><a href="javascript:attachFileInput();">Add a file...</a>
|
||||
<dd><input type="submit" value="Create" /></dd>
|
||||
</dl>
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -1,2 +1,7 @@
|
|||
<p class="message-content">{{ message.text|enrich }}</p>
|
||||
{% if message.uploads %}
|
||||
<div class="message-visual">
|
||||
<img src="/uploads/{{message.uploads[0].filename()}}">
|
||||
</div>
|
||||
{% endif %}
|
||||
<p class="message-footer"><a href="{{ url_for('user_detail', username=message.user.username) }}">{{ message.user.username }}</a> - {{ message.pub_date | human_date }}</p>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue