-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayground.py
More file actions
72 lines (64 loc) · 2.28 KB
/
playground.py
File metadata and controls
72 lines (64 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import render_template, url_for, flash, redirect, request
from forms import RegistrationForm, LoginForm, EntryForm,\
NewEntryForm, UpdateAccount, SendFeedback
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
from flask_bcrypt import Bcrypt
from flask_login import login_user, login_required, logout_user, current_user
from is_safe_url import is_safe_url
from datetime import datetime
from config import app, users, bcrypt, entries, text_regex, mail
from update import update_field, update_success_msg, update_failure_msg
from login import User
from flask_mail import Message
import cloudinary
import cloudinary.uploader
import cloudinary.api
import math
import socket
@app.route('/playground/')
@app.route('/playground/<tag>/')
def playground(tag=None):
# Change search query if tag exists or not
if tag:
match_query = {'user_id': ObjectId('5f81b103c984b7e86dc415ad'), 'tags': tag}
else:
match_query = {'user_id': ObjectId('5f81b103c984b7e86dc415ad')}
# Query that returns entries, sorted by creation date or update date
entries_list = entries.aggregate([
{'$match': match_query},
{'$addFields': {
# Sorts by created date, or updated date if it exists
'sort_date': {
'$cond': {
'if': '$updated_on',
'then': '$updated_on',
'else': '$created_on'
}
}
}},
{'$sort': {'sort_date': -1}},
])
return render_template(
'pages/listing.html',
title="Listing",
entries=entries_list,
tag=tag,
entry_count=1,
playground=True)
@app.route('/playground/entry/<entry_id>/')
def playground_entry(entry_id):
form = EntryForm()
the_entry = entries.find_one({"_id": ObjectId(entry_id)})
form.name.data = the_entry["name"]
form.description.data = the_entry["description"]
form.rating.data = str(the_entry["rating"])
form.hidden_id.data = entry_id
if the_entry.get("tags") is not None:
form.hidden_tags.data = ','.join(the_entry["tags"])
return render_template(
'pages/entry.html',
title="Entry",
entry=the_entry,
form=form,
playground=True)