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

Serializers #55

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5cbac4d
Made new models branch
RaquelGarcia999 Nov 5, 2019
aa50d4b
updated models with import and manytomany fields
mingye-christina-wang Nov 7, 2019
c166e2d
Merge branch 'master' into Django_models
RaquelGarcia999 Nov 8, 2019
b5eebe6
Too many blank spaces
RaquelGarcia999 Nov 8, 2019
0c23a59
Copying Edge, Person classes and load_network_json_data from master s…
RaquelGarcia999 Nov 8, 2019
e5b31d5
Merge branch 'master' of https://github.com/dhmit/tobacco_networks
RaquelGarcia999 Nov 11, 2019
7e6d349
Merge branch 'master' into Django_models
RaquelGarcia999 Nov 11, 2019
69e7ca4
Figuring out some TODOs for next session
RaquelGarcia999 Nov 11, 2019
7d8923f
Added PersonInfoSerializer
RaquelGarcia999 Nov 12, 2019
ead38bf
Temporary fix for bug with document
RaquelGarcia999 Nov 12, 2019
25d06a9
Moved the url for 'api/person_info/' above
RaquelGarcia999 Nov 12, 2019
e79b568
Updated get_person_info to return person info
RaquelGarcia999 Nov 14, 2019
c403f15
Merge branch 'master' of https://github.com/dhmit/tobacco_networks
RaquelGarcia999 Nov 15, 2019
4d1f3af
Deleted the dummy person in views.py
RaquelGarcia999 Nov 15, 2019
dd8cfcf
Merge branch 'master' into Serializers
RaquelGarcia999 Nov 16, 2019
389bf48
I deleted some imports that were never used
RaquelGarcia999 Nov 16, 2019
69c88be
Imported BACKEND_DIR
RaquelGarcia999 Nov 16, 2019
c02a853
I commented out things I thought we were going to delete
RaquelGarcia999 Nov 16, 2019
2959c52
Added a test case that checks models in tests.py
RaquelGarcia999 Nov 21, 2019
61191a0
Added test cases for views.py
RaquelGarcia999 Nov 22, 2019
89b7b30
Trying to render person_research_directors
RaquelGarcia999 Nov 25, 2019
64c7fd1
Merge branch 'master' of https://github.com/dhmit/tobacco_networks
RaquelGarcia999 Dec 3, 2019
dc55d58
Merge branch 'master' of https://github.com/dhmit/tobacco_networks
RaquelGarcia999 Dec 5, 2019
7530262
Trying to merge views
RaquelGarcia999 Dec 5, 2019
c0c142d
Merging serializers into
RaquelGarcia999 Dec 6, 2019
9acdf03
Changing Person to DjangoPerson
RaquelGarcia999 Dec 6, 2019
38ba209
Updating DjangoPerson and pasting in api_views
RaquelGarcia999 Dec 6, 2019
b3531c0
Imported counter
RaquelGarcia999 Dec 6, 2019
0ed1ccf
importing DjangoPerson
RaquelGarcia999 Dec 6, 2019
2ef991c
importing personinfoserializers
RaquelGarcia999 Dec 6, 2019
da57436
import personinfoserializer
RaquelGarcia999 Dec 6, 2019
00fd555
Fixed pylint issues
RaquelGarcia999 Dec 6, 2019
fa677a7
Merge branch 'master' into Serializers
srisi Dec 6, 2019
52659e7
Added list_edges
RaquelGarcia999 Dec 6, 2019
bd271ee
Merge branch 'Serializers' of https://github.com/dhmit/tobacco_networ…
RaquelGarcia999 Dec 6, 2019
8428734
We made migrations
RaquelGarcia999 Dec 6, 2019
36a0943
Merge branch 'master' into Serializers
mingye-christina-wang Dec 10, 2019
7f577f7
fixed pylint and django backend test
mingye-christina-wang Dec 10, 2019
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Run Django tests
run: |
cd backend
python manage.py test
python manage.py test apps.main

analysis_tests:
runs-on: ubuntu-latest
Expand All @@ -63,7 +63,7 @@ jobs:
run: |
pip install -r requirements.txt
python setup.py develop

- name: Run analysis tests
run: |
cd analysis
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ To create or update Django databases, run in terminal (in the backend folder):
2. python manage.py migrate

