Skip to content

Commit dcfe5f0

Browse files
author
Glass
authored
Merge pull request #37 from neutrons/keep-user
Keep user data
2 parents 704f0e8 + 05f1dd5 commit dcfe5f0

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed

src/apps/plots/migrations/0002_datarun_expiration_date.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22

33
import datetime
44

5+
from django.conf import settings
56
from django.db import migrations, models
7+
from django.utils import timezone
8+
9+
from config.instruments import Instruments
10+
11+
12+
def set_expiration_date(apps, _):
13+
DataRun = apps.get_model("plots", "DataRun")
14+
for run in DataRun.objects.all():
15+
if Instruments.has_value(run.instrument.name):
16+
run.expiration_date = run.created_on + datetime.timedelta(days=settings.LIVE_PLOT_EXPIRATION_TIME)
17+
else:
18+
run.expiration_date = timezone.datetime(2100, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
19+
run.save()
620

721

822
class Migration(migrations.Migration):
@@ -14,9 +28,7 @@ class Migration(migrations.Migration):
1428
migrations.AddField(
1529
model_name="datarun",
1630
name="expiration_date",
17-
field=models.DateTimeField(
18-
default=datetime.datetime(2027, 8, 8, 18, 55, 41, 999298, tzinfo=datetime.timezone.utc),
19-
verbose_name="Expires",
20-
),
31+
field=models.DateTimeField(default=None, blank=True, null=True, verbose_name="Expires"),
2132
),
33+
migrations.RunPython(set_expiration_date),
2234
]

src/apps/plots/models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from django.db import models
1111
from django.utils import timezone
1212

13+
from config.instruments import Instruments
14+
1315
DATA_TYPES = {"json": 0, "html": 1, "div": 1}
1416
DATA_TYPE_INFO = {0: {"name": "json"}, 1: {"name": "html"}}
1517

@@ -41,9 +43,14 @@ class DataRun(models.Model):
4143
run_id = models.TextField()
4244
instrument = models.ForeignKey(Instrument, on_delete=models.deletion.CASCADE)
4345
created_on = models.DateTimeField("Timestamp", auto_now_add=True)
44-
expiration_date = models.DateTimeField(
45-
"Expires", default=timezone.now() + timedelta(days=(settings.LIVE_PLOT_EXPIRATION_TIME))
46-
)
46+
expiration_date = models.DateTimeField("Expires", default=None, null=True, blank=True)
47+
48+
def clean(self):
49+
if self.expiration_date is None:
50+
if Instruments.has_value(self.instrument.name):
51+
self.expiration_date = self.created_on + timedelta(days=settings.LIVE_PLOT_EXPIRATION_TIME)
52+
else:
53+
self.expiration_date = timezone.datetime(2100, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
4754

4855
def __str__(self):
4956
return f"{self.instrument}_{self.run_number}_{self.run_id}"

src/apps/plots/view_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ def store_user_data(user, data_id, data, data_type, expiration_date: Optional[da
144144
run_obj.run_number = 0
145145
run_obj.run_id = data_id
146146
run_obj.expiration_date = expiration_date
147+
# Save run object to generate id (primary key)
147148
run_obj.save()
148-
# Since user data have no run number, force the run number to be the PK,
149-
# which is unique and will allow user to retrieve the data like normal
150-
# instrument data.
149+
# User data has no run number, use the unique id as the run number
150+
# so that the user can retrieve the data like normal instrument data
151151
run_obj.run_number = run_obj.id
152152
run_obj.save()
153153

src/apps/plots/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def _store(request, instrument, run_id=None, as_user=False):
9999
data_type_default = PlotData.get_data_type_from_data(raw_data)
100100
data_type = request.POST.get("data_type", default=data_type_default)
101101
expiration_date = request.POST.get(
102-
"expiration_date", default=timezone.now() + timedelta(days=settings.LIVE_PLOT_EXPIRATION_TIME)
102+
"expiration_date",
103+
default=timezone.datetime(2100, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
104+
if as_user
105+
else timezone.now() + timedelta(days=settings.LIVE_PLOT_EXPIRATION_TIME),
103106
)
104107

105108
if as_user:

src/config/instruments.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from enum import StrEnum
2+
3+
4+
class Instruments(StrEnum):
5+
ARCS = "arcs"
6+
CG2 = "cg2"
7+
CNCs = "cncs"
8+
CORELLI = "corelli"
9+
EQSANS = "eqsans"
10+
HB2A = "hb2a"
11+
HB2B = "hb2b"
12+
HB2C = "hb2c"
13+
HB3A = "hb3a"
14+
HYS = "hys"
15+
MANDI = "mandi"
16+
NOM = "nom"
17+
PG3 = "pg3"
18+
REF_L = "ref_l"
19+
REF_M = "ref_m"
20+
SEQ = "seq"
21+
SNAP = "snap"
22+
TOPAZ = "topaz"
23+
USANS = "usans"
24+
VULCAN = "vulcan"
25+
# instruments that haven't published to livedata yet
26+
BL0 = "bl0"
27+
BSS = "bss"
28+
CG1D = "cg1d"
29+
CG3 = "cg3"
30+
FNPB = "fnpb"
31+
HB3 = "hb3"
32+
NOWB = "nowb"
33+
NOWD = "nowd"
34+
NOWG = "nowg"
35+
NOWV = "nowv"
36+
NOWX = "nowx"
37+
NSE = "nse"
38+
VENUS = "venus"
39+
VIS = "vis"
40+
41+
@classmethod
42+
def has_value(cls, value):
43+
return any(value == item.value for item in cls)

tests/test_enum.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
sys.path.append(str(Path(__file__).parents[1]))
7+
from src.config.instruments import Instruments
8+
9+
10+
@pytest.mark.parametrize("instrument", ["fake_instrument", "ref_m"])
11+
def test_enum(instrument):
12+
assert Instruments.has_value(instrument) == (instrument == "ref_m")

0 commit comments

Comments
 (0)