start choice field refactor, remove deprecated model fields

This commit is contained in:
wh1te909
2022-04-25 07:10:33 +00:00
parent d11e14ad89
commit 7d9a8decf0
9 changed files with 48 additions and 18 deletions

View File

@@ -38,10 +38,10 @@ def agent_update(agent_id: str, force: bool = False) -> str:
if not force:
if agent.pendingactions.filter(
action_type="agentupdate", status="pending"
action_type="agentupdate", status=PendingAction.PENDING
).exists():
agent.pendingactions.filter(
action_type="agentupdate", status="pending"
action_type="agentupdate", status=PendingAction.PENDING
).delete()
PendingAction.objects.create(

View File

@@ -120,7 +120,8 @@ class GetAgents(APIView):
)
.annotate(
pending_actions_count=Count(
"pendingactions", filter=Q(pendingactions__status="pending")
"pendingactions",
filter=Q(pendingactions__status=PendingAction.PENDING),
)
)
.annotate(

View File

@@ -58,7 +58,7 @@ def handle_resolved_stuff() -> None:
actions = (
PendingAction.objects.select_related("agent")
.defer("agent__services", "agent__wmi_detail")
.filter(action_type="agentupdate", status="pending")
.filter(action_type="agentupdate", status=PendingAction.PENDING)
)
to_update = [
@@ -68,7 +68,9 @@ def handle_resolved_stuff() -> None:
and action.agent.status == "online"
]
PendingAction.objects.filter(pk__in=to_update).update(status="completed")
PendingAction.objects.filter(pk__in=to_update).update(
status=PendingAction.COMPLETED
)
agent_queryset = (
Agent.objects.defer(*AGENT_DEFER)

View File

@@ -411,10 +411,10 @@ class TestCoreTasks(TacticalTestCase):
handle_resolved_stuff()
complete = PendingAction.objects.filter(
action_type="agentupdate", status="completed"
action_type="agentupdate", status=PendingAction.COMPLETED
).count()
old = PendingAction.objects.filter(
action_type="agentupdate", status="pending"
action_type="agentupdate", status=PendingAction.PENDING
).count()
self.assertEqual(complete, 20)

View File

@@ -137,7 +137,9 @@ def server_maintenance(request):
auditlogs.delete()
if "pending_actions" in tables:
pendingactions = PendingAction.objects.filter(status="completed")
pendingactions = PendingAction.objects.filter(
status=PendingAction.COMPLETED
)
records_count += pendingactions.count()
pendingactions.delete()

View File

@@ -2,6 +2,8 @@ from itertools import cycle
from model_bakery.recipe import Recipe
from logs.models import PendingAction
object_types = [
"user",
"script",
@@ -28,5 +30,5 @@ object_logs = Recipe(
login_logs = Recipe("logs.AuditLog", action=cycle(login_actions), object_type="user")
pending_agentupdate_action = Recipe(
"logs.PendingAction", action_type="agentupdate", status="pending"
"logs.PendingAction", action_type="agentupdate", status=PendingAction.PENDING
)

View File

@@ -0,0 +1,21 @@
# Generated by Django 4.0.4 on 2022-04-25 06:59
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('logs', '0023_alter_pendingaction_action_type'),
]
operations = [
migrations.RemoveField(
model_name='pendingaction',
name='cancelable',
),
migrations.RemoveField(
model_name='pendingaction',
name='celery_id',
),
]

View File

@@ -63,11 +63,6 @@ AUDIT_OBJECT_TYPE_CHOICES = [
("customfield", "Custom Field"),
]
STATUS_CHOICES = [
("pending", "Pending"),
("completed", "Completed"),
]
class AuditLog(models.Model):
username = models.CharField(max_length=255)
@@ -385,6 +380,14 @@ class DebugLog(models.Model):
class PendingAction(models.Model):
PENDING = "pending"
COMPLETED = "completed"
STATUS_CHOICES = (
(PENDING, "Pending"),
(COMPLETED, "Completed"),
)
objects = PermissionQuerySet.as_manager()
agent = models.ForeignKey(
@@ -399,10 +402,8 @@ class PendingAction(models.Model):
status = models.CharField(
max_length=255,
choices=STATUS_CHOICES,
default="pending",
default=PENDING,
)
cancelable = models.BooleanField(blank=True, default=False)
celery_id = models.CharField(null=True, blank=True, max_length=255)
details = models.JSONField(null=True, blank=True)
def __str__(self) -> str:

View File

@@ -5,6 +5,7 @@ from django.utils import timezone as djangotime
from model_bakery import baker, seq
from tacticalrmm.test import TacticalTestCase
from .models import PendingAction
base_url = "/logs"
@@ -176,7 +177,7 @@ class TestAuditViews(TacticalTestCase):
"logs.PendingAction",
agent=agent2,
action_type="chocoinstall",
status="completed",
status=PendingAction.COMPLETED,
details={"name": "adobereader", "output": None, "installed": False},
_quantity=14,
)