Note:
- the api does not work at (http://127.0.0.1:8000/api/) as it's not set up for this repo. You can skip the step.
- there is no `frontend/src/App.js`. Use `frontend/src/main/main.js`
instead.

The api is functional: (http://127.0.0.1:8000/api/person_info/)

If you would like to find a person through the API,
paste the above url, followed by:

?full_name=Relevant Person Name

If the person is not in the database,
the API will return an "empty" person whose full name will be
shown as: Relevant Person Name not found.
Otherwise, a dictionary with the information in the
database associated with that person will be returned.
18 changes: 17 additions & 1 deletion backend/apps/main/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.2.4 on 2019-10-25 20:15
# Generated by Django 2.2.4 on 2019-12-06 22:05

from django.db import migrations, models

Expand All @@ -11,6 +11,20 @@ class Migration(migrations.Migration):
]

operations = [
migrations.CreateModel(
name='DjangoPerson',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last', models.CharField(max_length=250)),
('first', models.CharField(max_length=250)),
('middle', models.CharField(max_length=250)),
('full_name', models.CharField(max_length=250, unique=True)),
('most_likely_org', models.CharField(max_length=250)),
('positions', models.TextField()),
('aliases', models.TextField()),
('count', models.IntegerField()),
],
),
migrations.CreateModel(
name='Document',
fields=[
Expand All @@ -30,6 +44,8 @@ class Migration(migrations.Migration):
('text', models.TextField(blank=True)),
('tid', models.CharField(max_length=250, unique=True)),
('title', models.CharField(blank=True, max_length=250)),
('authors', models.ManyToManyField(related_name='document_by_authors', to='main.DjangoPerson')),
('recipients', models.ManyToManyField(related_name='document_by_recipients', to='main.DjangoPerson')),
],
),
]
36 changes: 0 additions & 36 deletions backend/apps/main/migrations/0002_auto_20191108_2333.py

This file was deleted.

18 changes: 0 additions & 18 deletions backend/apps/main/migrations/0003_auto_20191112_1537.py

This file was deleted.

18 changes: 0 additions & 18 deletions backend/apps/main/migrations/0004_djangoperson_full_name.py

This file was deleted.

18 changes: 0 additions & 18 deletions backend/apps/main/migrations/0005_auto_20191117_2156.py

This file was deleted.

33 changes: 0 additions & 33 deletions backend/apps/main/migrations/0006_auto_20191121_1535.py

This file was deleted.

1 change: 0 additions & 1 deletion backend/apps/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class Document(models.Model):
text: TextField, text of the document
tid: CharField, document ID
title: CharField, title of the document

authors: ManyToManyField, connect Document and its authors' DjangoPerson objects in database
recipients: ManyToManyField, connect Document and its recipients' DjangoPerson objects in
database
Expand Down
73 changes: 62 additions & 11 deletions backend/apps/main/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
in ways that can be transported across the backend/frontend divide, or
allow the frontend to suggest changes to the backend/database.
"""
import json
from pathlib import Path
from rest_framework import serializers
from .models import DjangoPerson


class PersonSerializer(serializers.Serializer):
class EdgeSerializer(serializers.Serializer):
"""
Serializer for the Person model
Serializer for the Edge model
"""
pk = serializers.IntegerField(read_only=True) # pylint: disable=C0103
name = serializers.CharField(read_only=True)
node1 = serializers.CharField(read_only=True)
node2 = serializers.CharField(read_only=True)
docs = serializers.IntegerField(read_only=True)
words = serializers.IntegerField(read_only=True)

Expand All @@ -22,19 +26,66 @@ def update(self, instance, validated_data):
""" We will not update data using this serializer """


class EdgeSerializer(serializers.Serializer):
class PersonInfoSerializer(serializers.ModelSerializer):
"""
Serializer for the Edge model
Serializer for the DjangoPerson Info model
"""
pk = serializers.IntegerField(read_only=True) # pylint: disable=C0103
node1 = serializers.CharField(read_only=True)
node2 = serializers.CharField(read_only=True)
docs = serializers.IntegerField(read_only=True)
words = serializers.IntegerField(read_only=True)

class Meta:
model = DjangoPerson
fields = ["last", "first", "middle", "full_name", "most_likely_org", "positions",
"aliases", "count"]

def create(self, validated_data):
""" We will not create new objects using this serializer """

def update(self, instance, validated_data):
""" We will not update data using this serializer """


class Edge:
"""
Python object to store edges, should be replaced if the DjangoPerson class is replaced
"""
def __init__(self, pk: int, node1: str, node2: str, # pylint: disable-msg=R0913
docs: int, words: int):
self.pk = pk # pylint: disable=C0103
self.node1 = node1 # could be replaced with DjangoPerson
self.node2 = node2
self.docs = docs
self.words = words


def load_network_json_data(return_type: str):
"""
Loads test json data for initial prototyping
:param return_type: string, determines which list needs to be determined, has the
precondition that it either must be 'nodes' or 'edges'
:return: list, contains all of the edges or all of the nodes (people at this point in time)
from network_test_data.json
"""
if return_type not in ['nodes', 'edges']:
raise ValueError("Specified return type needs to be nodes or edges")

json_path = Path('/Users/kimba/Documents/GitHub/tobacco_networks/backend', 'data',
'network_test_data.json')
with open(json_path) as json_file:
data = json.load(json_file)
if return_type == "edges":
edges = []
for edges_dict in data['links']:
pk = int(edges_dict.get('id')) # pylint: disable=C0103
node1 = edges_dict.get('node1')
node2 = edges_dict.get('node2')
docs = edges_dict.get('docs')
words = int(edges_dict.get('words'))
edges.append(Edge(pk, node1, node2, docs, words))
return edges
else:
people = []
for person_dict in data['nodes']:
pk = int(person_dict.get('id')) # pylint: disable=C0103
name = person_dict.get('name')
docs = person_dict.get('docs')
words = int(person_dict.get('words'))
people.append(DjangoPerson(pk, name, docs, words))
return people
Loading