Skip to content

Commit

Permalink
Make a Form for admins to accept Bids #2093 (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarthak5598 authored May 21, 2024
1 parent ad27b50 commit 73b60bb
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
6 changes: 6 additions & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
UserDeleteView,
UserProfileDetailsView,
UserProfileDetailView,
change_bid_status,
contributors_view,
deletions,
dislike_issue2,
Expand All @@ -80,10 +81,12 @@
flag_issue2,
flag_issue3,
generate_bid_image,
get_unique_issues,
github_callback,
google_callback,
like_issue2,
like_issue3,
select_bid,
submit_pr,
subscribe_to_domains,
vote_count,
Expand Down Expand Up @@ -495,6 +498,9 @@
path("trademarks/", website.views.trademark_search, name="trademark_search"),
path("generate_bid_image/<int:bid_amount>/", generate_bid_image, name="generate_bid_image"),
path("bidding/", SaveBiddingData, name="BiddingData"),
path("select_bid/", select_bid, name="select_bid"),
path("get_unique_issues/", get_unique_issues, name="get_unique_issues"),
path("change_bid_status/", change_bid_status, name="change_bid_status"),
path("fetch-current-bid/", fetch_current_bid, name="fetch_current_bid"),
path("Submitpr/", submit_pr, name="submit_pr"),
re_path(
Expand Down
151 changes: 151 additions & 0 deletions website/templates/bid_selection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{% extends "base.html" %}
{% block content %}
{% include "includes/sidenav.html" %}
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}

#bid {
max-width: 400px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

#issueLinkInput {
width: 100%;
padding: 8px;
margin-bottom: 10px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 3px;
}

#fetchBids {
padding: 8px 15px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
display: block;
margin: 0 auto;
}

#BidDisplay {
width: 400px;
margin: 0 auto;
border-radius: 5px;
background-color: #e53e3e;
color: #000;
padding: 10px;
text-align: center;
}

#BidDisplay button {
padding: 8px 15px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
</style>
<script>
function fetchBid(event) {
const issueUrl = document.getElementById("issueLinkInput").value;
const csrftoken = getCookie('csrftoken');
fetch(`/get_unique_issues/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken,
},
body: JSON.stringify({ issue_url: issueUrl })
})
.then(response => response.json())
.then(data => {
const bidContainer = document.getElementById("BidDisplay");
bidContainer.innerHTML = '';
if (data.success === false) {
alert(data.error);
} else {
data.forEach(bid => {
console.log('Data fetched:', bid.user);
const bidElement = document.createElement("div");
bidElement.innerHTML = `
<div>
<p>User: ${bid.user}</p>
<p>Current Bid: ${bid.amount}</p>
<p>Issue Url: ${bid.issue_url}</p>
<p>Created on: ${bid.created}</p>
<p>Status: ${bid.status}</p>
<button onclick="changeStatus(${bid.id})">Change Status</button>
</div>
`;
bidContainer.appendChild(bidElement);
});
}
})
.catch(error => console.error('Error fetching bids:', error));
}

function changeStatus(bidId) {
const csrftoken = getCookie('csrftoken');
fetch(`/change_bid_status/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken,
},
body: JSON.stringify({ id: bidId })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Status changed successfully');
fetchBid();
} else {
alert(data.error);
}
})
.catch(error => console.error('Error changing status:', error));
}

function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
</script>
<div>
<form id="bid" method="post">
{% csrf_token %}
<label>Paste Url to fetch bids</label>
<input type="text" id="issueLinkInput" name="issueLinkInput">
<button type="button" id="fetchBids" onclick="fetchBid()">Fetch</button>
</form>
<div id="BidDisplay"></div>
</div>
{% endblock content %}
2 changes: 1 addition & 1 deletion website/templates/includes/sidenav.html
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
<a href="{% url 'BiddingData' %}"
class="flex items-center w-full text-black no-underline p-2">
<div class="w-8 mr-4">
<i class="fa fa-bar-chart"></i>
<i class="fas fa-hand-holding-usd"></i>
</div>
<span>Place a Bid</span>
</a>
Expand Down
1 change: 1 addition & 0 deletions website/templates/submit_pr.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
display: none;
}


#issue_link, #bid_amount {
width: 100%;
padding: 8px;
Expand Down
33 changes: 33 additions & 0 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4417,6 +4417,39 @@ def generate_bid_image(request, bid_amount):
return HttpResponse(byte_io, content_type="image/png")


def change_bid_status(request):
if request.method == "POST":
try:
data = json.loads(request.body)
bid_id = data.get("id")
bid = Bid.objects.get(id=bid_id)
bid.status = "Selected"
bid.save()
return JsonResponse({"success": True})
except Bid.DoesNotExist:
return JsonResponse({"success": False, "error": "Bid not found"})
return HttpResponse(status=405)


def get_unique_issues(request):
if request.method == "POST":
try:
data = json.loads(request.body)
issue_url = data.get("issue_url")
if not issue_url:
return JsonResponse({"success": False, "error": "issue_url not provided"})

all_bids = Bid.objects.filter(issue_url=issue_url).values()
return JsonResponse(list(all_bids), safe=False)
except json.JSONDecodeError:
return JsonResponse({"success": False, "error": "Invalid JSON"})
return HttpResponse(status=405)


def select_bid(request):
return render(request, "bid_selection.html")


@login_required
def SaveBiddingData(request):
if request.method == "POST":
Expand Down

0 comments on commit 73b60bb

Please sign in to comment.