Skip to content

Commit

Permalink
Issue #26. User now can import zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
IoannisSina committed Jul 30, 2021
1 parent b59c065 commit 94a8dd5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ bin/
lib/



*.pyc
label_buddy/*.png
label_buddy/media/

extract_folder/

# local settings and db ignonred
label_buddy/db.sqlite3
Expand Down
22 changes: 22 additions & 0 deletions label_buddy/projects/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#functions used for views
import random

from zipfile import ZipFile
from django.core.files import File
from django.db.models import Q
from django.template.defaulttags import register

from .models import Project, Label
from users.models import User
from tasks.forms import TaskForm
from tasks.models import (
Task,
Annotation,
Expand Down Expand Up @@ -178,6 +182,24 @@ def users_annotated_task(tasks):
return context


# unzip file and add tasks
def add_tasks_from_compressed_file(compressed_file, project):
archive = ZipFile(compressed_file, 'r')
files_names = archive.namelist()

skipped_files = 0
for filename in files_names:
new_file = archive.open(filename, "r")
# for every file that has an extension in [.wav, .mp3, .mp4] create a task
if filename[-4:] in ['.wav', '.mp3', '.mp4']:
# create task
new_task = Task.objects.create(project=project)
new_task.file.save(filename, File(new_file))
else:
skipped_files += 1
return skipped_files


# functions for annotation page

# return next unlabeled task
Expand Down
20 changes: 15 additions & 5 deletions label_buddy/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
filter_tasks,
add_labels_to_project,
next_unlabeled_task_id,
add_tasks_from_compressed_file,
)


Expand All @@ -43,6 +44,8 @@ def index(request):
"""Index view"""
projects_count = 0
if request.user.is_authenticated:
if request.session.get('first_login'):
print("First!!")
projects = get_projects_of_user(request.user)
projects_count = projects.count()
else:
Expand Down Expand Up @@ -156,23 +159,30 @@ def project_page_view(request, pk):
tasks = filter_tasks(project, labeled, reviewed)
if not user or (user != request.user) or not project:
return HttpResponseRedirect("/")

if request.method == "POST":

task_form = TaskForm(request.POST, request.FILES)
if task_form.is_valid():
new_task = task_form.save(commit=False)
new_task.project = project
new_task.save()
file_extension = str(new_task.file)[-4:]
# if file uploaded is a zip add new tasks
if file_extension in ['.zip']:
# unzip file and add as many tasks as the files in the zip/rar file
skipped_files = add_tasks_from_compressed_file(new_task.file, project)
else:
new_task.project = project
new_task.save()
return HttpResponseRedirect(get_project_url(project.id))
else:
task_form = TaskForm()

tasks_per_page = 10
paginator = Paginator(tasks, tasks_per_page) # Show 15 tasks per page

page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)

context = {
"page_obj": page_obj,
"list_num_of_pages": range(1, paginator.num_pages+1),
Expand Down
2 changes: 1 addition & 1 deletion label_buddy/static/js/project_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function filter_tasks() {

// when a file is uploaded
function checkExtension(filePath) {
var valid_extensions = [".mp3", ".wav", ".mp4"];
var valid_extensions = [".mp3", ".wav", ".mp4", ".zip"];

var file = filePath.value;
var files_extension = file.substr(file.length - 4, 4);
Expand Down
2 changes: 1 addition & 1 deletion label_buddy/tasks/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TaskAdmin(admin.ModelAdmin):
search_fields = ["project"]
list_display = ["id", "project", "status", "review_status"]
list_display = ["id", "project", "file", "status", "review_status"]
ordering = ("id",)
list_filter = ["status", "review_status"]

Expand Down
2 changes: 1 addition & 1 deletion label_buddy/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Task(models.Model):
status = EnumChoiceField(Status, default=Status.unlabeled, help_text='If the task is annotated status must be labeled else unlabeled')
review_status = EnumChoiceField(Review_status, default=Review_status.unreviewed, help_text='Status for reviews')

assigned_to = models.ManyToManyField(User, blank=True, related_name='task_annotators', help_text='Annotators who will annotate the task')
# assigned_to = models.ManyToManyField(User, blank=True, related_name='task_annotators', help_text='Annotators who will annotate the task')

class Meta:
ordering = ['-id']
Expand Down
4 changes: 2 additions & 2 deletions label_buddy/templates/account/logout.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ <h3>Sign Out</h3>
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}

<button class="btn my-button" type="submit">Sign Out</button>
<a class="btn btn-danger my-button" href="/">No</a>
<button class="btn btn-danger my-button" type="submit">Sign Out</button>
<a style="color: white;" class="btn my-button" href="/">No</a>
</form>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions label_buddy/templates/label_buddy/delete_project.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ <h1>Delete {{ project.title }}</h1>
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}"/>
{% endif %}

<button class="btn my-button" type="submit">Yes</button>
<a class="btn btn-danger my-button" href="{% url 'project_page' project.id %}">No</a>
<button class="btn btn-danger my-button" type="submit">Yes</button>
<a style="color: white;" class="btn my-button" href="{% url 'project_page' project.id %}">No</a>
</form>
</div>
</div>
Expand Down
16 changes: 11 additions & 5 deletions label_buddy/templates/label_buddy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
{% block title %} Label Buddy {% endblock %}

{% block content %}
{% if user.is_authenticated%}
{% if user.is_authenticated %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Successfully singed in as {{ user.username }}!</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% if projects %}
<h1 style="text-align:center">Projects you are involved</h1>
{% if user.can_create_projects %}
Expand Down Expand Up @@ -90,10 +96,10 @@ <h1 style="text-align:center">Projects you are involved</h1>
{% else %}
<h1 style="text-align:center">No projects for you yet</h1>
{% if user.can_create_projects %}
<a href="{% url 'create_project'%}">
<button class="btn my-button my-button-page">Create</button>
</a>
{% endif %}
<a href="{% url 'create_project'%}">
<button class="btn my-button my-button-page">Create</button>
</a>
{% endif %}
{% endif %}

{% if projects_count > projects_per_page %}
Expand Down
3 changes: 1 addition & 2 deletions label_buddy/templates/label_buddy/project_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
{% endblock %}

{% block content %}

{% if tasks_count_no_filter > 0 %}
<h1 style="text-align:center">Tasks for {{ project.title }}</h1>
{% if user in project.managers.all%}
Expand Down Expand Up @@ -145,7 +144,7 @@ <h5 class="modal-title" id="exampleModalLongTitle">Import data</h5>
<span aria-hidden="true">&times;</span>
</button>
</div>
<p>This file is for the annotation process. The accepted formats are: <b>.wav, .mp3 and .mp4</b></p>
<p>This file is for the annotation process. The accepted formats are: <b>.wav, .mp3, .mp4 and .zip</b></p>
<div class="modal-body">
<form enctype="multipart/form-data" action="" method="POST">
{% csrf_token %}
Expand Down

0 comments on commit 94a8dd5

Please sign in to comment.