Skip to content

Commit

Permalink
Add projects/contribute page
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r committed Sep 10, 2024
1 parent 0889ff6 commit b14b391
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 25 deletions.
2 changes: 0 additions & 2 deletions backend/apps/github/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class IssueIndex(AlgoliaIndex):

settings = {
"minProximity": 3,
"attributeForDistinct": "idx_project_name",
"distinct": True,
"indexLanguages": ["en"],
"customRanking": [
"desc(idx_created_at)",
Expand Down
6 changes: 2 additions & 4 deletions backend/apps/owasp/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from apps.github.models import Issue


def search_project(_request):
def search_project(request):
"""Search project view."""
issues_params = {
"attributesToRetrieve": [
Expand All @@ -16,11 +16,9 @@ def search_project(_request):
"idx_title",
"idx_url",
],
# "filters": "idx_labels:issue",
"hitsPerPage": 100,
}

term = "python first issue"
issues = raw_search(Issue, term, issues_params)["hits"]
issues = raw_search(Issue, request.GET.get("q", ""), issues_params)["hits"]

return JsonResponse(issues, safe=False)
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def handle(self, *_args, **options):
subscribers_count.append(repository.subscribers_count)
watchers_count.append(repository.watchers_count)

languages.update(repository.used_languages)
languages.update(repository.top_languages)
if repository.license:
licenses.add(repository.license)
if repository.topics:
Expand Down
79 changes: 78 additions & 1 deletion backend/apps/owasp/templates/search/issues.html
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
{% extends "base.html" %}
{% load static %}

<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>

<script src="{% static 'js/htmx.min.js' %}"></script>
<script src="{% static 'js/markdown-it.min.js' %}"></script>

<h3>
Find a Project
</h3>
<input
autocomplete="off"
class="form-control"
id="query"
name="q"
placeholder="Type To Search..."
type="search"
hx-get="{% url 'api-projects-contribute' %}"
hx-indicator=".htmx-indicator"
hx-swap="none"
hx-target="#search-results"
hx-trigger="input changed delay:1000ms, search"
/>
<span class="htmx-indicator"> Searching... </span>

<script>
document
.getElementById('query')
.addEventListener('htmx:afterRequest', function (event) {
var jsonResponse = event.detail.xhr.response;
var hits = JSON.parse(jsonResponse);

const resultsContainer = document.getElementById('search-results');
resultsContainer.innerHTML = '';
const md = window.markdownit();

hits.forEach((hit) => {
const highlightedTitle = hit._highlightResult.idx_title.value;
const languages = hit.idx_repository_languages;
const projectName = hit.idx_project_name;
const createdAt = new Date(hit.idx_created_at * 1000);

const languageIcons = {
Python: 'fab fa-python',
JavaScript: 'fab fa-js',
Java: 'fab fa-java',
PHP: 'fab fa-php',
};

const container = document.createElement('div');
languages.forEach((language) => {
const iconClass = languageIcons[language];
if (iconClass) {
const languageSpan = document.createElement('span');
languageSpan.innerHTML = `<i class="${iconClass}"></i>`;
container.appendChild(languageSpan);
}
});

const url = hit.idx_url;

const resultItem = document.createElement('div');
resultItem.innerHTML = `
<h2><a href="${url}" target="_blank">${highlightedTitle}</a></h2>
<p>Project: ${projectName}. Created at: ${createdAt}</p>
<p>${container.innerHTML}</p>
<p></p>
`;

resultsContainer.appendChild(resultItem);
});
});
</script>

<div id="search-results"></div>
1 change: 0 additions & 1 deletion backend/apps/owasp/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from apps.owasp.views.home import home_page
from apps.owasp.views.search import search_issues
13 changes: 0 additions & 13 deletions backend/apps/owasp/views/search.py

This file was deleted.

3 changes: 3 additions & 0 deletions backend/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class Base(Configuration):

USE_TZ = True

STATICFILES_DIRS = [
BASE_DIR / "static",
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = "static/"
Expand Down
11 changes: 8 additions & 3 deletions backend/settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView
from rest_framework import routers

from apps.github.api.urls import router as github_router
from apps.owasp.api import search_project
from apps.owasp.api.urls import router as owasp_router
from apps.owasp.views import home_page, search_issues
from apps.owasp.views import home_page

router = routers.DefaultRouter()
router.registry.extend(github_router.registry)
router.registry.extend(owasp_router.registry)

urlpatterns = [
path("api/v1/", include(router.urls)),
path("api/v1/search/", search_project),
path("projects/contribute", search_issues),
path("api/v1/search/", search_project, name="api-projects-contribute"),
path(
"projects/contribute",
TemplateView.as_view(template_name="search/issues.html"),
name="projects-contribute",
),
path("", home_page),
path("a/", admin.site.urls),
]
Expand Down
6 changes: 6 additions & 0 deletions backend/static/css/font-awesome.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/static/js/htmx.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions backend/static/js/marked.min.js

Large diffs are not rendered by default.

0 comments on commit b14b391

Please sign in to comment.