From 1f65921bf72334b54f500c0bcc3478c08ab3c257 Mon Sep 17 00:00:00 2001
From: Marco Frailis <marco.frailis@inaf.it>
Date: Sat, 13 Oct 2018 22:48:37 +0200
Subject: [PATCH] Fixing some constraints

---
 db.sqlite3              | Bin 180224 -> 188416 bytes
 imagedb/models.py       |   5 ++
 imagedb_objects.ipynb   | 116 ++++++++++++++++++++++++++++++----------
 orm_example/settings.py |   2 +-
 4 files changed, 94 insertions(+), 29 deletions(-)

diff --git a/db.sqlite3 b/db.sqlite3
index 84a7f50e08912e8476c470186a338b42043db3fe..49efc9c42bba66b81ac13a41bfbcfffeeaa2429c 100644
GIT binary patch
delta 1065
zcmZo@;BGj;JwaN~h=GAYj{yOKCh8b78f{G2EG=tfU}a)%WniFZW@KSzWNyUI${?-k
zn3<cHo|=+0d7->GKSG6}sezf%<S-d4gqW#?frY8z<oPm|NMfdzrWQt%pUc=Gi5VN1
zSQt$<ku^aQGc+_aHJMy4Yl<XhU}kJ#IC;0MF+$A5+{nP#a`InUGbAx%GYb>b$xd?C
zNMc5Y#wMnddr-s-%q`8$Cts7ZL8vn}H#D*|ovbKtA`SPOv9Y<ik)Z)ID}%VIV`55f
zW*(~FjSY<rjZ7v_lUG8RV`OP=X>2z6xV)A++#Dk_a}#q5UjE4p9Q-!S{4e=;@!Rk(
z<!|NB;Sc4%w6Rfzf3l5!I1>ZI<f;0$;-dV_hMdr_j?c_1E-5O_P0cIW{8C?h10&aF
z#)SX!iwXoFXj1{Bz;;FlM&<)-jQk%M_&;nGOnAmGz{kkKAS&tS864nRnw*oFvZz2|
zQGozRu`YW#13!Z{1K%$`6^`E=n>aGrblJD?ma;l9&0_h@xP##kV>N>|I8ii;xK95Y
z$Yjf?H+_Bvqv-U2ASP!<=jl^|m^2u5rtb}6>SJ|b6ZhAge$bmyVtPg}lN`5hW4sf)
zc)clOllb)dSSBfzl+=>c<dXa%&y@J0#B#Ty#N5>Q%#?T|BNG!7;}o;_(!9*V$%XnN
zqWsQ5u8twD3ZZ_UfuXJno_;Q_5egs~jd)_rQ1JJgUKqwCC5KR~q@$pOWT%p5OPs^>
zu3#nuMy>6;gPHW0SRL8K!wn&>zYxL1F`X}rNv>YIG0UDqypi3Iu}M8MFD11CGbWHC
z#Si46lGL2k;^h2-R4~uNB+We4A}JN@H%!f%*gT3*W(JFH5-b6EeohRNqyjvxfE)ty
sKZ?tgG`H)9F<sW2RKUW<$p4Xn|Kn!Cgy;PHd@Q1poJeUU!2puB07`B%kpKVy

