Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sort functionality (wikimedia hackathon contribution) #14

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ More at: https://phabricator.wikimedia.org/T341379

1. Clone the repository: `git clone https://github.com/gopavasanth/ToolWatch`
2. Navigate to the project directory: `cd ToolWatch`
3. Create a virtual environment: `python -m venv venv`
3. Create a virtual environment: `python3 -m venv venv`
4. Activate the virtual environment:
- For Windows: `venv\Scripts\activate`
- For Unix/Linux: `source venv/bin/activate`
Expand Down
35 changes: 34 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,40 @@ def index():
was_crawled.append(True)
else:
was_crawled.append(False)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page,total_pages=tools[0].total_pages)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page,total_pages=tools[0].total_pages, route='index')

@app.route("/sortT")
def sortT():
session = Session()
curr_page = int(request.args.get("page",1))
tools = session.query(Tool).order_by(Tool.title).all()
paginated_tools=tools[(curr_page-1)*page_limit:curr_page*page_limit]

was_crawled = []
for tool in paginated_tools:
url_parsed = urlparse(tool.url)
if url_parsed.hostname != None and 'toolforge.org' in url_parsed.hostname :
Copy link
Owner

@gopavasanth gopavasanth May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modularise code, something similar to:

def get_paginated_tools(session, order_by, curr_page):
tools = session.query(Tool).order_by(order_by).all()
total_pages = (len(tools) + page_limit - 1) // page_limit
paginated_tools = tools[(curr_page - 1) * page_limit:curr_page * page_limit]
was_crawled = [urlparse(tool.url).hostname and 'toolforge.org' in urlparse(tool.url).hostname for tool in paginated_tools]
return paginated_tools, was_crawled, total_pages

@app.route("/sortT")
def sortT():
session = Session()
curr_page = int(request.args.get("page", 1))
paginated_tools, was_crawled, total_pages = get_paginated_tools(session, Tool.title, curr_page)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page, total_pages=total_pages, route='sortT')

@app.route("/sortA")
def sortA():
session = Session()
curr_page = int(request.args.get("page", 1))
paginated_tools, was_crawled, total_pages = get_paginated_tools(session, Tool.author, curr_page)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page, total_pages=total_pages, route='sortA')

was_crawled.append(True)
else:
was_crawled.append(False)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page,total_pages=tools[0].total_pages, route='sortT')

@app.route("/sortA")
def sortA():
session = Session()
curr_page = int(request.args.get("page",1))
tools = session.query(Tool).order_by(Tool.author).all()
paginated_tools=tools[(curr_page-1)*page_limit:curr_page*page_limit]

was_crawled = []
for tool in paginated_tools:
url_parsed = urlparse(tool.url)
if url_parsed.hostname != None and 'toolforge.org' in url_parsed.hostname :
was_crawled.append(True)
else:
was_crawled.append(False)
return render_template('index.html', tools=paginated_tools, was_crawled=was_crawled, curr_page=curr_page,total_pages=tools[0].total_pages, route='sortA')


