diff --git a/website/migrations/0161_alter_badge_icon.py b/website/migrations/0161_alter_badge_icon.py new file mode 100644 index 000000000..706c1b22c --- /dev/null +++ b/website/migrations/0161_alter_badge_icon.py @@ -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/"), + ), + ] diff --git a/website/migrations/0162_add_new_badges.py b/website/migrations/0162_add_new_badges.py new file mode 100644 index 000000000..46d53026c --- /dev/null +++ b/website/migrations/0162_add_new_badges.py @@ -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), + ] diff --git a/website/signals.py b/website/signals.py index b397987c6..35fb8064a 100644 --- a/website/signals.py +++ b/website/signals.py @@ -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.""" @@ -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,