delta 588
zcmZoTz}?WmJwaN~kb!|ghXDZsCh8b78g5M3EG=tjYGq(yWn!dfVQOh<Zf3;K${?-k
zn3<cHo|=+0d7->GKSG6xsiBd@<S-d4Br!u13j?#s^JOfN#0(4#%?u|$m$5^LnOm3}
znHx_wku^aQGdHv_FrHj4Yl<XhW^QU~I(fIOF_M^xsfn4{<iE0JNMc5oCPo&Mo#d>M
z#0*RgEDb03Ad8t<8X1}xPQE5*gHUH?ZeU<yGFegHL>lflGZSMo3j+gYRt9lZ$HbJ}
z%sf=Tn;DrIm>Nu;Ca;8Kj)A4Aq2=V`@>;5Jb4<;REX@sg`6n~5^V=}-zvSP=Z^OTo
zzm-3SKXhZG8vkS){ctA!i<777+cGmSNKAAT+5A#pe8Zvyg+&Dd5VWa)QD8fx10(Z+
z%`6Fj^cNKffJ7A8%NckXe=+d=;#1-H&9R9ilTDX>3vVfNJL?OkiOgP%zcx1BU}SC-
zah|Rhz+^l9Z2%L;^ko^0eA62Pn4B3MrymVq(qQDD{w{#2kI`v*Z6K2si*`$#{q(s(
mOa_da+s_3t=`k@nO#d6g<ix18T{n#BvgV=&sFM~I2mk;(A(xc^

diff --git a/imagedb/models.py b/imagedb/models.py
index a2a487e..06cc9da 100644
--- a/imagedb/models.py
+++ b/imagedb/models.py
@@ -75,6 +75,9 @@ class ImageBaseFrame(models.Model):
 class Instrument(models.Model):
   instrumentName = models.CharField(max_length=100)
   telescopeName = models.CharField(max_length=100)
+  
+  class Meta:
+    unique_together = (('instrumentName', 'telescopeName'))
 
 
 class Pointing(CompositeField):
@@ -114,6 +117,8 @@ class NispDetector(models.Model):
   rawFrame = models.ForeignKey('NispRawFrame',
                                related_name='detectors',
                                on_delete=models.CASCADE)
+  class Meta:
+    unique_together = (('detectorId', 'rawFrame'))
 
 
 WCS_COORDINATE_TYPE = (
diff --git a/imagedb_objects.ipynb b/imagedb_objects.ipynb
index 6dff79e..2a2f3ea 100644
--- a/imagedb_objects.ipynb
+++ b/imagedb_objects.ipynb
@@ -1,14 +1,12 @@
 {
  "cells": [
   {
-   "cell_type": "code",
-   "execution_count": 1,
+   "cell_type": "markdown",
    "metadata": {},
-   "outputs": [],
    "source": [
-    "from imagedb.models import Instrument\n",
+    "## Data insertion and retrieval with Django models\n",
     "\n",
-    "instrument = Instrument.objects.get(instrumentName='NISP')\n"
+    "In the following we show some examples in order to perform insertions and retrievals of metadata "
    ]
   },
   {
@@ -20,24 +18,37 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "NISP\n"
+      "Euclid\n"
      ]
     }
    ],
    "source": [
-    "print(instrument.instrumentName)"
+    "from imagedb.models import Instrument\n",
+    "\n",
+    "instrument = Instrument.objects.get(instrumentName='NISP')\n",
+    "\n",
+    "print(instrument.telescopeName)\n"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 18,
    "metadata": {},
    "outputs": [],
    "source": [
-    "from imagedb.models import ImageType, Pointing, NispDetector, NispRawFrame\n",
+    "from imagedb.models import ImageType, Pointing, NispDetector, DataContainer, NispRawFrame\n",
     "\n",
     "from datetime import datetime\n",
-    "                 \n",
+    "\n",
+    "dataFile = DataContainer(\n",
+    "  fileFormat = 'fits',\n",
+    "  formatIdentifier = 'le1.nisprawframe',\n",
+    "  formatVersion = '1.0',\n",
+    "  url = \"http://ia2-ownclud.oats.inaf.it/fake/7ff2f203/data/EUC_LE1_NISP_53892-Y-1_20170712T155430.1Z_00.00.fits\"\n",
+    ")\n",
+    "\n",
+    "# We have to save the data container to the DB before assigning it to a NispRawFrame\n",
+    "dataFile.save()\n",
     "\n",
     "image = NispRawFrame(exposureTime = 105,\n",
     "                     imgNumber = 16,\n",
@@ -48,72 +59,121 @@
     "                                  'secondType':'STD'},\n",
     "                     observationDateTime = datetime.strptime(\"2025-06-21T18:27:23.000001\", \n",
     "                                                             \"%Y-%m-%dT%H:%M:%S.%f\"),\n",
+    "                     observationId = 53892,\n",
+    "                     ditherNumber = 1,\n",
     "                     instrument = instrument,\n",
     "                     commandedPointing = {'rightAscension':8.48223045516,\n",
     "                                          'declination':8.48223045516,\n",
-    "                                          'pointingAngle':64.8793517547},\n",
+    "                                          'orientation':64.8793517547},\n",
     "                     filterWheelPosition = \"Y\",\n",
     "                     grismWheelPosition = \"OPEN\"\n",
-    "                    )\n"
+    "                    )\n",
+    "\n",
+    "\n",
+    "\n",
+    "image.frameFile = dataFile"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 19,
    "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "None\n",
+      "3\n"
+     ]
+    }
+   ],
+   "source": [
+    "print(image.id)\n",
+    "image.save()\n",
+    "print(image.id)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 30,
+   "metadata": {
+    "scrolled": true
+   },
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "Pointing(rightAscension=8.48223045516, declination=8.48223045516, pointingAngle=64.8793517547)"
+       "<NispDetector: NispDetector object (4)>"
       ]
      },
-     "execution_count": 4,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "image.commandedPointing"
+    "# We can start creating a detector\n",
+    "\n",
+    "d11 = NispDetector(detectorId = \"11\", gain = 1.0, readoutNoise = 0.0, rawFrame = image)\n",
+    "d11.save()\n",
+    "\n",
+    "# or we can create the detector starting from the NispRawFrame, using the reversed relationship.\n",
+    "# No need to save the detector in this case. It is done automatically\n",
+    "\n",
+    "image.detectors.create(\n",
+    "  detectorId = \"12\",\n",
+    "  gain = 1.0,\n",
+    "  readoutNoise = 0.0\n",
+    ")"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "8.48223045516"
+       "<QuerySet [<NispDetector: NispDetector object (1)>, <NispDetector: NispDetector object (2)>]>"
       ]
      },
-     "execution_count": 5,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "image.commandedPointing.rightAscension"
+    "image.detectors.all()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 27,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "<QuerySet [<NispDetector: NispDetector object (1)>]>"
+      ]
+     },
+     "execution_count": 27,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
-    "image = NispRawFrame.objects.filter(commandedPointing_rightAscension__lte=8.48223045516)[0]"
+    "image.detectors.all()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": null,
    "metadata": {},
    "outputs": [],
-   "source": [
-    "d = NispDetector(detectorId = \"11\", gain = 1.0, readoutNoise = 0.0, rawFrame = image)"
-   ]
+   "source": []
   }
  ],
  "metadata": {
@@ -132,7 +192,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.6.3"
+   "version": "3.7.0"
   }
  },
  "nbformat": 4,
diff --git a/orm_example/settings.py b/orm_example/settings.py
index c651d61..d9fde8d 100644
--- a/orm_example/settings.py
+++ b/orm_example/settings.py
@@ -121,7 +121,7 @@ USE_I18N = True
 
 USE_L10N = True
 
-USE_TZ = True
+USE_TZ = False
 
 
 # Static files (CSS, JavaScript, Images)
-- 
GitLab