Skip to content

Commit

Permalink
Simplified task grid page logic.
Browse files Browse the repository at this point in the history
This will make deleting categories possible since there were indexing problems with the old approach.
  • Loading branch information
gartnera committed Mar 5, 2016
1 parent f4df8b3 commit 2dc3192
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 72 deletions.
69 changes: 19 additions & 50 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,69 +192,40 @@ def tasks():

user = get_user()
userCount = db['users'].count()
isAdmin = user['isAdmin']

categories = db['categories']
catCount = categories.count()

flags = db['flags']

tasks = db.query("SELECT * FROM tasks ORDER BY category, score");

tasks = list(tasks)
tasks = db['tasks']

grid = []

rowCount = 0
currentCat = 0
currentCatCount = 0

if len(tasks) == 0:
row = [None] * catCount
grid.append(row)

for task in tasks:
cat = task["category"] - 1

while currentCatCount + 1 >= rowCount:
row = [None] * catCount
grid.append(row)
rowCount += 1

if currentCat != cat:
if user['isAdmin']:
endTask = { "end": True, "category": currentCat }
grid[currentCatCount][currentCat] = endTask
currentCat = cat
currentCatCount = 0


percentComplete = (float(flags.count(task_id=task['id'])) / userCount) * 100

#hax for bad css (if 100, nothing will show)
if percentComplete == 100:
percentComplete = 99.99
for cat in categories:
cTasks = [x for x in tasks if x['category'] == cat['id']]
gTasks = []

task['percentComplete'] = percentComplete
gTasks.append(cat)
for task in cTasks:
percentComplete = (float(flags.count(task_id=task['id'])) / userCount) * 100

isComplete = bool(flags.count(task_id=task['id'], user_id=user['id']))
#hax for bad css (if 100, nothing will show)
if percentComplete == 100:
percentComplete = 99.99

task['isComplete'] = isComplete
task['percentComplete'] = percentComplete

grid[currentCatCount][cat] = task
currentCatCount += 1
isComplete = bool(flags.count(task_id=task['id'], user_id=user['id']))

#add the final endTask element
if user['isAdmin']:
if len(tasks) > 0:
endTask = { "end": True, "category": currentCat }
grid[currentCatCount][currentCat] = endTask
task['isComplete'] = isComplete
gTasks.append(task)

#if any None in first row, add end task
for i, t in enumerate(grid[0]):
if t is None:
endTask = { "end": True, "category": i }
grid[0][i] = endTask
if isAdmin:
gTasks.append({'add': True, 'category': cat['id']})

grid.append(gTasks)

# Render template
render = render_template('frame.html', lang=lang, page='tasks.html',
Expand Down Expand Up @@ -283,9 +254,7 @@ def addcatsubmit():
@app.route('/addtask/<cat>/', methods=['GET'])
@admin_required
def addtask(cat):
category = db.query('SELECT * FROM categories LIMIT 1 OFFSET :cat', cat=cat)
category = list(category)
category = category[0]
category = db['categories'].find_one(id=cat)

user = get_user()

Expand Down
9 changes: 9 additions & 0 deletions static/css/ctf.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ background-size: cover;
* Tasks page
*/

.category-column > a{
float: left;
clear: left;
}

.category-column{
float: left;
}


.chall-box-score {
z-index:100;
Expand Down
38 changes: 16 additions & 22 deletions templates/tasks.html
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
<div class="landing-page container">

<div class="row">
{% for category in categories %}
<div class="chall-category col-md-1">
{{ category.name }}
</div>
{% endfor %}
{% if user.isAdmin %}
<a href="/addcat" >
<div class="add-box col-md-1">
+
</div>
</a>
{% endif %}

</div>

{% for row in grid %}
<div class="row">
{% for row in grid %}
<div class="category-column">
{% for task in row %}
{% if task.id %}

{% if task.score %}
<a href="/tasks/{{ task.id }}">
<div style="animation-delay: -{{task.percentComplete}}s" class="chall-box chall-task col-md-1 {% if task.isComplete %}chall-done{% endif %}">
<span class="chall-box-score">{{ task.score }}</span>
</div>
</a>
{% elif task.end %}
{% elif task.add %}
<a href="/addtask/{{ task.category }}" >
<div class="add-box col-md-1">
+
</div>
</a>
{% else %}
<div class="chall-hidden col-md-1">
&nbsp;
<div class="chall-category col-md-1">
{{ task.name }}
</div>
{% endif %}
{% endfor %}
</div>
{% endfor %}
{% if user.isAdmin %}
<a href="/addcat" >
<div class="add-box col-md-1">
+
</div>
</a>
{% endif %}
</div>
{% endfor %}

</div>

Expand Down

0 comments on commit 2dc3192

Please sign in to comment.