From 40b0d4a88226362af7cdb0e7907f1369d35ce146 Mon Sep 17 00:00:00 2001 From: SahilDhillon21 Date: Sun, 12 Jan 2025 00:06:40 +0530 Subject: [PATCH] all changes --- .../0181_suggestion_organization.py | 23 +++++++ website/models.py | 1 + website/templates/feature_suggestion.html | 12 ++++ website/views/core.py | 65 ++++++++++++++++++- 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 website/migrations/0181_suggestion_organization.py diff --git a/website/migrations/0181_suggestion_organization.py b/website/migrations/0181_suggestion_organization.py new file mode 100644 index 000000000..976131cd0 --- /dev/null +++ b/website/migrations/0181_suggestion_organization.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.4 on 2025-01-11 18:20 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("website", "0180_rename_project_visit_count_repo_repo_visit_count"), + ] + + operations = [ + migrations.AddField( + model_name="suggestion", + name="organization", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="website.organization", + ), + ), + ] diff --git a/website/models.py b/website/models.py index 9172cabc6..3d5e22476 100644 --- a/website/models.py +++ b/website/models.py @@ -867,6 +867,7 @@ class Suggestion(models.Model): up_votes = models.IntegerField(null=True, blank=True, default=0) down_votes = models.IntegerField(null=True, blank=True, default=0) created = models.DateTimeField(auto_now_add=True) + organization = models.ForeignKey(Organization, null=True, blank=True, on_delete=models.CASCADE) def __str__(self): return f"{self.title} by {self.user}" diff --git a/website/templates/feature_suggestion.html b/website/templates/feature_suggestion.html index 28cfa29ea..938331ab1 100644 --- a/website/templates/feature_suggestion.html +++ b/website/templates/feature_suggestion.html @@ -126,6 +126,11 @@

User:{{ suggestion.user }}

+ {% if suggestion.organization %} +

+ Organization:{{ suggestion.organization.name }} +

+ {% endif %}

name="description" rows="4" placeholder="Description of your suggestion"> + +
diff --git a/website/views/core.py b/website/views/core.py index 02f8db300..3fe4ae515 100644 --- a/website/views/core.py +++ b/website/views/core.py @@ -37,6 +37,7 @@ Badge, Domain, Issue, + Organization, PRAnalysisReport, Suggestion, SuggestionVotes, @@ -521,6 +522,17 @@ def set_vote_status(request): return JsonResponse({"success": False, "error": "Invalid request method"}, status=400) +import json +import os + +import requests +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse +from django.shortcuts import render +from giturlparse import parse + + @login_required def add_suggestions(request): if request.method == "POST": @@ -528,15 +540,61 @@ def add_suggestions(request): data = json.loads(request.body) title = data.get("title") description = data.get("description", "") + organization_id = data.get("organization") if title and description and user: suggestion = Suggestion(user=user, title=title, description=description) + if organization_id: + try: + organization = Organization.objects.get(id=organization_id) + except Organization.DoesNotExist: + organization = None + else: + organization = None + suggestion = Suggestion( + user=user, title=title, description=description, organization=organization + ) suggestion.save() messages.success(request, "Suggestion added successfully.") + + # GitHub issue creation + github_repo_url = os.environ.get("GITHUB_REPO_URL") + github_token = os.environ.get("GITHUB_ACCESS_TOKEN") + + github_url = github_repo_url.replace("https", "git").replace("http", "git") + ".git" + p = parse(github_url) + + url = "https://api.github.com/repos/%s/%s/issues" % (p.owner, p.repo) + + organization_name = organization.name if organization else "" + organization_text = f" for company: {organization_name}" if organization else "" + suggestion = { + "title": title, + "body": description + "\n\n" + " Suggested by " + str(user) + organization_text, + "milestone": 42, + } + + if github_repo_url and github_token: + response = requests.post( + url, json.dumps(suggestion), headers={"Authorization": "token " + github_token} + ) + + if response.status_code == 201: + messages.success( + request, "Suggestion added successfully and GitHub issue created." + ) + else: + messages.warning(request, "Suggestion added but failed to create GitHub issue.") + else: + messages.warning(request, "Suggestion added but GitHub settings are missing.") + return JsonResponse({"status": "success"}) else: messages.error(request, "Please fill all the fields.") return JsonResponse({"status": "error"}, status=400) + organizations = Organization.objects.all() + return render(request, "suggestions/add_suggestion.html", {"organizations": organizations}) + class GoogleLogin(SocialLoginView): adapter_class = GoogleOAuth2Adapter @@ -720,7 +778,12 @@ def get_context_data(self, *args, **kwargs): def view_suggestions(request): suggestion = Suggestion.objects.all() - return render(request, "feature_suggestion.html", {"suggestions": suggestion}) + organizations = Organization.objects.all() + return render( + request, + "feature_suggestion.html", + {"suggestions": suggestion, "organizations": organizations}, + ) def sitemap(request):