Skip to content

Commit d106260

Browse files
authored
Merge branch 'main' into otn_softdel
2 parents d52f21c + d038076 commit d106260

File tree

4 files changed

+522
-35
lines changed

4 files changed

+522
-35
lines changed

pydis_site/apps/home/views/home.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class HomeView(View):
1919

2020
github_api = "https://api.github.com/users/python-discord/repos?per_page=100"
2121
repository_cache_ttl = 3600
22-
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
2322

2423
# Which of our GitHub repos should be displayed on the front page, and in which order?
2524
repos = [
@@ -35,6 +34,16 @@ def __init__(self):
3534
"""Clean up stale RepositoryMetadata."""
3635
RepositoryMetadata.objects.exclude(repo_name__in=self.repos).delete()
3736

37+
# If no token is defined (for example in local development), then
38+
# it does not make sense to pass the Authorization header. More
39+
# specifically, GitHub will reject any requests from us due to the
40+
# invalid header. We can make a limited number of anonymous requests
41+
# though, which is useful for testing.
42+
if GITHUB_TOKEN:
43+
self.headers = {"Authorization": f"token {GITHUB_TOKEN}"}
44+
else:
45+
self.headers = {}
46+
3847
def _get_api_data(self) -> Dict[str, Dict[str, str]]:
3948
"""
4049
Call the GitHub API and get information about our repos.
@@ -74,58 +83,52 @@ def _get_api_data(self) -> Dict[str, Dict[str, str]]:
7483

7584
def _get_repo_data(self) -> List[RepositoryMetadata]:
7685
"""Build a list of RepositoryMetadata objects that we can use to populate the front page."""
77-
database_repositories = []
78-
79-
# First, let's see if we have any metadata cached.
80-
cached_data = RepositoryMetadata.objects.all()
86+
# First off, load the timestamp of the least recently updated entry.
87+
last_update = (
88+
RepositoryMetadata.objects.values_list("last_updated", flat=True)
89+
.order_by("last_updated").first()
90+
)
8191

82-
# If we don't, we have to create some!
83-
if not cached_data:
92+
# If we did not retrieve any results here, we should import them!
93+
if last_update is None:
8494

8595
# Try to get new data from the API. If it fails, we'll return an empty list.
8696
# In this case, we simply don't display our projects on the site.
8797
api_repositories = self._get_api_data()
8898

8999
# Create all the repodata records in the database.
90-
for api_data in api_repositories.values():
91-
repo_data = RepositoryMetadata(
100+
return RepositoryMetadata.objects.bulk_create(
101+
RepositoryMetadata(
92102
repo_name=api_data["full_name"],
93103
description=api_data["description"],
94104
forks=api_data["forks_count"],
95105
stargazers=api_data["stargazers_count"],
96106
language=api_data["language"],
97107
)
98-
99-
repo_data.save()
100-
database_repositories.append(repo_data)
101-
102-
return database_repositories
108+
for api_data in api_repositories.values()
109+
)
103110

104111
# If the data is stale, we should refresh it.
105-
if (timezone.now() - cached_data[0].last_updated).seconds > self.repository_cache_ttl:
112+
if (timezone.now() - last_update).seconds > self.repository_cache_ttl:
106113
# Try to get new data from the API. If it fails, return the cached data.
107114
api_repositories = self._get_api_data()
108115

109116
if not api_repositories:
110117
return RepositoryMetadata.objects.all()
111118

112119
# Update or create all RepoData objects in self.repos
113-
for repo_name, api_data in api_repositories.items():
114-
try:
115-
repo_data = RepositoryMetadata.objects.get(repo_name=repo_name)
116-
repo_data.description = api_data["description"]
117-
repo_data.language = api_data["language"]
118-
repo_data.forks = api_data["forks_count"]
119-
repo_data.stargazers = api_data["stargazers_count"]
120-
except RepositoryMetadata.DoesNotExist:
121-
repo_data = RepositoryMetadata(
122-
repo_name=api_data["full_name"],
123-
description=api_data["description"],
124-
forks=api_data["forks_count"],
125-
stargazers=api_data["stargazers_count"],
126-
language=api_data["language"],
127-
)
128-
repo_data.save()
120+
database_repositories = []
121+
for api_data in api_repositories.values():
122+
repo_data, _created = RepositoryMetadata.objects.update_or_create(
123+
repo_name=api_data["full_name"],
124+
defaults={
125+
'repo_name': api_data["full_name"],
126+
'description': api_data["description"],
127+
'forks': api_data["forks_count"],
128+
'stargazers': api_data["stargazers_count"],
129+
'language': api_data["language"],
130+
}
131+
)
129132
database_repositories.append(repo_data)
130133
return database_repositories
131134

pydis_site/templates/events/pages/code-jams/8/_index.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ <h3 if="qualifier"><a href="#qualifier">The Qualifier</a></h3>
5252
<li>The Qualifier must be submitted through the Code Jam sign-up form.</li>
5353
</ul>
5454
</p>
55-
<h3 id="how-to-join"><a href="#how-to-join">How to Join</a></h3>
55+
<h3 id="submissions"><a href="#submissions">Submissions</a></h3>
5656
<p>
57-
To enter into the code jam you must complete <a href="#qualifier">The Qualifier</a> and submit the sign-up form.
58-
Don't forget to join us on Discord at <a href="https://discord.gg/python">discord.gg/python</a>!
59-
<div class="has-text-centered"><a class="button is-link" href="https://form.jotform.com/211714357615050" target="_blank">Sign up for the Code Jam</a></div>
57+
63 teams started out on July 9th 2021. By the end of the jam, 51 teams made project submissions. Check them all out here:
58+
<div class="has-text-centered"><a class="button is-link" href="submissions">View Submissions</a></div>
6059
</p>
6160
<h3 id="prizes"><a href="#prizes">Prizes</a></h3>
6261
<p>

0 commit comments

Comments
 (0)