Compare commits

..

No commits in common. "d1cd44f8f66f864b134053e96aeedf4383d4e05f" and "b4c4ed2689b9ff74310a8b9d2d20fdda6a57d5c2" have entirely different histories.

6 changed files with 13 additions and 68 deletions

View File

@ -1,10 +1,7 @@
import json
from uuid import uuid4
from django.conf import settings
from django.urls import reverse_lazy
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
@ -16,8 +13,9 @@ from django.views.generic.edit import (
DeleteView,
UpdateView,
)
from django.http import JsonResponse
from uuid import uuid4
from utils.save_snapshots import move_json, save_in_disk
from dashboard.mixins import DashboardView
from evidence.models import Annotation
from evidence.parse import Build
@ -26,6 +24,10 @@ from api.models import Token
from api.tables import TokensTable
def save_in_disk(data, user):
pass
@csrf_exempt
def NewSnapshot(request):
# Accept only posts
@ -58,11 +60,10 @@ def NewSnapshot(request):
).first()
if exist_annotation:
txt = "error: the snapshot {} exist".format(data['uuid'])
return JsonResponse({'status': txt}, status=500)
raise ValidationError("error: the snapshot {} exist".format(data['uuid']))
# Process snapshot
path_name = save_in_disk(data, tk.owner.institution.name)
# save_in_disk(data, tk.user)
try:
Build(data, tk.owner)
@ -92,8 +93,6 @@ def NewSnapshot(request):
"url": url,
"public_url": url
}
move_json(path_name, tk.owner.institution.name)
return JsonResponse(response, status=200)

View File

@ -1,6 +1,5 @@
from django import forms
from utils.device import create_annotation, create_doc, create_index
from utils.save_snapshots import move_json, save_in_disk
DEVICE_TYPES = [
@ -56,12 +55,9 @@ class BaseDeviceFormSet(forms.BaseFormSet):
doc = create_doc(row)
if not commit:
return doc
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
create_index(doc, self.user)
create_annotation(doc, user, commit=commit)
move_json(path_name, self.user.institution.name, place="placeholder")
return doc

View File

@ -10,7 +10,6 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
import os
import xapian
from pathlib import Path
@ -63,7 +62,6 @@ EMAIL_FILE_PATH = config('EMAIL_FILE_PATH', default='/tmp/app-messages')
ENABLE_EMAIL = config("ENABLE_EMAIL", default=True, cast=bool)
EVIDENCES_DIR = config("EVIDENCES_DIR", default=os.path.join(BASE_DIR, "db"))
# Application definition

View File

@ -9,7 +9,6 @@ from utils.forms import MultipleFileField
from device.models import Device
from evidence.parse import Build
from evidence.models import Annotation
from utils.save_snapshots import move_json, save_in_disk
class UploadForm(forms.Form):
@ -49,9 +48,7 @@ class UploadForm(forms.Form):
return
for ev in self.evidences:
path_name = save_in_disk(ev[1], user.institution.name)
Build(ev[1], user)
move_json(path_name, user.institution.name)
class UserTagForm(forms.Form):
@ -154,11 +151,8 @@ class ImportForm(forms.Form):
if commit:
for doc, cred in table:
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
cred.save()
create_index(doc, self.user)
move_json(path_name, self.user.institution.name, place="placeholder")
return table
cred.save()
create_index(doc, self.user)
return table
return

View File

@ -65,6 +65,7 @@ class PasswordResetView(auth_views.PasswordResetView):
success_url = reverse_lazy('login:password_reset_done')
def form_valid(self, form):
import pdb; pdb.set_trace()
try:
response = super().form_valid(form)
return response

View File

@ -1,43 +0,0 @@
import os
import json
import shutil
from datetime import datetime
from django.conf import settings
def move_json(path_name, user, place="snapshots"):
if place != "snapshots":
place = "placeholders"
tmp_snapshots = settings.EVIDENCES_DIR
path_dir = os.path.join(tmp_snapshots, user, place)
if os.path.isfile(path_name):
shutil.copy(path_name, path_dir)
os.remove(path_name)
def save_in_disk(data, user, place="snapshots"):
uuid = data.get('uuid', '')
now = datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minutes = now.minute
tmp_snapshots = settings.EVIDENCES_DIR
if place != "snapshots":
place = "placeholders"
name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{uuid}.json"
path_dir = os.path.join(tmp_snapshots, user, place, "errors")
path_name = os.path.join(path_dir, name_file)
if not os.path.isdir(path_dir):
os.system(f'mkdir -p {path_dir}')
with open(path_name, 'w') as snapshot_file:
snapshot_file.write(json.dumps(data))
return path_name