Skip to content

Commit

Permalink
added images
Browse files Browse the repository at this point in the history
  • Loading branch information
tsu-ki committed Jan 31, 2025
1 parent fb0a648 commit c40c921
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 20 deletions.
47 changes: 47 additions & 0 deletions website/migrations/0165_add_badge_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@

def add_badge_icons(apps, schema_editor):
Badge = apps.get_model("website", "Badge")

pr_badges = [
{
"title": "1 Merged Pull Request",
"description": "Awarded for merging your first pull request",
"icon": "badges/1-pr.png",
},
{
"title": "10 Merged Pull Requests",
"description": "Awarded for merging 10 pull requests",
"icon": "badges/10-pr.png",
},
{
"title": "25 Merged Pull Requests",
"description": "Awarded for merging 25 pull requests",
"icon": "badges/25-pr.png",
},
{
"title": "50 Merged Pull Requests",
"description": "Awarded for merging 50 pull requests",
"icon": "badges/50-pr.png",
},
{
"title": "100 Merged Pull Requests",
"description": "Awarded for merging 100 pull requests",
"icon": "badges/100-pr.png",
},
]

# Create PR milestone badges
for badge_data in pr_badges:
badge = Badge.objects.filter(title=badge_data["title"]).first()
if not badge:
badge = Badge.objects.create(
title=badge_data["title"], description=badge_data["description"], type="automatic"
)

# Handle icon assignment
static_icon_path = os.path.join("website", "static", "img", badge_data["icon"])
if os.path.exists(static_icon_path):
media_icon_path = os.path.join(settings.MEDIA_ROOT, "badges", os.path.basename(static_icon_path))
os.makedirs(os.path.dirname(media_icon_path), exist_ok=True)
shutil.copy(static_icon_path, media_icon_path)

with open(media_icon_path, "rb") as f:
badge.icon.save(os.path.basename(media_icon_path), File(f), save=True)

new_badges = [
{"title": "Mentor", "icon": "badges/icons8-mentor-94.png"},
{"title": "First Pull Request Merged", "icon": "badges/icons8-merge-40.png"},
Expand Down
43 changes: 24 additions & 19 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def update_streak_and_award_points(self, check_in_date=None):
return False

return True

def award_streak_badges(self):
"""
Award badges for streak milestones
Expand All @@ -740,29 +740,34 @@ def award_streak_badges(self):
def __str__(self):
return self.user.username

def check_merged_pr_badges(self):
thresholds = [
(1, ("1 Merged Pull Request", "1-pr.png")),
(10, ("10 Merged Pull Requests", "10-pr.png")),
(25, ("25 Merged Pull Requests", "25-pr.png")),
(50, ("50 Merged Pull Requests", "50-pr.png")),
(100, ("100 Merged Pull Requests", "100-pr.png")),
def check_merged_pr_badges(self):
"""
Check and award badges based on merged PR thresholds
"""
badge_thresholds = [
(1, "1 Merged Pull Request"),
(10, "10 Merged Pull Requests"),
(25, "25 Merged Pull Requests"),
(50, "50 Merged Pull Requests"),
(100, "100 Merged Pull Requests"),
]
for limit, (badge_name, icon_file) in thresholds:
if self.merged_pr_count >= limit:
badge, created = Badge.objects.get_or_create(
title=badge_name,
type="automatic",

for threshold, badge_title in badge_thresholds:
if self.merged_pr_count >= threshold:
badge, _ = Badge.objects.get_or_create(
title=badge_title,
defaults={
"description": f"Awarded for merging {limit} pull requests.",
"icon": f"badges/{icon_file}",
"description": f"Badge for merging {threshold} pull requests",
"type": "automatic",
},
)
if not UserBadge.objects.filter(user=self.user, badge=badge).exists():
UserBadge.objects.create(user=self.user, badge=badge)
print(f"Awarded badge '{badge_name}' to {self.user.username}")

UserBadge.objects.get_or_create(
user=self.user, badge=badge, defaults={"reason": f"Awarded for merging {threshold} pull requests"}
)

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.check_merged_pr_badges()


def create_profile(sender, **kwargs):
Expand Down
Binary file added website/static/img/badges/1-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/static/img/badges/10-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/static/img/badges/100-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/static/img/badges/25-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/static/img/badges/50-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 42 additions & 1 deletion website/templates/badges.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,25 @@
<div class="container mx-auto px-4 py-8">
<h1 class="text-4xl font-bold mb-12 mt-6 text-center">Badges</h1>
<hr>
<!-- Badge Categories -->
<div class="mb-8">
<div class="inline-flex rounded-md shadow-sm" role="group">
<button type="button"
class="filter-btn active px-4 py-2 text-sm font-medium bg-white border border-gray-200 rounded-l-lg hover:bg-gray-100 focus:z-10 focus:ring-2"
data-category="all">All Badges</button>
<button type="button"
class="filter-btn px-4 py-2 text-sm font-medium bg-white border border-gray-200 hover:bg-gray-100 focus:z-10 focus:ring-2"
data-category="pr">Pull Request Badges</button>
<button type="button"
class="filter-btn px-4 py-2 text-sm font-medium bg-white border border-gray-200 rounded-r-lg hover:bg-gray-100 focus:z-10 focus:ring-2"
data-category="streak">Streak Badges</button>
</div>
</div>
<!-- Badge Cards -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-8">
{% for badge in badges %}
<div class="bg-white border-2 border-gray-100 rounded-2xl shadow-2xl overflow-hidden transform transition-all duration-100 hover:shadow-4xl hover:scale-102 hover:-translate-y-2 relative">
<div class="badge-card bg-white border-2 border-gray-100 rounded-2xl shadow-2xl overflow-hidden transform transition-all duration-100 hover:shadow-4xl hover:scale-102 hover:-translate-y-2 relative"
data-category="{% if 'Pull Request' in badge.title %}pr{% elif 'Streak' in badge.title %}streak{% else %}other{% endif %}">
<a href="{% url 'badge_user_list' badge_id=badge.id %}"
class="absolute top-0 left-0 w-full z-10">
<div class="absolute top-0 left-0 w-full h-2 bg-purple-600"></div>
Expand Down Expand Up @@ -68,4 +83,30 @@ <h2 class="text-3xl font-['Playfair_Display'] font-bold text-center mb-4 text-gr
{% endfor %}
</div>
</div>
<script>
// Filter functionality
document.addEventListener('DOMContentLoaded', function() {
const filterButtons = document.querySelectorAll('.filter-btn');
const badges = document.querySelectorAll('.badge-card');

filterButtons.forEach(button => {
button.addEventListener('click', () => {
// Remove active class from all buttons
filterButtons.forEach(btn => btn.classList.remove('active'));
// Add active class to clicked button
button.classList.add('active');

const category = button.dataset.category;

badges.forEach(badge => {
if (category === 'all' || badge.dataset.category === category) {
badge.style.display = 'block';
} else {
badge.style.display = 'none';
}
});
});
});
});
</script>
{% endblock content %}

0 comments on commit c40c921

Please sign in to comment.