-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Empty file.
This file contains 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,11 @@ | ||
from django.contrib import admin | ||
from .models import Context | ||
|
||
|
||
class ContextAdmin(admin.ModelAdmin): | ||
list_display = ("id", "key", "value", "created_at", "updated_at") | ||
search_fields = ("key", "value") | ||
list_filter = ("key",) | ||
|
||
|
||
admin.site.register(Context, ContextAdmin) |
This file contains 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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class MetaConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "itsm.meta" |
This file contains 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,34 @@ | ||
# Generated by Django 3.2.25 on 2024-12-06 14:55 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Context", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("key", models.CharField(max_length=255, unique=True)), | ||
("value", models.TextField(blank=True)), | ||
], | ||
options={ | ||
"db_table": "meta_context", | ||
}, | ||
), | ||
] |
Empty file.
This file contains 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,45 @@ | ||
from django.core.cache import cache | ||
from django.db import models | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
|
||
class Context(models.Model): | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
key = models.CharField(max_length=255, unique=True) | ||
value = models.TextField(blank=True) | ||
|
||
def __str__(self): | ||
return self.key | ||
|
||
class Meta: | ||
db_table = "meta_context" | ||
|
||
|
||
@receiver(post_save, sender=Context) | ||
def update_cache(sender, instance, **kwargs): | ||
cache_key = f"meta_context_{instance.key}" | ||
cache.set(cache_key, instance.value, 30) | ||
|
||
|
||
class ContextService: | ||
@staticmethod | ||
def get_context_value(key): | ||
cache_key = f"meta_context_{key}" | ||
context_value = cache.get(cache_key) | ||
if not context_value: | ||
try: | ||
context_value = Context.objects.get(key=key).value | ||
except Context.DoesNotExist: | ||
context_value = "" | ||
cache.set(cache_key, context_value, 30) | ||
return context_value | ||
|
||
@staticmethod | ||
def get_context_value_list(key): | ||
context_value = ContextService.get_context_value(key) | ||
context_value = ( | ||
[item.strip() for item in context_value.split(",")] if context_value else [] | ||
) | ||
return context_value |
This file contains 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