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

Workspace Model Implementation #412

Closed
wants to merge 5 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
1 change: 1 addition & 0 deletions project/backend/database/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Register your models here.

admin.site.register(Workspace)
admin.site.register(Entry)
admin.site.register(BasicUser)
admin.site.register(Contributor)
admin.site.register(Reviewer)
Expand Down
142 changes: 87 additions & 55 deletions project/backend/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,76 @@
import copy
from datetime import datetime

# Create your models here.
class Workspace(models.Model):

class SemanticTag(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
label = models.CharField(max_length=50, unique=True)
desc = models.CharField(max_length=100)
parent_tag = models.ForeignKey("SemanticTag", on_delete=models.CASCADE, null=True, blank=True,
related_name="sub_tags")

@property
def count(self):
return self.node_set.all().count()

@property
def nodes(self):
return self.node_set.all()

@property
def recursive_nodes(self):
nodes = list(self.nodes)

for sub in self.sub_tags.all():
nodes.extend(sub.recursive_nodes)

return nodes

@property
def recursive_count(self):
return len(self.recursive_nodes)

class Meta:
constraints = [
models.UniqueConstraint(fields=['label', 'parent_tag'],
name='semantictag_label_parenttag_unique_constraint')
]

class WikiTag(models.Model):
pass
class Request(models.Model):
"""
This class definition is written beforehand (to be implemented afterwards)
This class definition is written beforehand (to be implemented afterwards)
in order to be referred from other classes. e.g. ReviewRequest
"""
pass
class Entry(models.Model):
"""
This class definition is written beforehand (to be implemented afterwards)
in order to be referred from other classes. e.g. Contributor
This class definition is written beforehand (to be implemented afterwards)
in order to be referred from other classes. e.g. Workspace
"""
pass
# Create your models here.
class Workspace(models.Model): #Node and Review Requests may be added later
workspace_id = models.AutoField(primary_key=True)
workspace_title = models.CharField(max_length=100)
collab_requests = models.ManyToManyField(Request, blank=True,related_name='CollaborationRequests')
semantic_tags = models.ManyToManyField(SemanticTag, blank=True,related_name = 'WorkspaceSemanticTags')
wiki_tags = models.ManyToManyField(WikiTag,blank=True,related_name = 'WorkspaceWikiTags')
is_finalized = models.BooleanField(null = True)
is_published = models.BooleanField(null = True)
is_in_review = models.BooleanField(null = True)
is_rejected = models.BooleanField(null = True)
theorem_posted = models.BooleanField(null = True)
num_approvals = models.IntegerField(null = True)
theorem_entry = models.ManyToManyField(Entry,related_name='TheoremEntry')
final_entry = models.ForeignKey(Entry,null=True, on_delete=models.CASCADE,related_name='FinalEntry')
def finalize_workspace(self):
self.is_finalized = True
self.is_in_review = False
return True


class BasicUser(models.Model):
Expand All @@ -31,38 +94,45 @@ class Contributor(BasicUser):

def __str__(self):
return self.user.first_name + " " + self.user.last_name

"""
Methods below (create/delete Workspace instances) should be reinvestigated
after implementation of Workspace class.
"""
def create_workspace(self):
new_workspace = Workspace.objects.create()
new_workspace = Workspace.objects.create(
workspace_id=1,
workspace_title="Test Workspace",
is_finalized=False,
is_published=False,
is_in_review=False,
is_rejected=False,
theorem_posted=False,
num_approvals=0,
final_entry=None
)
self. workspaces.add(new_workspace)
return new_workspace

def delete_workspace(self, workspace_to_delete): # Note that this function doesn't delete the
if workspace_to_delete in self.workspaces.all(): # Workspace but pops from the list to prevent
if workspace_to_delete in self.workspaces.all(): # Workspace but pops from the list to prevent
self.workspaces.remove(workspace_to_delete) # errors if multiple Contributors present

class Reviewer(Contributor):

def __str__(self):
return self.user.first_name + " " + self.user.last_name
def get_review_requests(self):

def get_review_requests(self):
return ReviewRequest.objects.filter(reviewer=self)



class Admin(BasicUser):
def __str__(self):
return self.user.first_name + " " + self.user.last_name

class Request(models.Model):
"""
This class definition is written beforehand (to be implemented afterwards)
in order to be referred from other classes. e.g. ReviewRequest
"""
pass

class ReviewRequest(Request):
"""
This class definition is written beforehand (to be implemented afterwards)
Expand All @@ -84,43 +154,6 @@ class Theorem(models.Model):
publish_date = models.DateField()


class SemanticTag(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
label = models.CharField(max_length=50, unique=True)
desc = models.CharField(max_length=100)
parent_tag = models.ForeignKey("SemanticTag", on_delete=models.CASCADE, null=True, blank=True, related_name="sub_tags")

@property
def count(self):
return self.node_set.all().count()

@property
def nodes(self):
return self.node_set.all()

@property
def recursive_nodes(self):
nodes = list(self.nodes)

for sub in self.sub_tags.all():
nodes.extend(sub.recursive_nodes)

return nodes

@property
def recursive_count(self):
return len(self.recursive_nodes)

class Meta:
constraints = [
models.UniqueConstraint(fields=['label', 'parent_tag'], name='semantictag_label_parenttag_unique_constraint')
]


class WikiTag(models.Model):
pass


class Annotation(models.Model):
pass

Expand All @@ -145,7 +178,6 @@ class Node(models.Model):
def increment_num_visits(self):
self.num_visits += 1


class Proof(models.Model):
proof_id = models.AutoField(primary_key=True)
proof_title = models.CharField(max_length=100, null=False)
Expand Down
69 changes: 67 additions & 2 deletions project/backend/database/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@ def test_create_workspace(self):
def test_delete_workspace(self):
# Create a workspace and add it to the contributor
contributor = Contributor.objects.create(user=User.objects.create())
workspace = Workspace.objects.create()
workspace = Workspace.objects.create(
workspace_id=1,
workspace_title="Test Workspace",
is_finalized=False,
is_published=False,
is_in_review=False,
is_rejected=False,
theorem_posted=False,
num_approvals=0,
final_entry=None
)
contributor.workspaces.add(workspace)

# Test the delete_workspace method
Expand All @@ -97,7 +107,17 @@ def test_delete_workspace(self):
def test_delete_nonexistent_workspace(self):
# Create a workspace, but don't add it to the contributor
contributor = Contributor.objects.create(user=User.objects.create())
workspace = Workspace.objects.create()
workspace = Workspace.objects.create(
workspace_id=1,
workspace_title="Test Workspace",
is_finalized=False,
is_published=False,
is_in_review=False,
is_rejected=False,
theorem_posted=False,
num_approvals=0,
final_entry=None
)

# Test the delete_workspace method with a non-existent workspace
contributor.delete_workspace(workspace) # This should not raise an error
Expand Down Expand Up @@ -207,6 +227,51 @@ def test_admin_create(self):
self.assertFalse(admin.email_notification_preference)
self.assertTrue(admin.show_activity_preference)

class WorkspaceModelTestCase(TestCase):
def tearDown(self):
Workspace.objects.all().delete()
print("All tests for the Workspace Model are completed!")

def test_workspace_model(self): #Testing workspace model creation
workspace = Workspace.objects.create(
workspace_id = 1,
workspace_title = "Test Workspace",
is_finalized = False,
is_published= False,
is_in_review = False,
is_rejected = False,
theorem_posted = False,
num_approvals = 0,
final_entry = None
)

self.assertEqual(workspace.workspace_id, 1)
self.assertEqual(workspace.workspace_title, "Test Workspace")
self.assertEqual(workspace.is_finalized, False)
self.assertEqual(workspace.is_published, False)
self.assertEqual(workspace.is_in_review, False)
self.assertEqual(workspace.is_rejected, False)
self.assertEqual(workspace.theorem_posted, False)
self.assertEqual(workspace.num_approvals, 0)
self.assertIsNone(workspace.final_entry)

def test_finalize_workspace(self): #Testing finalize workspace function
workspace = Workspace.objects.create(
workspace_id=1,
workspace_title="Test Workspace",
is_finalized=False,
is_published=False,
is_in_review=False,
is_rejected=False,
theorem_posted=False,
num_approvals=0,
final_entry=None
)
Workspace.finalize_workspace(workspace)
self.assertEqual(workspace.is_finalized, True)
self.assertEqual(workspace.is_in_review, False)



class NodeModelTestCase(TestCase):
def tearDown(self):
Expand Down