Commit 4c26635c authored by cermak's avatar cermak
Browse files

Add Log models

parent ad76d9c4
......@@ -5,7 +5,7 @@ from django.conf.urls import url
from django.utils.html import strip_tags
from django.urls import reverse
from django.http import HttpResponseRedirect
from .models import Proposals, Instruments, Contacts, Affiliations, Countries, Options, SharedOptions, Samples, SamplePhotos, SampleRemarks, Publication, Experiments, Status, InstrumentGroup, SharedOptionSlot, Report
from .models import *
import requests
import datetime
......@@ -290,3 +290,31 @@ class SharedOptionSlotAdmin(admin.ModelAdmin):
admin.site.register(SharedOptionSlot, SharedOptionSlotAdmin)
class UsageInline(admin.TabularInline):
model = Usage
extra = 1
class LogAdminForm(forms.ModelForm):
class Meta:
model = Log
fields = '__all__'
class LogAdmin(admin.ModelAdmin):
form = LogAdminForm
list_display = ['created', 'last_updated', 'proposal', 'instrument','start', 'end', 'duration']
inlines = (UsageInline,)
admin.site.register(Log, LogAdmin)
class ResourceAdminForm(forms.ModelForm):
class Meta:
model = Resource
fields = '__all__'
class ResourceAdmin(admin.ModelAdmin):
form = ResourceAdminForm
list_display = ['name', 'unit']
admin.site.register(Resource, ResourceAdmin)
# Generated by Django 3.0.6 on 2020-06-09 08:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0051_auto_20200507_1245'),
]
operations = [
migrations.CreateModel(
name='Log',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('last_updated', models.DateTimeField(auto_now=True)),
('start', models.DateTimeField()),
('end', models.DateTimeField()),
('duration', models.DurationField(editable=False)),
('creator', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='app.Contacts')),
('instrument', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='app.Instruments')),
('localcontact', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='log_as_lc', to='app.Contacts')),
('proposal', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='app.Proposals')),
],
options={
'ordering': ('-created',),
},
),
migrations.CreateModel(
name='Resource',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=300)),
('unit', models.CharField(max_length=300)),
],
options={
'ordering': ('name',),
},
),
migrations.AlterField(
model_name='publication',
name='authors',
field=models.ManyToManyField(blank=True, to='app.Contacts'),
),
migrations.AlterField(
model_name='publication',
name='citations',
field=models.IntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='publication',
name='issued',
field=models.DateField(blank=True, db_index=True, null=True),
),
migrations.CreateModel(
name='Usage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('amount', models.IntegerField()),
('log', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='app.Log')),
('resource', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='app.Resource')),
],
),
migrations.AddField(
model_name='log',
name='resources',
field=models.ManyToManyField(blank=True, through='app.Usage', to='app.Resource'),
),
]
......@@ -838,3 +838,49 @@ class Publication(models.Model):
def get_update_url(self):
return reverse('app_publication_update', args=(self.pk,))
class Log(models.Model):
# Fields
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
start = models.DateTimeField()
end = models.DateTimeField()
duration = models.DurationField(editable=False)
# Relationship Fields
proposal = models.ForeignKey('app.Proposals', on_delete=models.PROTECT)
creator = models.ForeignKey('app.Contacts', on_delete=models.PROTECT)
localcontact = models.ForeignKey('app.Contacts', on_delete=models.PROTECT, related_name='log_as_lc')
instrument = models.ForeignKey('app.Instruments', on_delete=models.PROTECT)
resources = models.ManyToManyField('app.Resource', blank=True, through='Usage')
class Meta:
ordering = ('-created',)
def __str__(self):
return f'{self.instrument} ({self.proposal.pid}, {self.end})'
def save(self, *args, **kwargs):
# calculate duration
self.duration = self.end - self.start
super().save(*args, **kwargs)
class Usage(models.Model):
amount = models.IntegerField()
resource = models.ForeignKey('app.Resource', on_delete=models.PROTECT)
log = models.ForeignKey('app.Log', on_delete=models.PROTECT)
class Resource(models.Model):
name = models.CharField(max_length=300)
unit = models.CharField(max_length=300)
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment