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

Search - Adds the ability to search within an LCV and create an Alias without a context #16

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
26 changes: 12 additions & 14 deletions app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,13 @@ def get_form(self, request, obj=None, **kwargs):
class NeoTermAdminForm(forms.ModelForm):
alias = forms.CharField(required=False, help_text="Enter alias") # Custom field
definition = forms.CharField(required=True, help_text="Enter definition") # Custom field
context = forms.CharField(required=True, help_text="Enter context") # Custom field
context_description = forms.CharField(required=True, help_text="Enter context description") # Custom field
context = forms.CharField(required=False, help_text="Enter context") # Custom field
context_description = forms.CharField(required=False, help_text="Enter context description") # Custom field

class Meta:
model = NeoTerm
fields = ['lcvid', 'alias', 'definition', 'context', 'context_description']

# def clean_definition(self):
# definition = self.cleaned_data.get('definition')

# get_terms_with_multiple_definitions()
# # Check if the definition already exists in the NeoDefinition model
# if is_any_node_present(NeoDefinition, definition=definition):
# raise forms.ValidationError(f"A definition of '{definition}' already exists.")

# return definition # Return the cleaned value

class NeoTermAdmin(admin.ModelAdmin):
form = NeoTermAdminForm
list_display = ('lcvid', 'uid')
Expand All @@ -177,8 +167,17 @@ def save_model(self, request, obj, form, change):
definition = form.cleaned_data['definition']
context = form.cleaned_data['context']
context_description = form.cleaned_data['context_description']
logger.info(f"Creating NeoTerm with alias: {alias}, definition: {definition}, context: {context}, context_description: {context_description}")


logger.info(f"Creating NeoTerm with alias: {alias}, definition: {definition}, context: {context}, context_description: {context_description}")
if context == '' and context_description == '' and alias != '':
definition_node = NeoDefinition.nodes.get_or_none(definition=definition)
if definition_node:
messages.warning(request, 'Adding an alias without a context is not recommended.')
run_node_creation(alias=alias, definition=definition, context=context, context_description=context_description)
return
messages.error(request, 'Adding a definition without a context is not allowed.')
return
run_node_creation(alias=alias, definition=definition, context=context, context_description=context_description)

messages.success(request, 'NeoTerm saved successfully.')
Expand All @@ -187,7 +186,6 @@ def save_model(self, request, obj, form, change):
logger.error('Error saving NeoTerm: {}'.format(e))
messages.error(request, 'Error saving NeoTerm: {}'.format(e))
return


def delete_model(self, request, obj) -> None:
messages.error(request, 'Deleting terms is not allowed')
Expand Down
101 changes: 57 additions & 44 deletions app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,25 +472,17 @@ def create_new_term(cls, lcvid: str = None) -> 'NeoTerm':

return term_node

def set_relationships(self, definition_node, context_node, alias_node):
def set_relationships(self, definition_node=None, context_node=None, alias_node=None):
try:
if alias_node:
self.alias.connect(alias_node)
self.context.connect(context_node)
self.definition.connect(definition_node)
if context_node:
self.context.connect(context_node)
if definition_node:
self.definition.connect(definition_node)
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while connecting relationships for term '{self.uid}': {e}")
raise e

# @abstract
# class NeoGeneric(DjangoNode):

# def are_any_nodes_present(self, model, **kwargs):
# pass
# def connect(self, node, relationship):
# pass


class NeoAlias(DjangoNode):
django_id = UniqueIdProperty()
alias = StringProperty(unique_index=True,required=True)
Expand Down Expand Up @@ -520,16 +512,25 @@ def get_or_create(cls, alias: str) -> Tuple['NeoAlias', bool]:
logger.error(f"Unexpected error in get_or_create for alias '{alias}': {e}")
raise e

def set_relationships(self, term_node, context_node):
def set_relationships(self, term_node=None, context_node=None, collided_definition=None):
try:
self.term.connect(term_node)
self.context.connect(context_node)
if term_node:
self.term.connect(term_node)
if context_node:
self.context.connect(context_node)
if collided_definition:
self.collided_definition.connect(collided_definition)
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while connecting relationships for alias '{self.alias}': {e}")
raise e
except Exception as e:
logger.error(f"Unexpected error while connecting relationships for alias '{self.alias}': {e}")
raise e

