diff --git a/.gitignore b/.gitignore index dfa0e64..4df2b19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ coriplus.sqlite -__pycache__/ \ No newline at end of file +__pycache__/ +uploads/ \ No newline at end of file diff --git a/app.py b/app.py index 56645fd..f4cf64a 100644 --- a/app.py +++ b/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/.jpg') +def uploads(id, type='jpg'): + return send_from_directory(UPLOAD_DIRECTORY, id + '.' + type) + @app.route('/ajax/username_availability/') def username_availability(username): if session.get('logged_in'): diff --git a/static/lib.js b/static/lib.js index 09755c6..55edfb0 100644 --- a/static/lib.js +++ b/static/lib.js @@ -82,4 +82,9 @@ function requestUsernameAvailability(u, callback){ } } xhr.send(); +} + +function attachFileInput(){ + var fileInput = document.getElementById('fileInputContainer'); + fileInput.innerHTML = ''; } \ No newline at end of file diff --git a/static/style.css b/static/style.css index 6dad08f..6ba4f04 100644 --- a/static/style.css +++ b/static/style.css @@ -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} diff --git a/templates/create.html b/templates/create.html index 9e4503b..3c644f1 100644 --- a/templates/create.html +++ b/templates/create.html @@ -1,10 +1,11 @@ {% extends "base.html" %} {% block body %}

Create

-
+
Message:
+
Add a file...
diff --git a/templates/includes/message.html b/templates/includes/message.html index 034bcf6..8991530 100644 --- a/templates/includes/message.html +++ b/templates/includes/message.html @@ -1,2 +1,7 @@

{{ message.text|enrich }}

+{% if message.uploads %} +
+ +
+{% endif %}