@app.route('/search')
def search():
Expand Down
329 changes: 145 additions & 184 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,201 +1,162 @@
<!DOCTYPE html>
<html>
<head>
<title>ToolWatch</title>
<link
rel="stylesheet"
href="{{ url_for('static', filename='style.css') }}"
/>
<!-- Add Bootstrap CSS link -->
<link
rel="stylesheet"
href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.1/css/bootstrap.min.css"
/>
</head>
<script>
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setUTCHours(0, 0, 0, 0);
const tt = tomorrow.getTime();
// Update the count down every 1 second
let x = setInterval(function () {
// Get today's date and time
const now = new Date().getTime();
<head>
<title>ToolWatch</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
<!-- Add Bootstrap CSS link -->
<link rel="stylesheet"
href="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.1/css/bootstrap.min.css" />
</head>
<script>
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setUTCHours(0, 0, 0, 0);
const tt = tomorrow.getTime();
// Update the count down every 1 second
let x = setInterval(function () {
// Get today's date and time
const now = new Date().getTime();

// Find the distance between now and the count down date
const distance = tt - now;
// Find the distance between now and the count down date
const distance = tt - now;

// Time calculations for hours, minutes and seconds
const hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Time calculations for hours, minutes and seconds
const hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Display the result in the element with id="demo"
document.querySelector(".header-timer-space").innerText =
hours + "h " + minutes + "m " + seconds + "s ";
// Display the result in the element with id="demo"
document.querySelector(".header-timer-space").innerText =
hours + "h " + minutes + "m " + seconds + "s ";

if (distance < 0) {
clearInterval(x);
tt = tomorrow.setDate(tomorrow.getDate() + 1);
}
}, 1000);
</script>
<body>
<nav
class="flex navbar navbar-expand-lg navbar-dark bg-dark"
style="width: 100%"
>
<div
class="container"
style="font-family: 'Courier New', Courier, monospace"
>
<a class="navbar-brand" href="/">
<div class="tool-title">ToolWatch</div></a
>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<div class="nav-link active">
<!-- check if last_checked is present in any one of the tool -->
Last crawled on {% if tools and tools[0] and
tools[0].last_checked %}
<span>{{tools[0].last_checked}}</span>
{% else %}
<span>-</span>
{% endif %}
</div>
</li>
if (distance < 0) {
clearInterval(x);
tt = tomorrow.setDate(tomorrow.getDate() + 1);
}
}, 1000);
</script>

<!-- Add more navigation items as needed -->
</ul>
</div>
</div>
</nav>
<div
style="position: sticky; top: 5px; background-color: white; z-index: 1"
>
<form
action="{{ url_for('search') }}"
method="GET"
style="display: flex; padding: 15px; justify-content: center"
>
<div class="form-outline" style="width: 75vw">
<input
type="search"
id="search"
name="search"
class="wiki-search-input"
placeholder="Search by Author, URL or Title"
`{%
if
search_term
%}
value="{{ search_term }}"
{%
endif
%}`
/>
</div>
<button type="submit" class="wiki-submit-btn"><b>Search</b></button>
</form>
</div>
<div class="container mt-4">
<table class="table table-striped" style="table-layout: fixed">
<thead>
<tr>
<th>Tool</th>
<th>Description</th>
<th>Author</th>
<th
data-toggle="tooltip"
data-placement="top"
title="Whether the tool is available or not"
>
Health
</th>
</tr>
</thead>
<tbody>
{% for tool in tools %}
<tr>
<td>
<a
href="{{ tool.url }}"
target="_blank"
style="word-wrap: break-word"
>
{{ tool.title }}
</a>
</td>
<td
data-toggle="tooltip"
data-placement="top"
title="{{tool.description}}"
>
{{tool.description[:50]}}...
</td>
<td>{{ tool.author }}</td>
<td>
{% if tool.health_status == True %}
<span class="badge bg-success">Available</span>
<body>
<nav class="flex navbar navbar-expand-lg navbar-dark bg-dark" style="width: 100%">
<div class="container" style="font-family: 'Courier New', Courier, monospace;">
<a class="navbar-brand" href="/">
<div class="tool-title">ToolWatch</div>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<div class="nav-link active">
<!-- check if last_checked is present in any one of the tool -->
Last crawled on {% if tools and tools[0] and
tools[0].last_checked %}
<span>{{tools[0].last_checked}}</span>
{% else %}
<span class="badge bg-danger">Unavailable</span>
<span>-</span>
{% endif %}
</td>
<!-- <td>{{ tool.last_checked }}</td> -->
</tr>
{% endfor %}
</tbody>
</table>
<div class="alert alert-primary" role="alert">
Note: URLs that do not have a <code>parsed_url.netloc</code> of
<code>*.toolforge.org</code> are not shown.
</div>
</li>