def handle_collision(self, definition_node, context_node=None):
if context_node:
self.context.connect(context_node)
self.collided_definition.connect(definition_node)

class NeoContext(DjangoNode):
django_id = UniqueIdProperty()
Expand All @@ -545,32 +546,30 @@ class Meta:
@classmethod
def get_or_create(cls, context: str) -> Tuple['NeoContext', bool]:
try:

context_node = cls.nodes.get_or_none(context=context)

if context_node:
return context_node, False

context_node = NeoContext(context=context)
context_node.save()
return context_node, True

if not context == '':
context_node = NeoContext(context=context)
context_node.save()
return context_node, True
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while getting or creating context '{context}': {e}")
raise e
except Exception as e:
logger.error(e)
logger.error(f"Unexpected error in get_or_create for context '{context}': {e}")
raise e

def set_relationships(self, term_node, alias_node, definition_node, context_description_node):
def set_relationships(self, term_node=None, alias_node=None, definition_node=None, context_description_node=None,):
try:
self.term.connect(term_node)
if term_node:
self.term.connect(term_node)
if alias_node:
self.alias.connect(alias_node)
self.definition.connect(definition_node)
logger.info(f"Connecting context_description_node: {context_description_node}")
self.context_description.connect(context_description_node)
if definition_node:
self.definition.connect(definition_node)
if context_description_node:
self.context_description.connect(context_description_node)
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while connecting relationships for context '{self.context}': {e}")
raise e
Expand All @@ -588,27 +587,27 @@ class Meta:
app_label = 'core'

@classmethod
def get_or_create(cls, context_description: str, context_node: NeoContext):
def get_or_create(cls, context_description: str, context_node: 'NeoContext'):
try:
existing_context_description = context_node.context_description.all()
if existing_context_description:
return existing_context_description[0], False

context_description_node = NeoContextDescription(context_description=context_description)
existing = context_node.context_description.all() if context_node else []
if existing:
return existing[0], False
context_description_node = cls(context_description=context_description)
context_description_node.save()
return context_description_node, True

except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while getting or creating context_description '{context_description}': {e}")
raise e
except Exception as e:
logger.error(f"Unexpected error in get_or_create for context_description '{context_description}': {e}")
raise e

def set_relationships(self, definition_node, context_node):
def set_relationships(self, definition_node=None, context_node=None):
try:
self.definition.connect(definition_node)
self.context.connect(context_node)
if definition_node:
self.definition.connect(definition_node)
if context_node:
self.context.connect(context_node)
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while connecting relationships for context_description '{self.context_description}': {e}")
raise e
Expand All @@ -623,7 +622,7 @@ class NeoDefinition(DjangoNode):
rejected = BooleanProperty(default=False)
context = RelationshipTo('NeoContext', 'VALID_IN')
context_description = RelationshipFrom('NeoContextDescription', 'BASED_ON')
term = RelationshipFrom('NeoTerm', 'POINTS_TO')
term = Relationship('NeoTerm', 'POINTS_TO')
collision = Relationship('NeoDefinition', 'IS_COLLIDING_WITH')
collision_alias = Relationship('NeoAlias', 'WAS_ADDED_WITH')

Expand All @@ -642,12 +641,26 @@ def get_or_create(cls, definition:str, definition_embedding=None):

except Exception as e:
logger.error(f"Error in get for NeoDefinition '{definition}': {e}")
raise e

def get_term_node(self)-> 'NeoTerm':
if self.term:
logger.info(f'The data is: {self.term.single()}')
return self.term.single()
return None

def set_relationships(self, term_node, context_node, context_description_node):
def set_relationships(self, term_node=None, context_node=None, context_description_node=None, collision=None, collision_alias=None):
try:
self.term.connect(term_node)
self.context.connect(context_node)
self.context_description.connect(context_description_node)
if term_node:
self.term.connect(term_node)
if context_node:
self.context.connect(context_node)
if context_description_node:
self.context_description.connect(context_description_node)
if collision:
self.collision.connect(collision)
if collision_alias:
self.collision_alias.connect(collision_alias)
except exceptions.NeomodelException as e:
logger.error(f"NeoModel-related error while connecting relationships for definition '{self.definition}': {e}")
raise e
Expand Down
1 change: 0 additions & 1 deletion app/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

urlpatterns = [
path('neo_term_list/', views.get_neo_terms, name='neo_term_list'),

]
Loading