Skip to content

Commit

Permalink
Rename [Topic]order to topic_number. Add field [Topic]namespace.
Browse files Browse the repository at this point in the history
Make article_number non-nullable and unique. Add field [Article]batch_name.
  • Loading branch information
normangilmore committed Nov 10, 2017
1 parent 79b3e96 commit 45e7c98
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/components/Quiz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class Quiz extends Component {

var rootTopicName = '';
for(var i = 0; i < topic.length; i++) {
// Topics are sorted by their "order" field in ascending order.
// Topics are sorted by their topic_number field in ascending order.
// The root topic is imported as order 0. So we're going to find
// it first before any subtopic.
if (this.props.currTask.topTopicId === topic[i].id) {
Expand Down
2 changes: 1 addition & 1 deletion app/components/TextSpanner/model/DisplayState.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class DisplayState extends DisplayStateRecord {
orderedLayers.push(annoToLayer.get(annotationKey));
};
};
orderedLayers.sort( (a, b) => a.order - b.order );
orderedLayers.sort( (a, b) => a.topic_number - b.topic_number );
return orderedLayers;
}
}
13 changes: 5 additions & 8 deletions data/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ def load_topics(topics):
"""
root_topic = None
for topic_args in topics:
topic_number = topic_args.pop('topic_number')
# Use topic number for sort order
topic_args['order'] = int(topic_number)
# Set questions aside, put them back after creating topic
questions = topic_args.pop('questions')
# Set reference to parent (will be None for first topic...)
Expand All @@ -121,7 +118,6 @@ def load_topics(topics):
Topic.objects.filter(parent=topic).delete()

topic_args['id'] = topic.id
topic_args['topic_number'] = topic_number
if root_topic is None:
root_topic = topic
load_questions(questions, topic)
Expand Down Expand Up @@ -266,12 +262,12 @@ def load_dependencies(schema):

def load_options(schema, root_topic):
for option in schema['options']:
if root_topic.order == option.topic:
if root_topic.topic_number == option.topic:
topic_obj = root_topic
else:
try:
topic_obj = Topic.objects.get(parent=root_topic,
order=option.topic)
topic_number=option.topic)
except Topic.DoesNotExist:
logger.error("%s\nDidn't find topic number %d" % (option, option.topic,))
continue
Expand Down Expand Up @@ -348,10 +344,11 @@ def load_annotations(article, article_obj):
topic = Topic.objects.filter(name=tua_type)[0]
except IndexError:
# No analysis type loaded--create a dummy type.
next_order = Topic.objects.aggregate(Max('order'))['order__max'] + 1
last_number = Topic.objects.aggregate(Max('topic_number'))
next_number = last_number['topic_number__max'] + 1
topic = Topic.objects.create(
name=tua_type,
order=next_order,
topic_number=next_number,
instructions='',
glossary='',
)
Expand Down
4 changes: 2 additions & 2 deletions thresher/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ProjectAdmin(admin.ModelAdmin):


class ArticleAdmin(admin.ModelAdmin):
list_display = ('id', 'article_number', metadata_filename)
list_display = ('id', 'batch_name', 'article_number', metadata_filename)

actions = ['view_articles']

Expand All @@ -77,7 +77,7 @@ def getParent(self, obj):
return "-"
getParent.short_description = 'Parent'

list_display = ('id', 'getParent', 'order', 'name')
list_display = ('id', 'getParent', 'namespace', 'topic_number', 'name')


class QuestionAdmin(admin.ModelAdmin):
Expand Down
41 changes: 41 additions & 0 deletions thresher/migrations/0017_auto_20171110_0608.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-11-10 06:08
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('thresher', '0016_answer_options'),
]

operations = [
migrations.RemoveField(
model_name='topic',
name='order',
),
migrations.AddField(
model_name='article',
name='batch_name',
field=models.TextField(db_index=True, default=b''),
),
migrations.AddField(
model_name='topic',
name='namespace',
field=models.CharField(default=b'', max_length=64),
),
migrations.AddField(
model_name='topic',
name='topic_number',
field=models.IntegerField(default=0),
preserve_default=False,
),
migrations.AlterField(
model_name='article',
name='article_number',
field=models.IntegerField(default=0, unique=True),
preserve_default=False,
),
]
29 changes: 20 additions & 9 deletions thresher/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,14 @@ def get_remote_URL(self):

# Articles containing text for analysis
class Article(models.Model):
# A number assigned by researcher. Not the autofield.
article_number = models.IntegerField(null=True)
# Mandatory article number assigned by researcher.
article_number = models.IntegerField(unique=True)

# batch_name is used to group articles into sets so they can be
# selected as groups for creating tasks. If the articles come from
# a directory hierarchy, it could be useful to store the directory
# path for the source article in this field.
batch_name = models.TextField(default="", db_index=True)

# raw article text
text = models.TextField()
Expand All @@ -176,8 +182,13 @@ class Topic(models.Model):
# The name of the topic
name = models.TextField()

# The order of a leaf-topic
order = models.IntegerField(null=True)
# Plan to replace parent/child hierarchy with a namespace
# Likely will be the filename of the source schema
namespace = models.CharField(default="", max_length=64)

# The order topics in this namespace are presented.
# renamed from 'order'
topic_number = models.IntegerField()

# Glossary related to the topic under analysis
glossary = JSONField(default={}) # as a JSON map
Expand All @@ -196,20 +207,20 @@ def __unicode__(self):
def getTopicTree(self):
""" returns the topic with all levels of its subtopic tree """
topicQuery = Topic.objects.raw("""
WITH RECURSIVE subtopic(id, parent_id, name, "order",
WITH RECURSIVE subtopic(id, parent_id, name, topic_number,
glossary, instructions)
AS (
SELECT id, parent_id, name, "order",
SELECT id, parent_id, name, topic_number,
glossary, instructions
FROM thresher_topic WHERE id=%s
UNION ALL
SELECT t.id, t.parent_id, t.name, t.order,
SELECT t.id, t.parent_id, t.name, t.topic_number,
t.glossary, t.instructions
FROM subtopic, thresher_topic t
WHERE t.parent_id = subtopic.id
)
SELECT id, parent_id, name, "order", glossary, instructions
FROM subtopic ORDER BY "order" LIMIT 500;
SELECT id, parent_id, name, topic_number, glossary, instructions
FROM subtopic ORDER BY topic_number LIMIT 500;
""", [self.id])
# Force query to execute and generate Topic models array
return topicQuery[:]
Expand Down
12 changes: 6 additions & 6 deletions thresher/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ class Meta:

class HighlightGroupSerializer(serializers.ModelSerializer):
topic_name = serializers.SerializerMethodField()
topic_order = serializers.SerializerMethodField()
topic_number = serializers.SerializerMethodField()

def get_topic_name(self, obj):
return obj.topic.name

def get_topic_order(self, obj):
return obj.topic.order
def get_topic_number(self, obj):
return obj.topic.topic_number

class Meta:
model = HighlightGroup
fields = ('id', 'article_highlight', 'topic',
'topic_name', 'topic_order',
'topic_name', 'topic_number',
'case_number', 'offsets')

class ArticleHighlightSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -123,13 +123,13 @@ class TopicSerializer(serializers.ModelSerializer):
class Meta:
model = Topic
fields = ('id', 'parent', 'name',
'order', 'glossary', 'instructions',
'topic_number', 'glossary', 'instructions',
'questions')

class RootTopicSerializer(serializers.ModelSerializer):
class Meta:
model = Topic
fields = ('id', 'name', 'order', 'glossary', 'instructions')
fields = ('id', 'name', 'topic_number', 'glossary', 'instructions')

class NLPHintSerializer(serializers.ModelSerializer):
class Meta:
Expand Down

0 comments on commit 45e7c98

Please sign in to comment.