<!-- Add more navigation items as needed -->
</ul>
</div>
<div
style="
</div>
</nav>
<div style="position: sticky; top: 5px; background-color: white; z-index: 1">
<form action="{{ url_for('search') }}" method="GET" style="display: flex; padding: 15px; justify-content: center">
<div class="form-outline" style="width: 75vw">
<input type="search" id="search" name="search" class="wiki-search-input"
placeholder="Search by Author, URL or Title" `{% if search_term %} value="{{ search_term }}" {% endif %}` />
</div>
<button type="submit" class="wiki-submit-btn"><b>Search</b></button>
</form>
</div>
<!-- Sorting mechanism -->
<div style="display: flex; justify-content: center;">
<div style="width: 75vw; display: flex; justify-content: center;">
<div style="width: 60px; display: flex;">
Sort By:
</div>
<div style="display: flex;">
<a class="dropdown-item" href="{{ url_for('index') }}" style="margin-right: 10px; color: blue;"> None </a>
<a class="dropdown-item" href="{{ url_for('sortT') }}" style="margin-right: 10px; color: blue;"> Title </a>
<a class="dropdown-item" href="{{ url_for('sortA') }}" style="color: blue;"> Author </a>
</div>
</div>
</div>

<div class="container mt-4">
<table class="table table-striped" style="table-layout: fixed">
<thead>
<tr>
<th>Tool</th>
<th>Description</th>
<th>Author</th>
<th data-toggle="tooltip" data-placement="top" title="Whether the tool is available or not">
Health
</th>
</tr>
</thead>
<tbody>
{% for tool in tools %}
<tr>
<td>
<a href="{{ tool.url }}" target="_blank" style="word-wrap: break-word">
{{ tool.title }}
</a>
</td>
<td data-toggle="tooltip" data-placement="top" title="{{tool.description}}">
{{tool.description[:50]}}...
</td>
<td>{{ tool.author }}</td>
<td>
{% if tool.health_status == True %}
<span class="badge bg-success">Available</span>
{% else %}
<span class="badge bg-danger">Unavailable</span>
{% endif %}
</td>
<!-- <td>{{ tool.last_checked }}</td> -->
</tr>
{% endfor %}
</tbody>
</table>
<div class="alert alert-primary" role="alert">
Note: URLs that do not have a <code>parsed_url.netloc</code> of
<code>*.toolforge.org</code> are not shown.
</div>
<div style="
margin-bottom: 30px;
display: flex;
justify-content: space-between;
"
>
<div>{{curr_page}} / {{total_pages}}</div>
<div>
{% if curr_page > 1 %}
<a href="{{ url_for('index', page=curr_page-1) }}">Previous</a>
{% endif %} {% if curr_page < total_pages %}
<a href="{{ url_for('index', page=curr_page+1) }}">Next</a>
">
<div>{{curr_page}} / {{total_pages}}</div>
<div>
{% if curr_page > 1 %}
<a href="{{ url_for(route, page=curr_page-1) }}">Previous</a>
{% endif %} {% if curr_page < total_pages %} <a href="{{ url_for(route, page=curr_page+1) }}">Next</a>
{% endif %}
</div>
</div>
<footer class="tool-footer">
<a
style="display: flex; justify-content: center"
href="https://github.com/gopavasanth/toolwatch"
>Source code (MIT License)</a
>
<div class="tool-next-timer">
Next crawl in <span class="header-timer-space">00h 00m 00s</span>
</div>
</footer>
</div>
<footer class="tool-footer">
<a style="display: flex; justify-content: center" href="https://github.com/gopavasanth/toolwatch">Source code (MIT
License)</a>
<div class="tool-next-timer">
Next crawl in <span class="header-timer-space">00h 00m 00s</span>
</div>
</footer>
</div>

<!-- Add Bootstrap JS scripts at the end of the body -->
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/popper.js/2.9.1/umd/popper.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.1/js/bootstrap.min.js"></script>
</body>

<!-- Add Bootstrap JS scripts at the end of the body -->
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/popper.js/2.9.1/umd/popper.min.js"></script>
<script src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/bootstrap/5.3.1/js/bootstrap.min.js"></script>
</body>
</html>
</html>