Repository navigation
Authorization/Permissions Adapter #86
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
538ecd1
Permissions intial commit
johnyrahul 0beefe9
Merge branch 'main' into feat/adapter-permission
johnyrahul cb2b423
Update main.py
johnyrahul a3f0c97
Permissions for adapter intial commit
johnyrahul d9b345d
Corrected the query
johnyrahul 82d1ccd
Merge branch 'main' into feat/adapter-permission
johnyrahul a0d4b1b
Default triad and tool setting changes
johnyrahul dd7be01
Merge branch 'main' into feat/adapter-permission
johnyrahul d2c5e7e
Default triad and tool setting changes
johnyrahul 8ae820a
Resolving the conflicts
johnyrahul 8c7a990
Merge branch 'main' into feat/adapter-permission
johnyrahul a61b366
Fix issue in pre-populating the default triad on the drpdown
mohamed-siddhiq 8daa03d
implemented UI for adapter permissions
jaags-dev d4a3acc
Added permission for shared users
johnyrahul 6f9298c
Merge branch 'feat/adapter-permission' into feat/adapter-permission-u…
jaags-dev 7575ac9
changed delete text and code clean up
jaags-dev 53a8f27
Merge branch 'main' into feat/adapter-permission
johnyrahul 14d73c2
Merge branch 'feat/adapter-permission' into feat/adapter-permission-u…
jaags-dev 331e765
Merge branch 'feat/adapter-permission-ui-changes' of github.com:Zipst…
jaags-dev d432ddc
changed delete text for share popup
jaags-dev cc2553a
removed is_defaul from serilaizer
johnyrahul 71af56f
Merge branch 'feat/adapter-permission-ui-changes' of https://github.c…
johnyrahul 8956656
Merge pull request #100 from Zipstack/feat/adapter-permission-ui-changes
johnyrahul 1bc888a
handled missing value for created by email
jaags-dev c6dae31
Merge branch 'main' into feat/adapter-permission
johnyrahul ed45c7d
optimized migration file
johnyrahul 0ebbd62
Merge branch 'feat/adapter-permission' of https://github.com/Zipstack…
johnyrahul 5ab0ac8
Merge branch 'main' into feat/adapter-permission
johnyrahul dc56311
code optimization
jaags-dev 22e953a
Added comments
johnyrahul f292941
Merge branch 'main' into feat/adapter-permission
jaags-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| import logging | ||
| from typing import Any, Optional | ||
|
|
||
| import adapter_processor | ||
| from account.models import User | ||
| from adapter_processor.constants import AdapterKeys | ||
| from adapter_processor.exceptions import ( | ||
|
|
@@ -21,7 +20,7 @@ | |
| from unstract.adapters.exceptions import AdapterError | ||
| from unstract.adapters.x2text.constants import X2TextConstants | ||
|
|
||
| from .models import AdapterInstance | ||
| from .models import AdapterInstance, UserDefaultAdapter | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -131,47 +130,34 @@ def __fetch_adapters_by_key_value(key: str, value: Any) -> Adapter: | |
|
|
||
| @staticmethod | ||
| def set_default_triad(default_triad: dict[str, str], user: User) -> None: | ||
| filter_params: dict[str, Any] = {} | ||
|
|
||
| try: | ||
| for key in default_triad: | ||
| filter_params.clear() | ||
| adapter_id = default_triad[key] | ||
| # Query rows where adapter_type=X and is_default=True | ||
| if key == AdapterKeys.LLM_DEFAULT: | ||
| adapter_type = AdapterTypes.LLM.name | ||
| elif key == AdapterKeys.EMBEDDING_DEFAULT: | ||
| adapter_type = AdapterTypes.EMBEDDING.name | ||
| elif key == AdapterKeys.VECTOR_DB_DEFAULT: | ||
| adapter_type = AdapterTypes.VECTOR_DB.name | ||
| elif key == AdapterKeys.X2TEXT_DEFAULT: | ||
| adapter_type = AdapterTypes.X2TEXT.name | ||
|
|
||
| filter_params["adapter_type"] = adapter_type | ||
| filter_params["is_default"] = True | ||
| filter_params["created_by"] = user | ||
|
|
||
| AdapterInstance.objects.filter(**filter_params).update( | ||
| is_default=False | ||
| ) | ||
| ( | ||
| user_default_adapter, | ||
| created, | ||
| ) = UserDefaultAdapter.objects.get_or_create(user=user) | ||
|
|
||
| # Update the adapter_id in the incoming | ||
| # list to set is_default=True | ||
| filter_params.clear() | ||
| try: | ||
| new_adapter_default: AdapterInstance = ( | ||
| AdapterInstance.objects.get(pk=adapter_id) | ||
| if default_triad.get(AdapterKeys.LLM_DEFAULT, None): | ||
| user_default_adapter.default_llm_adapter = ( | ||
| AdapterInstance.objects.get( | ||
| pk=default_triad[AdapterKeys.LLM_DEFAULT] | ||
| ) | ||
| new_adapter_default.is_default = True | ||
| new_adapter_default.save() | ||
| except ( | ||
| adapter_processor.models.AdapterInstance.DoesNotExist | ||
| ) as e: | ||
| logger.error( | ||
| f"Error while retrieving adapter: {adapter_id} " | ||
| f"reason: {e}" | ||
| ) | ||
| if default_triad.get(AdapterKeys.EMBEDDING_DEFAULT, None): | ||
| user_default_adapter.default_embedding_adapter = ( | ||
| AdapterInstance.objects.get( | ||
| pk=default_triad[AdapterKeys.EMBEDDING_DEFAULT] | ||
| ) | ||
| raise InValidAdapterId() | ||
| ) | ||
|
|
||
| if default_triad.get(AdapterKeys.VECTOR_DB_DEFAULT, None): | ||
| user_default_adapter.default_vector_db_adapter = ( | ||
| AdapterInstance.objects.get( | ||
| pk=default_triad[AdapterKeys.VECTOR_DB_DEFAULT] | ||
| ) | ||
| ) | ||
|
|
||
| user_default_adapter.save() | ||
|
|
||
| logger.info("Changed defaults successfully") | ||
| except Exception as e: | ||
| logger.error(f"Unable to save defaults because: {e}") | ||
|
|
@@ -209,13 +195,17 @@ def get_adapters_by_type( | |
|
|
||
| Parameters: | ||
| - adapter_type (AdapterTypes): The type of adapters to retrieve. | ||
| - user: Logged in User | ||
|
|
||
| Returns: | ||
| - list[AdapterInstance]: A list of AdapterInstance objects that match | ||
| the specified adapter type. | ||
| """ | ||
| adapters: list[AdapterInstance] = AdapterInstance.objects.filter( | ||
| adapter_type=adapter_type.value, created_by=user | ||
|
|
||
| adapters: list[AdapterInstance] = AdapterInstance.objects.for_user( | ||
| user | ||
| ).filter( | ||
| adapter_type=adapter_type.value, | ||
| ) | ||
| return adapters | ||
|
|
||
|
|
@@ -260,9 +250,16 @@ def get_default_adapters(user: User) -> list[AdapterInstance]: | |
| marked as default. | ||
| """ | ||
| try: | ||
| adapters: list[AdapterInstance] = AdapterInstance.objects.filter( | ||
| is_default=True, created_by=user | ||
| ) | ||
| adapters: list[AdapterInstance] = [] | ||
| default_adapter = UserDefaultAdapter.objects.get(user=user) | ||
|
|
||
| if default_adapter.default_embedding_adapter: | ||
| adapters.append(default_adapter.default_embedding_adapter) | ||
| if default_adapter.default_llm_adapter: | ||
| adapters.append(default_adapter.default_llm_adapter) | ||
| if default_adapter.default_vector_db_adapter: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @johnyrahul need to handle similarly for x2text also |
||
| adapters.append(default_adapter.default_vector_db_adapter) | ||
|
|
||
| return adapters | ||
| except ObjectDoesNotExist as e: | ||
| logger.error(f"No default adapters found: {e}") | ||
|
|
||
72 changes: 72 additions & 0 deletions
72
...adapter_processor/migrations/0007_remove_adapterinstance_is_default_userdefaultadapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Generated by Django 4.2.1 on 2024-03-12 12:00 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ("adapter_processor", "0006_adapterinstance_shared_users"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RemoveField( | ||
| model_name="adapterinstance", | ||
| name="is_default", | ||
| ), | ||
| migrations.CreateModel( | ||
| name="UserDefaultAdapter", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("modified_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "default_embedding_adapter", | ||
| models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="default_embedding_adapter", | ||
| to="adapter_processor.adapterinstance", | ||
| ), | ||
| ), | ||
| ( | ||
| "default_llm_adapter", | ||
| models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="default_llm_adapter", | ||
| to="adapter_processor.adapterinstance", | ||
| ), | ||
| ), | ||
| ( | ||
| "default_vector_db_adapter", | ||
| models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="default_vector_db_adapter", | ||
| to="adapter_processor.adapterinstance", | ||
| ), | ||
| ), | ||
| ( | ||
| "user", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| ] |
26 changes: 26 additions & 0 deletions
26
backend/adapter_processor/migrations/0008_userdefaultadapter_default_x2text_adapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Generated by Django 4.2.1 on 2024-03-13 06:16 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ( | ||
| "adapter_processor", | ||
| "0007_remove_adapterinstance_is_default_userdefaultadapter", | ||
| ), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="userdefaultadapter", | ||
| name="default_x2text_adapter", | ||
| field=models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="default_x2text_adapter", | ||
| to="adapter_processor.adapterinstance", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnyrahul don't we need to handle this for X2Text / OCR as well?