JS and srv.py refactoring

This commit is contained in:
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('/')
def home(first_id=None, second_id=None, third_id=None):
'''
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)
return render_template('home.html')
@app.route('/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'])
def api_save():

View file

@ -1,16 +1,34 @@
$(function() {
$.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) {
results = data.responseData.results;
//for(var i in results)
{
var i = 0; // hardcore
function reload() {
$.getJSON('/api/words', function(words) {
$.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) {
results = data.responseData.results;
// replace words
$('#word1').text(words[0]);
$('#word2').text(words[1]);
// TODO: insert it w/out jQuery, would be faster
$img = $('<img>');
$img.attr('src', results[i].url);
$img.appendTo($('#image_wrapper'));
return;
}
})
$img.attr('src', results[0].url);
$('#image_wrapper').html($img);
});
});
}
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) {
window.location.reload()
reload();
});
});

View file

@ -52,10 +52,10 @@
</style>
</head>
<body>
<script>words = {{ words|jsonify }}; </script>
<span id="word1">{{ words[0]|e }}</span>
<span id="word1"></span>
<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>
<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>