Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
krrish-sehgal committed Nov 30, 2024
1 parent d4af40e commit 6364c3a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
18 changes: 18 additions & 0 deletions website/migrations/0161_alter_badge_icon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-11-30 14:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("website", "0160_merge_20241129_0712"),
]

operations = [
migrations.AlterField(
model_name="badge",
name="icon",
field=models.ImageField(blank=True, null=True, upload_to="badges/"),
),
]
37 changes: 37 additions & 0 deletions website/migrations/0162_add_new_badges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 5.1.3 on 2024-11-30 14:15

from django.db import migrations


def add_new_badges(apps, schema_editor):
# Get the Badge model
Badge = apps.get_model("website", "Badge")

# Define the new badges to add
new_badges = [
{"title": "First IP Reported", "description": "Awarded for reporting the first intellectual property.", "type": "automatic"},
{"title": "First Bid Placed", "description": "Awarded for placing the first bid.", "type": "manual"},
{"title": "First Bug Bounty", "description": "Awarded for earning the first bug bounty.", "type": "manual"},
{"title": "First Suggestion", "description": "Awarded for making the first suggestion.", "type": "manual"},
]

# Loop through the new badges and create them if they don't already exist
for badge in new_badges:
Badge.objects.get_or_create(
title=badge["title"],
defaults={
"description": badge["description"],
"type": badge["type"],
},
)


class Migration(migrations.Migration):

dependencies = [
("website", "0161_alter_badge_icon"), # Adjust based on your actual previous migration
]

operations = [
migrations.RunPython(add_new_badges),
]
40 changes: 36 additions & 4 deletions website/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@

from blog.models import Post

from .models import Activity, Hunt, IpReport, Issue
from .models import Activity, Hunt, IpReport, Issue, UserBadge, Badge, Suggestion, Bid


def get_default_user():
"""Get or create a default 'anonymous' user."""
return User.objects.get_or_create(username="anonymous")[0]

# Helper function to assign the badge based on action
def assign_first_action_badge(user, action_title):
"""Assign badges for first-time actions."""
if user.is_authenticated:
badge, created = Badge.objects.get_or_create(title=action_title, type="automatic")

if not UserBadge.objects.filter(user=user, badge=badge).exists():
UserBadge.objects.get_or_create(user=user, badge=badge)
print(f"Assigned '{action_title}' badge to {user.username}")

def create_activity(instance, action_type):
"""Generic function to create an activity for a given model instance."""
Expand Down Expand Up @@ -41,9 +50,32 @@ def create_activity(instance, action_type):
@receiver(post_save)
def handle_post_save(sender, instance, created, **kwargs):
"""Generic handler for post_save signal."""
if sender in [Issue, Hunt, IpReport, Post]: # Add any model you want to track
if created:
create_activity(instance, "created")
if sender == IpReport and created: # Track first IP report
assign_first_action_badge(instance.user, "First IP Reported")
create_activity(instance, "created")

elif sender == Post and created: # Track first blog post
assign_first_action_badge(instance.user, "First Blog Posted")
create_activity(instance, "created")

elif sender == Issue and created: # Track first bug report
assign_first_action_badge(instance.user, "First Bug Reported")
create_activity(instance, "created")

elif sender == Hunt and created: # Track first bid placed
assign_first_action_badge(instance.user, "First Bug Bounty")
create_activity(instance, "created")

elif sender == Suggestion and created: # Track first suggestion
assign_first_action_badge(instance.user, "First Suggestion")
create_activity(instance, "suggested")


elif sender == Bid and created: # Track first bid placed
assign_first_action_badge(instance.user, "First Bid Placed")
create_activity(instance, "placed")


elif sender is User and created: # Handle user sign-up
Activity.objects.create(
user=instance,
Expand Down

0 comments on commit 6364c3a

Please sign in to comment.