Skip to content

Commit

Permalink
all changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilDhillon21 committed Jan 11, 2025
1 parent f7151c0 commit 40b0d4a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
23 changes: 23 additions & 0 deletions website/migrations/0181_suggestion_organization.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
1 change: 1 addition & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
12 changes: 12 additions & 0 deletions website/templates/feature_suggestion.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ <h1>
<p>
<strong>User:</strong>{{ suggestion.user }}
</p>
{% if suggestion.organization %}
<p>
<strong>Organization:</strong>{{ suggestion.organization.name }}
</p>
{% endif %}
<p hidden id="suggestion-id---{{ suggestion.suggestion_id }}">{{ suggestion.suggestion_id }}</p>
<div class="suggestion-vote">
<button type="button"
Expand All @@ -152,6 +157,13 @@ <h2 id="semiheading">Suggest us Features</h2>
name="description"
rows="4"
placeholder="Description of your suggestion"></textarea>
<label>Organization</label>
<select id="organization" name="organization">
<option value="" selected>-- None --</option>
{% for organization in organizations %}
<option value="{{ organization.id }}">{{ organization.name }}</option>
{% endfor %}
</select>
<br>
<button type="submit" onclick="Savefeature()">Post Suggestion</button>
</form>
Expand Down
65 changes: 64 additions & 1 deletion website/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Badge,
Domain,
Issue,
Organization,
PRAnalysisReport,
Suggestion,
SuggestionVotes,
Expand Down Expand Up @@ -521,22 +522,79 @@ 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":
user = request.user
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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 40b0d4a

Please sign in to comment.