Skip to content

Commit 6e6a388

Browse files
authored
feat: add on this day from wiki (#121)
* feat: add on this day from wiki
1 parent fdb5d87 commit 6e6a388

File tree

8 files changed

+145
-37
lines changed

8 files changed

+145
-37
lines changed

app/database/models.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from datetime import datetime
44
from typing import Dict, Any
55

6+
7+
from app.config import PSQL_ENVIRONMENT
8+
from app.database.database import Base
69
from sqlalchemy import (DDL, Boolean, Column, DateTime, ForeignKey, Index,
7-
Integer, String, event, UniqueConstraint)
10+
Integer, String, event, UniqueConstraint, JSON)
811
from sqlalchemy.dialects.postgresql import TSVECTOR
912
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
1013
from sqlalchemy.orm import relationship, Session
1114

12-
from app.config import PSQL_ENVIRONMENT
13-
from app.database.database import Base
1415
from app.dependencies import logger
1516

1617

@@ -151,6 +152,16 @@ def __repr__(self):
151152
)
152153

153154

155+
class WikipediaEvents(Base):
156+
__tablename__ = "wikipedia_events"
157+
158+
id = Column(Integer, primary_key=True, index=True)
159+
date_ = Column(String, nullable=False)
160+
wikipedia = Column(String, nullable=False)
161+
events = Column(JSON, nullable=True)
162+
date_inserted = Column(DateTime, default=datetime.utcnow)
163+
164+
154165
class Quote(Base):
155166
__tablename__ = "quotes"
156167

app/internal/on_this_day_events.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
from datetime import date, datetime
3+
from typing import Any, Dict
4+
import requests
5+
from fastapi import Depends
6+
from loguru import logger
7+
from sqlalchemy import func
8+
from sqlalchemy.exc import SQLAlchemyError
9+
from sqlalchemy.orm import Session
10+
from sqlalchemy.orm.exc import NoResultFound
11+
12+
from app.database.database import get_db
13+
from app.database.models import WikipediaEvents
14+
15+
16+
def insert_on_this_day_data(
17+
db: Session = Depends(get_db)
18+
) -> Dict[str, Any]:
19+
now = datetime.now()
20+
day, month = now.day, now.month
21+
22+
res = requests.get(
23+
f'https://byabbe.se/on-this-day/{month}/{day}/events.json')
24+
text = json.loads(res.text)
25+
res_events = text.get('events')
26+
res_date = text.get('date')
27+
res_wiki = text.get('wikipedia')
28+
db.add(WikipediaEvents(events=res_events,
29+
date_=res_date, wikipedia=res_wiki))
30+
db.commit()
31+
return text
32+
33+
34+
def get_on_this_day_events(
35+
db: Session = Depends(get_db)
36+
) -> Dict[str, Any]:
37+
try:
38+
data = (db.query(WikipediaEvents).
39+
filter(
40+
func.date(WikipediaEvents.date_inserted) == date.today()).
41+
one())
42+
43+
except NoResultFound:
44+
data = insert_on_this_day_data(db)
45+
except (SQLAlchemyError, AttributeError) as e:
46+
logger.error(f'on this day failed with error: {e}')
47+
data = {'events': [], 'wikipedia': 'https://en.wikipedia.org/'}
48+
return data

app/routers/profile.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import io
22

3-
from fastapi import APIRouter, Depends, File, Request, UploadFile
4-
from starlette.responses import RedirectResponse
5-
from starlette.status import HTTP_302_FOUND
6-
from PIL import Image
7-
83
from app import config
94
from app.database.database import get_db
105
from app.database.models import User
116
from app.dependencies import MEDIA_PATH, templates
7+
from app.internal.on_this_day_events import get_on_this_day_events
8+
9+
from fastapi import APIRouter, Depends, File, Request, UploadFile
10+
from PIL import Image
11+
from starlette.responses import RedirectResponse
12+
from starlette.status import HTTP_302_FOUND
1213

