Skip to content

Commit 56c1822

Browse files
authored
Merge pull request #432 from navascogt/issue#378
feat: Citas aleatorias en página de inicio
2 parents ae405d3 + 4d49aea commit 56c1822

5 files changed

Lines changed: 71 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import RandomQuote from './random-quotes'
2+
3+
4+
document.addEventListener('DOMContentLoaded', () => {
5+
6+
RandomQuote.init()
7+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function init() {
2+
getRandomQuote();
3+
4+
// Change quote every RANDOM_QUOTE_INTERVAL
5+
setInterval(function() { getRandomQuote(); }, random_quote_interval);
6+
7+
function getRandomQuote() {
8+
// Get the text and author span id
9+
let text = document.getElementById("quote-text");
10+
let author = document.getElementById("quote-author");
11+
// Load random quote and convert to JSON
12+
fetch(quotes_api_url)
13+
.then(response => response.json())
14+
.then(data => {
15+
text.textContent = data.result.text
16+
author.textContent = data.result.author
17+
})
18+
// Animate display
19+
let quote_block = document.getElementById("quote-block");
20+
fadeInQuote(quote_block)
21+
}
22+
}
23+
24+
function fadeInQuote(quote, duration=2000) {
25+
quote.animate([
26+
{ // from
27+
opacity: 0,
28+
},
29+
{ // to
30+
opacity: 1,
31+
}
32+
], duration);
33+
}
34+
35+
export default {
36+
init
37+
}

apps/homepage/templates/homepage/index.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@
3737

3838
<img class="logo" src="{{ assets|get_asset_key:'commons/img/logo_text.png' }}" alt="Python Canarias" />
3939

40-
{% if quote %}
41-
<div class="quote is-centered">
42-
<p><i class="fas fa-quote-left"></i> {{ quote.text }} <i class="fas fa-quote-right"></i></p>
43-
<p>&mdash; {{ quote.author }}</p>
40+
<div id="quote-block" class="quote is-centered">
41+
<p>
42+
<i class="fas fa-quote-left"></i>
43+
<span id="quote-text">
44+
</span>
45+
<i class="fas fa-quote-right"></i>
46+
</p>
47+
<p>&mdash;
48+
<span id="quote-author">
49+
</span>
50+
</p>
4451
</div>
45-
{% endif %}
4652

4753
<div class="links buttons is-centered">
4854
<a href="{% url 'about:index' %}" class="link button is-primary is-medium is-outlined">Entrar</a>
@@ -93,5 +99,13 @@ <h4 class="blog-message">Para más noticias, pásate por nuestro
9399
</div>
94100
</div>
95101
</div>
102+
103+
<!-- Live random quotes managed through JS -->
104+
<script>
105+
const random_quote_interval = "{{ random_quote_interval }}";
106+
const quotes_api_url = "{% url 'api:random_quote' %}";
107+
</script>
108+
<script src="{{ assets|get_asset_key:'homepage/custom.min.js' }}"></script>
109+
96110
</body>
97111
</html>

apps/homepage/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from django.shortcuts import render
44

5+
from django.conf import settings
6+
57
from apps.events.models import Event
68
from apps.quotes.models import Quote
79
from apps.jobs.models import JobOffer
@@ -12,4 +14,5 @@ def homepage(request):
1214
'active_events': Event.objects.all().filter(active=True).count(),
1315
'quote': Quote.get_random_quote(),
1416
'jobs_count': JobOffer.actives.count(),
17+
'random_quote_interval': settings.RANDOM_QUOTE_INTERVAL,
1518
})

main/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@
331331
TWITTER_ACCESS_TOKEN = config('TWITTER_ACCESS_TOKEN')
332332
TWITTER_ACCESS_TOKEN_SECRET = config('TWITTER_ACCESS_TOKEN_SECRET')
333333

334+
# Random quote interval (seconds)
335+
RANDOM_QUOTE_INTERVAL = config(
336+
'RANDOM_QUOTE_INTERVAL', default=10, cast=lambda i: 1000 * int(i)
337+
)
338+
334339
if DEBUG:
335340
MESSAGE_LEVEL = message_constants.DEBUG
336341

0 commit comments

Comments
 (0)