JS and srv.py refactoring

master
Michał 'czesiek' Czyżewski 2013-03-22 22:01:12 +01:00
parent 7015f78c2d
commit d2bffc0117
3 changed files with 62 additions and 39 deletions

53
srv.py
View File

@ -15,33 +15,38 @@ app.jinja_env.filters['jsonify'] = jsonify
@app.route('/<int:first_id>/<int:second_id>/<int:third_id>') @app.route('/<int:first_id>/<int:second_id>/<int:third_id>')
@app.route('/') @app.route('/')
def home(first_id=None, second_id=None, third_id=None): def home(first_id=None, second_id=None, third_id=None):
''' return render_template('home.html')
print len(array)
while first_id == None:
rand_int = randint(0, len(array)-1)
value = array[rand_int]
if len(value) > 0:
first_id = rand_int
while second_id == None:
rand_int = randint(0, len(array)-1)
value = array[rand_int]
if len(value) > 0:
second_id = rand_int
'''
def get_words(words):
ret = []
for word_id in words:
word = array[word_id] if word_id != None and word_id < len(array) else random.choice(array)
ret.append(word.decode('utf-8'))
return ret
words = get_words([first_id, second_id, third_id])
return render_template('home.html', words=words)
@app.route('/api/words') @app.route('/api/words')
def api_words(): def api_words():
return json.dumps(['frirst', '2nd', 'th33rd']) # NOTE: below is old code from route('/') up for a review
#
#'''
# print len(array)
#while first_id == None:
# rand_int = randint(0, len(array)-1)
# value = array[rand_int]
# if len(value) > 0:
# first_id = rand_int
#while second_id == None:
# rand_int = randint(0, len(array)-1)
# value = array[rand_int]
# if len(value) > 0:
# second_id = rand_int
#'''
#
#def get_words(words):
# ret = []
# for word_id in words:
# word = array[word_id] if word_id != None and word_id < len(array) else random.choice(array)
# ret.append(word.decode('utf-8'))
# return ret
word1 = random.choice(array);
word2 = random.choice(array);
word3 = random.choice(array);
return json.dumps([word1, word2, word3])
@app.route('/api/save', methods=['POST']) @app.route('/api/save', methods=['POST'])
def api_save(): def api_save():

View File

@ -1,16 +1,34 @@
$(function() { function reload() {
$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=medium|large|xlarge|xxlarge&rsz=8&as_rights=(cc_publicdomain|cc_attribute|cc_sharealike|cc_nonderived).-(cc_noncommertial)&as_filetype=jpg|png|gif&safe=moderate&callback=?', {'q':words.join(' || ')}, function(data) { $.getJSON('/api/words', function(words) {
results = data.responseData.results; $.getJSON('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&imgsz=medium|large|xlarge|xxlarge&rsz=8&as_rights=(cc_publicdomain|cc_attribute|cc_sharealike|cc_nonderived).-(cc_noncommertial)&as_filetype=jpg|png|gif&safe=moderate&callback=?', {'q': words.join(' || ')}, function(data) {
//for(var i in results) results = data.responseData.results;
{
var i = 0; // hardcore // replace words
$('#word1').text(words[0]);
$('#word2').text(words[1]);
// TODO: insert it w/out jQuery, would be faster
$img = $('<img>'); $img = $('<img>');
$img.attr('src', results[i].url); $img.attr('src', results[0].url);
$img.appendTo($('#image_wrapper')); $('#image_wrapper').html($img);
return; });
} });
}) }
function prefetch_images() {
// check how much images are there
var qty_max = 3;
var qty = qty_max - 1; // testing
// fetch and add images
for(var i = 0; i < qty; i++) {
}
}
$(function() {
reload();
$(document).click(function(e) { $(document).click(function(e) {
window.location.reload() reload();
}); });
}); });

View File

@ -52,10 +52,10 @@
</style> </style>
</head> </head>
<body> <body>
<script>words = {{ words|jsonify }}; </script> <span id="word1"></span>
<span id="word1">{{ words[0]|e }}</span>
<div id="image_wrapper"></div> <div id="image_wrapper"></div>
<span id="word2">{{ words[1]|e }}</span><br /> <span id="word2"></span>
<br />
<div class="foot">Copyright &copy; RiddleKiller Ltd.<br>Features Kelson Sans font by <a href=http://www.fontfabric.com/>FontFabric.com/</a></div> <div class="foot">Copyright &copy; RiddleKiller Ltd.<br>Features Kelson Sans font by <a href=http://www.fontfabric.com/>FontFabric.com/</a></div>
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery-1.7.2.min.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='js/jquery-1.7.2.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/penis.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='js/penis.js') }}"></script>