1314
PICTURE_EXTENSION = config.PICTURE_EXTENSION
1415
PICTURE_SIZE = config.AVATAR_SIZE
@@ -45,10 +46,14 @@ async def profile(
4546
session.commit()
4647
user = session.query(User).filter_by(id=1).first()
4748

49+
# Get on this day data from wiki
50+
on_this_day_data = get_on_this_day_events(session)
51+
4852
return templates.TemplateResponse("profile.html", {
4953
"request": request,
5054
"user": user,
5155
"events": upcoming_events,
56+
"on_this_day_data": on_this_day_data
5257
})
5358

5459

app/static/style.css

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
body {
2-
background: #A1FFCE;
3-
background: -webkit-linear-gradient(to right, #FAFFD1, #A1FFCE);
4-
background: linear-gradient(to right, #FAFFD1, #A1FFCE);
2+
background: #a1ffce;
3+
background: -webkit-linear-gradient(to right, #faffd1, #a1ffce);
4+
background: linear-gradient(to right, #faffd1, #a1ffce);
55
}
66

77
.profile-image {
@@ -25,38 +25,42 @@ body {
2525
}
2626

2727
.no-border {
28-
border: none;
28+
border: none;
2929
}
3030

3131
.profile-modal-fadeIn {
32-
-webkit-animation-name: profile-modal-fadeIn;
33-
animation-name: profile-modal-fadeIn;
32+
-webkit-animation-name: profile-modal-fadeIn;
33+
animation-name: profile-modal-fadeIn;
3434

35-
-webkit-animation-duration: 1s;
36-
animation-duration: 1s;
37-
-webkit-animation-fill-mode: both;
38-
animation-fill-mode: both;
35+
-webkit-animation-duration: 1s;
36+
animation-duration: 1s;
37+
-webkit-animation-fill-mode: both;
38+
animation-fill-mode: both;
39+
}
40+
41+
@keyframes profile-modal-fadeIn {
42+
from {
43+
opacity: 0;
44+
-webkit-transform: translate3d(-100%, 0, 0);
45+
transform: translate3d(-100%, 0, 0);
3946
}
4047

41-
@keyframes profile-modal-fadeIn {
42-
from {
43-
opacity: 0;
44-
-webkit-transform: translate3d(-100%, 0, 0);
45-
transform: translate3d(-100%, 0, 0);
46-
}
47-
48-
to {
49-
opacity: 1;
50-
-webkit-transform: translate3d(0, 0, 0);
51-
transform: translate3d(0, 0, 0);
52-
}
48+
to {
49+
opacity: 1;
50+
-webkit-transform: translate3d(0, 0, 0);
51+
transform: translate3d(0, 0, 0);
5352
}
53+
}
5454

5555
.profile-modal-dialog {
56-
margin: 0;
56+
margin: 0;
57+
}
58+
59+
.card-body {
60+
overflow: auto;
5761
}
5862

5963
.profile-modal-header {
60-
border: none;
61-
background-color:whitesmoke;
62-
}
64+
border: none;
65+
background-color: whitesmoke;
66+
}

app/templates/on_this_day.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="container">
2+
<p class="card-title">On This Day</p>
3+
<ul>
4+
{% for event in on_this_day_data.events[1::-1] %}
5+
<li>
6+
({{event.year}}) {{ event.description}}
7+
<a href="{{event.wikipedia[0].wikipedia }}" target="_blank" >></a>
8+
</li>
9+
{% endfor %}
10+
</ul>
11+
<a href="{{ on_this_day_data.wikipedia }} " target="_blank" >Explore More ...</a>
12+
</div>

app/templates/profile.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,7 @@ <h6 class="card-title text-center mb-1">{{ user.full_name }}</h6>
273273
<!-- Card -->
274274
<div class="card card-profile mb-3" style="height: 18em;">
275275
<div class="card-body">
276-
<p class="card-title">
277-
Your Card
278-
</p>
276+
{% include "on_this_day.html" %}
279277
</div>
280278
</div>
281279
</div>

tests/test_on_this_day_events.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from app.database.models import WikipediaEvents
2+
from app.internal.on_this_day_events import (get_on_this_day_events,
3+
insert_on_this_day_data)
4+
5+
6+
def test_insert_on_this_day_data(session):
7+
is_exists_data = session.query(WikipediaEvents).all()
8+
assert not is_exists_data
9+
insert_on_this_day_data(session)
10+
is_exists_data = session.query(WikipediaEvents).all()
11+
assert is_exists_data is not None
12+
13+
14+
def test_get_on_this_day_events(session):
15+
data = get_on_this_day_events(session)
16+
assert isinstance(data, dict)
17+
assert isinstance(data.get('events'), list)
18+
assert isinstance(data.get('wikipedia'), str)
19+
20+
21+
def test_get_on_this_day_events_exists(session):
22+
fake_object = WikipediaEvents(
23+
events=['fake'], wikipedia="www.fake.com", date_="not a date string")
24+
session.add(fake_object)
25+
session.commit()
26+
fake_data = get_on_this_day_events(session)
27+
assert fake_data.events[0] == 'fake'
28+
assert fake_data.wikipedia == 'www.fake.com'
29+
assert fake_data.date_ == 'not a date string'

tests/test_profile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_profile_page(profile_test_client):
3636
assert b'profile.png' in data
3737
assert b'FakeName' in data
3838
assert b'Happy new user!' in data
39+
assert b'On This Day' in data
3940

4041

4142
def test_update_user_fullname(profile_test_client):

0 commit comments

Comments
 (0)