41 Commits

Author SHA1 Message Date
49b7aac807 Run black 2022-07-16 21:00:17 +02:00
24af738ad4 Fix page range 2022-07-16 20:57:29 +02:00
8136a180ab Try another fix for ellipsis 2022-07-16 19:56:08 +02:00
908790c3e0 Try a fix for ellipsis 2022-07-16 19:46:33 +02:00
44cdb8b09f Refactor paginator and add ellipsis 2022-07-16 18:57:46 +02:00
7d3f29e734 Use int sort for road numbers 2022-07-16 17:48:04 +02:00
65b615ae63 Validate search 2022-07-15 22:26:37 +02:00
66a85b4ed7 Add tag based filtering 2022-07-15 21:31:40 +02:00
70c12c69b2 Improve road number sorting and enforce company on consists 2022-07-15 18:10:18 +02:00
e55f953c8a Add sorting, enforce foreign keys 2022-07-15 17:28:44 +02:00
273225f919 Default if none in template 2022-07-13 21:43:50 +02:00
1296c4e663 Default if none in template 2022-07-13 21:42:33 +02:00
bbecdd54bb Remove a pdb leftover 2022-07-13 20:41:48 +02:00
899e33da61 Move properties under model and class sections 2022-07-13 20:40:42 +02:00
1f4966ad49 Add missing swagger template 2022-07-13 20:39:49 +02:00
c7dfb84632 Hide 'About' when no text is provided 2022-07-12 21:56:07 +02:00
4e3a13162a Fix navbar on mobile 2022-07-12 21:17:29 +02:00
74431c5d94 Remove thumbnail padding 2022-07-12 21:01:17 +02:00
cb2ff90d8a Review roster API 2022-07-12 19:00:50 +02:00
2dbe01d8bd Add private properties and consist thumbnail 2022-07-12 19:00:29 +02:00
47b4b2915b Add an option to completely disable driver 2022-07-11 20:56:31 +02:00
49d28b176e Rename a logo file 2022-07-11 18:14:24 +02:00
0e30740366 Fix sample data 2022-07-11 18:12:45 +02:00
7c3fee4127 Limit decoders selection to model manufacturers 2022-07-10 23:48:10 +02:00
1f96ff8833 Metadata pretty print 2022-07-10 23:44:18 +02:00
e76a1ea6b2 Add some sample metadata 2022-07-10 23:42:13 +02:00
565028de72 Add support for local_settings.py 2022-07-10 23:31:37 +02:00
7c88983cba More url generation fixes 2022-07-10 23:19:21 +02:00
51eb9ba2a2 Fix main redirection 2022-07-10 22:56:27 +02:00
b19ea23fa6 Update footer and version 2022-07-10 22:52:54 +02:00
353da224be Fix consists pagination and remove hardcoded urls 2022-07-10 22:48:45 +02:00
d39cd47280 Update arduino-cli to 0.24.0 2022-07-10 22:41:54 +02:00
a67ea83068 Update README.md 2022-06-25 22:48:04 +02:00
410fcd8626 Update README.md 2022-06-25 22:47:34 +02:00
05eb0909c8 Update README.md 2022-06-25 22:46:16 +02:00
8369ffce46 Update README.md 2022-05-20 21:16:11 +02:00
818db8c6e3 Update README.md 2022-05-20 21:15:55 +02:00
9267d78815 Update README.md 2022-05-11 22:21:23 +02:00
de9910936a Update README.md 2022-04-24 00:12:02 +02:00
ea78c81f79 Update README.md 2022-04-24 00:10:53 +02:00
cb7a86c977 Update README 2022-04-24 00:02:06 +02:00
51 changed files with 1069 additions and 169 deletions

View File

@@ -2,22 +2,25 @@
[![Django CI](https://github.com/daniviga/django-rma/actions/workflows/django.yml/badge.svg)](https://github.com/daniviga/django-rma/actions/workflows/django.yml)
![image](https://user-images.githubusercontent.com/1818657/175789825-9a03f0ff-a95e-42a2-9611-e14d2817e22f.png)
A `jff` (just for fun) project that aims to create a
model railroad assets manager that allows to:
- Create a database of assets (model trains) and consits with their metadata
- Create a database of assets (model trains) and consists with their metadata
- Manage the database via a simple but rationale backoffice
- Expose main data via an HTML interface to show how beautiful is your collection
to the outside world
- Act as a DCC++ EX REST API gateway to control assets remotely via DCC.
By anyone, if you'd like (really?).
By anyone, if you'd like (seriously?).
## Preface
Project is intended to have fun only and it has been developed with a
commitment of few minutes a day; it lacks any kind of documentation, code
review, architectural review, security assesment, pentest, ISO certification,
etc.
**This project is work in progress**. It is intended for fun only and
it has been developed with a commitment of few minutes a day;
it lacks any kind of documentation, code review, architectural review,
security assesment, pentest, ISO certification, etc.
This project probably doesn't match you needs nor expectations. Be aware.
@@ -30,20 +33,20 @@ Project is based on the following technologies and components:
- [Django](https://www.djangoproject.com/): *the* web framework
- [Django REST](https://www.django-rest-framework.org/): API for the lazy
- [Bootstrap](https://getbootstrap.com/): for the web frontend
- [Arduino](https://arduino.cc): DCC hardware
- [Arduino](https://arduino.cc): DCC hardware; you must get one, really
- [DCC++ EX Command Station](https://dcc-ex.com/): DCC firmware; an amazing project
- [DCC++ EX WebThrottle](https://github.com/DCC-EX/WebThrottle-EX): a slighly modified version of the DCC++ EX web throttle
- [DCC++ EX WebThrottle](https://github.com/DCC-EX/WebThrottle-EX): the DCC++ EX web throttle, a slightly modified version
It has been developed with:
- [vim](https://www.vim.org/): because it rocks
- [arduino-cli](https://github.com/arduino/arduino-cli/): a mouse? What the hack?
- [arduino-cli](https://github.com/arduino/arduino-cli/): a mouse? What the heck?
- [vim-arduino](https://github.com/stevearc/vim-arduino): another IDE? No thanks
- [podman](https://podman.io/): because containers are fancy
- [QEMU (avr)](https://qemu-project.gitlab.io/qemu/system/target-avr.html): QEMU can even make toast!
## Requirments
## Requirements
- Python 3.8+
- A USB port when running Arduino hardware (and adaptors if you have a Mac)
@@ -132,10 +135,30 @@ $ podman run --init --cpus 0.1 -d -p 2560:2560 dcc/net-to-serial:sim
To be continued ...
## Screenshots
### Frontend
![image](https://user-images.githubusercontent.com/1818657/175789897-9ec4a9bb-9c65-48ef-9b57-ae94e094e6a7.png)
---
![image](https://user-images.githubusercontent.com/1818657/175789901-ef50acd7-8c05-4788-92a2-1bb1280d598c.png)
---
![image](https://user-images.githubusercontent.com/1818657/175790004-18926d23-28f9-45bb-b279-6c26575ae3a5.png)
---
![image](https://user-images.githubusercontent.com/1818657/175790008-62eea2cc-1c41-42df-9026-4cf6e8ef712c.png)
### Backoffice
![image](https://user-images.githubusercontent.com/1818657/175789937-3e4970a2-b37d-44c3-8605-62dabe209c65.png)
---
![image](https://user-images.githubusercontent.com/1818657/175789946-d7ce882c-1ba6-49b2-8e0a-1144e5c6bc35.png)
---
![image](https://user-images.githubusercontent.com/1818657/175789954-0735a4ea-bcaf-4a45-adbc-64105091b051.png)
### Rest API
![image](https://user-images.githubusercontent.com/1818657/175790064-23ec038e-e8bf-4c39-964c-3118e4295b59.png)

View File

@@ -1,6 +1,6 @@
# AVR Simulator
`qemu-system-avr` tries to use all the CPU cicles (leaving a CPU core stuck at 100%. Limit CPU core usage to 10%. It may be adjusted on slower machines.
`qemu-system-avr` tries to use all the CPU cicles (leaving a CPU core stuck at 100%; limit CPU core usage to 10% via `--cpus 0.1`. It can be adjusted on slower machines.
```bash
$ podman build -t dcc/net-to-serial:sim .

View File

@@ -31,6 +31,7 @@ class ConsistAdmin(SortableAdminBase, admin.ModelAdmin):
"consist_address",
"company",
"era",
"image",
"notes",
"tags",
)

View File

@@ -0,0 +1,17 @@
# Generated by Django 4.0.6 on 2022-07-10 13:19
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('consist', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='consist',
options={'ordering': ['creation_time']},
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-12 12:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('consist', '0002_alter_consist_options'),
]
operations = [
migrations.AddField(
model_name='consist',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='images/'),
),
]

View File

@@ -0,0 +1,20 @@
# Generated by Django 4.0.6 on 2022-07-15 16:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('metadata', '0004_alter_rollingstocktype_options_and_more'),
('consist', '0003_consist_image'),
]
operations = [
migrations.AlterField(
model_name='consist',
name='company',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='metadata.company'),
),
]

View File

@@ -13,20 +13,22 @@ class Consist(models.Model):
consist_address = models.SmallIntegerField(
default=None, null=True, blank=True
)
company = models.ForeignKey(
Company, on_delete=models.CASCADE, null=True, blank=True
)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
era = models.CharField(max_length=32, blank=True)
image = models.ImageField(upload_to="images/", null=True, blank=True)
notes = models.TextField(blank=True)
creation_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now=True)
def __str__(self):
return "{0}".format(self.identifier)
return "{0} {1}".format(self.company, self.identifier)
def get_absolute_url(self):
return reverse("consist", kwargs={"uuid": self.uuid})
class Meta:
ordering = ["creation_time"]
class ConsistItem(models.Model):
consist = models.ForeignKey(

View File

@@ -7,6 +7,10 @@ from driver.models import DriverConfiguration
@admin.register(DriverConfiguration)
class DriverConfigurationAdmin(SingletonModelAdmin):
fieldsets = (
(
"General configuration",
{"fields": ("enabled",)},
),
(
"Remote DCC-EX configuration",
{

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-11 18:31
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('driver', '0004_alter_driverconfiguration_remote_host'),
]
operations = [
migrations.AddField(
model_name='driverconfiguration',
name='enabled',
field=models.BooleanField(default=False),
),
]

View File

@@ -1,10 +1,11 @@
from django.db import models
from django.core.exceptions import ValidationError
from ipaddress import IPv4Address, IPv4Network
from ipaddress import IPv4Network
from solo.models import SingletonModel
class DriverConfiguration(SingletonModel):
enabled = models.BooleanField(default=False)
remote_host = models.GenericIPAddressField(
protocol="IPv4", default="192.168.4.1"
)

View File

@@ -34,9 +34,22 @@ def addresschecker(f):
return addresslookup
class IsEnabled(BasePermission):
def has_permission(self, request, view):
config = DriverConfiguration.get_solo()
# if driver is disabled, block all connections
if not config.enabled:
raise Http404
return True
class Firewall(BasePermission):
def has_permission(self, request, view):
config = DriverConfiguration.get_solo()
# if network is not configured, accept only read ops
if not config.network:
return request.method in SAFE_METHODS
@@ -61,7 +74,7 @@ class Test(APIView):
"""
parser_classes = [PlainTextParser]
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def get(self, request):
response = Connector().passthrough("<s>")
@@ -76,7 +89,7 @@ class SendCommand(APIView):
"""
parser_classes = [PlainTextParser]
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def put(self, request):
data = request.data
@@ -101,7 +114,7 @@ class Function(APIView):
Send "Function" commands to a valid DCC address
"""
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def put(self, request, address):
serializer = FunctionSerializer(data=request.data)
@@ -118,7 +131,7 @@ class Cab(APIView):
Send "Cab" commands to a valid DCC address
"""
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def put(self, request, address):
serializer = CabSerializer(data=request.data)
@@ -134,7 +147,7 @@ class Infra(APIView):
Send "Infra" commands to a valid DCC address
"""
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def put(self, request):
serializer = InfraSerializer(data=request.data)
@@ -150,7 +163,7 @@ class Emergency(APIView):
Send an "Emergency" stop, no matter the HTTP method used
"""
permission_classes = [IsAuthenticated | Firewall]
permission_classes = [IsEnabled & IsAuthenticated | Firewall]
def put(self, request):
Connector().emergency()

View File

@@ -1,4 +1,6 @@
from django.contrib import admin
from adminsortable2.admin import SortableAdminMixin
from metadata.models import (
Property,
Decoder,
@@ -54,7 +56,7 @@ class TagAdmin(admin.ModelAdmin):
@admin.register(RollingStockType)
class RollingStockTypeAdmin(admin.ModelAdmin):
class RollingStockTypeAdmin(SortableAdminMixin, admin.ModelAdmin):
list_display = ("__str__",)
list_filter = ("type", "category")
search_fields = list_display

View File

@@ -0,0 +1,19 @@
# Generated by Django 4.0.6 on 2022-07-10 21:46
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('metadata', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='decoder',
name='manufacturer',
field=models.ForeignKey(limit_choices_to={'category': 'model'}, on_delete=django.db.models.deletion.CASCADE, to='metadata.manufacturer'),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-12 10:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('metadata', '0002_alter_decoder_manufacturer'),
]
operations = [
migrations.AddField(
model_name='property',
name='private',
field=models.BooleanField(default=False),
),
]

View File

@@ -0,0 +1,23 @@
# Generated by Django 4.0.6 on 2022-07-14 14:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('metadata', '0003_property_private'),
]
operations = [
migrations.AlterModelOptions(
name='rollingstocktype',
options={'ordering': ['order']},
),
migrations.AddField(
model_name='rollingstocktype',
name='order',
field=models.PositiveSmallIntegerField(default=0),
preserve_default=False,
),
]

View File

@@ -8,6 +8,7 @@ from ram.utils import get_image_preview, slugify
class Property(models.Model):
name = models.CharField(max_length=128, unique=True)
private = models.BooleanField(default=False)
class Meta:
verbose_name_plural = "Properties"
@@ -59,7 +60,11 @@ class Company(models.Model):
class Decoder(models.Model):
name = models.CharField(max_length=128, unique=True)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
manufacturer = models.ForeignKey(
Manufacturer,
on_delete=models.CASCADE,
limit_choices_to={"category": "model"},
)
version = models.CharField(max_length=64, blank=True)
interface = models.PositiveSmallIntegerField(
choices=settings.DECODER_INTERFACES, null=True, blank=True
@@ -103,12 +108,14 @@ def tag_pre_save(sender, instance, **kwargs):
class RollingStockType(models.Model):
type = models.CharField(max_length=64)
order = models.PositiveSmallIntegerField()
category = models.CharField(
max_length=64, choices=settings.ROLLING_STOCK_TYPES
)
class Meta(object):
unique_together = ("category", "type")
ordering = ["order"]
def __str__(self):
return "{0} {1}".format(self.type, self.category)

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-07-15 12:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('portal', '0006_alter_siteconfiguration_site_name'),
]
operations = [
migrations.AddField(
model_name='siteconfiguration',
name='items_ordering',
field=models.CharField(choices=[('type', 'By rolling stock type'), ('company', 'By company name'), ('identifier', 'By rolling stock class')], default='type', max_length=10),
),
]

View File

@@ -16,6 +16,15 @@ class SiteConfiguration(SingletonModel):
choices=[(str(x * 3), str(x * 3)) for x in range(2, 11)],
default="6",
)
items_ordering = models.CharField(
max_length=10,
choices=[
("type", "By rolling stock type"),
("company", "By company name"),
("identifier", "By rolling stock class"),
],
default="type",
)
footer = models.TextField(blank=True)
footer_extended = models.TextField(blank=True)
show_version = models.BooleanField(default=True)

View File

@@ -6,10 +6,19 @@
display: inline-block;
}
a.badge, a.badge:hover {
text-decoration: none;
color: #fff;
}
.tab-pane {
min-height: 300px;
}
.img-thumbnail {
padding: 0;
}
#nav-notes > p {
padding: .5rem;
}

View File

@@ -8,7 +8,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="description" content="{{ site_conf.about}}">
<meta name="author" content="{{ site_conf.site_author }}">
<meta name="generator" content="Django Framework">
<title>{{ site_conf.site_name }}</title>
@@ -34,7 +34,7 @@
<header>
<div class="navbar navbar-light bg-light shadow-sm">
<div class="container">
<a href="/" class="navbar-brand d-flex align-items-center">
<a href="{% url 'index' %}" class="navbar-brand d-flex align-items-center">
<svg class="me-2" width="26" height="16" enable-background="new 0 0 26 26" version="1" viewBox="0 0 26 16" xmlns="http://www.w3.org/2000/svg">
<path d="m2.8125 0.0010991a1.0001 1.0001 0 0 0-0.8125 1c0 0.55455-0.44545 1-1 1a1.0001 1.0001 0 0 0-1 1v10a1.0001 1.0001 0 0 0 1 1c0.55455 0 1 0.44546 1 1a1.0001 1.0001 0 0 0 1 1h20a1.0001 1.0001 0 0 0 1-1c0-0.55454 0.44546-1 1-1a1.0001 1.0001 0 0 0 1-1v-10a1.0001 1.0001 0 0 0-1-1c-0.55454 0-1-0.44545-1-1a1.0001 1.0001 0 0 0-1-1h-20a1.0001 1.0001 0 0 0-0.09375 0 1.0001 1.0001 0 0 0-0.09375 0zm0.78125 2h14.406v1h2v-1h2.4062c0.30628 0.76906 0.82469 1.2875 1.5938 1.5938v8.8125c-0.76906 0.30628-1.2875 0.82469-1.5938 1.5938h-2.4062v-1h-2v1h-14.406c-0.30628-0.76906-0.82469-1.2875-1.5938-1.5938v-8.8125c0.76906-0.30628 1.2875-0.82469 1.5938-1.5938zm14.406 2v2h2v-2zm0 3v2h2v-2zm0 3v2h2v-2z" enable-background="accumulate" fill="#000" overflow="visible" stroke-width="2" style="text-indent:0;text-transform:none"/>
</svg>
@@ -48,14 +48,14 @@
<div class="container py-2">
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container-fluid g-0">
<a class="navbar-brand" href="/">Home</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<a class="navbar-brand" href="{% url 'index' %}">Home</a>
<div class="navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="/portal">Roster</a>
<a class="nav-link" href="{% url 'index' %}">Roster</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/portal/consist">Consists</a>
<a class="nav-link" href="{% url 'consists' %}">Consists</a>
</li>
</ul>
{% include 'includes/search.html' %}
@@ -79,14 +79,14 @@
<div class="col">
<div class="card shadow-sm">
{% for i in r.image.all %}
{% if i.is_thumbnail %}<a href="/portal/{{ r.uuid }}"><img src="{{ i.image.url }}" alt="Card image cap"></a>{% endif %}
{% if i.is_thumbnail %}<a href="{{r.get_absolute_url}}"><img src="{{ i.image.url }}" alt="Card image cap"></a>{% endif %}
{% endfor %}
<div class="card-body">
<p class="card-text"><strong>{{ r }}</strong></p>
{% if r.tags.all %}
<p class="card-text"><small>Tags:</small>
{% for t in r.tags.all %}<span class="badge bg-primary">
{{ t.name }}</span>{# new line is required #}
{% for t in r.tags.all %}<a href="{% url 'filtered' _filter="tag" search=t.slug %}" class="badge rounded-pill bg-primary">
{{ t.name }}</a>{# new line is required #}
{% endfor %}
</p>
{% endif %}
@@ -151,7 +151,7 @@
</table>
{% endif %}
<div class="btn-group mb-4">
<a class="btn btn-sm btn-outline-primary" href="/portal/{{ r.uuid }}">Show all data</a>
<a class="btn btn-sm btn-outline-primary" href="{{r.get_absolute_url}}">Show all data</a>
{% if request.user.is_staff %}<a class="btn btn-sm btn-outline-danger" href="{% url 'admin:roster_rollingstock_change' r.pk %}">Edit</a>{% endif %}
</div>
<div class="d-flex justify-content-between align-items-center">

View File

@@ -16,7 +16,7 @@
<div class="col">
<div class="card shadow-sm">
{% for i in r.rolling_stock.image.all %}
{% if i.is_thumbnail %}<a href="/portal/{{ r.rolling_stock.uuid }}"><img src="{{ i.image.url }}" alt="Card image cap"></a>{% endif %}
{% if i.is_thumbnail %}<a href="{{r.rolling_stock.get_absolute_url}}"><img src="{{ i.image.url }}" alt="Card image cap"></a>{% endif %}
{% endfor %}
<div class="card-body">
<p class="card-text"><strong>{{ r }}</strong></p>
@@ -88,7 +88,7 @@
</table>
{% endif %}
<div class="btn-group mb-4">
<a class="btn btn-sm btn-outline-primary" href="/portal/{{ r.rolling_stock.uuid }}">Show all data</a>
<a class="btn btn-sm btn-outline-primary" href="{{r.rolling_stock.get_absolute_url}}">Show all data</a>
{% if request.user.is_staff %}<a class="btn btn-sm btn-outline-danger" href="{% url 'admin:roster_rollingstock_change' r.rolling_stock.pk %}">Edit</a>{% endif %}
</div>
<div class="d-flex justify-content-between align-items-center">
@@ -105,25 +105,29 @@
<ul class="pagination justify-content-center mt-4">
{% if rolling_stock.has_previous %}
<li class="page-item">
<a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ rolling_stock.previous_page_number }}#rolling_stock" tabindex="-1">Previous</a>
<a class="page-link" href="{% url 'consist_pagination' uuid=consist.uuid page=rolling_stock.previous_page_number %}#rolling-stock" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">Previous</span>
</li>
{% endif %}
{% for i in rolling_stock.paginator.page_range %}
{% for i in page_range %}
{% if rolling_stock.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ i }}#rolling-stock">{{ i }}</a></li>
{% if i == rolling_stock.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'consist_pagination' uuid=consist.uuid page=i %}#rolling-stock">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if rolling_stock.has_next %}
<li class="page-item">
<a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ rolling_stock.next_page_number }}#rolling-stock" tabindex="-1">Next</a>
<a class="page-link" href="{% url 'consist_pagination' uuid=consist.uuid page=rolling_stock.next_page_number %}#rolling-stock" tabindex="-1">Next</a>
</li>
{% else %}
<li class="page-item disabled">

View File

@@ -8,12 +8,13 @@
{% for c in consist %}
<div class="col">
<div class="card shadow-sm">
{% if c.image %}<a href="{{ c.get_absolute_url }}"><img src="{{ c.image.url }}" alt="Card image cap"></a>{% endif %}
<div class="card-body">
<p class="card-text"><strong>{{ c.identifier }}</strong></p>
<p class="card-text"><strong>{{ c }}</strong></p>
{% if c.tags.all %}
<p class="card-text"><small>Tags:</small>
{% for t in c.tags.all %}<span class="badge bg-primary">
{{ t.name }}</span>{# new line is required #}
{% for t in c.tags.all %}<a href="{% url 'filtered' _filter="tag" search=t.slug %}" class="badge rounded-pill bg-primary">
{{ t.name }}</a>{# new line is required #}
{% endfor %}
</p>
{% endif %}
@@ -45,8 +46,8 @@
</tbody>
</table>
<div class="btn-group mb-4">
<a class="btn btn-sm btn-outline-primary" href="/portal/consist/{{ c.uuid }}">Show all data</a>
{% if request.user.is_staff %}<a class="btn btn-sm btn-outline-danger" href="{# url 'admin:consist_consist_change' c.pk #}">Edit</a>{% endif %}
<a class="btn btn-sm btn-outline-primary" href="{{ c.get_absolute_url }}">Show all data</a>
{% if request.user.is_staff %}<a class="btn btn-sm btn-outline-danger" href="{% url 'admin:consist_consist_change' c.pk %}">Edit</a>{% endif %}
</div>
<div class="d-flex justify-content-between align-items-center">
<small class="text-muted">Updated {{ c.updated_time | date:"M d, Y H:m" }}</small>
@@ -57,30 +58,34 @@
{% endfor %}
{% endblock %}
{% block pagination %}
{% if rolling_stock.has_other_pages %}
{% if consist.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center mt-4">
{% if rolling_stock.has_previous %}
{% if consist.has_previous %}
<li class="page-item">
<a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ rolling_stock.previous_page_number }}#rolling_stock" tabindex="-1">Previous</a>
<a class="page-link" href="{% url 'consists_pagination' page=consist.previous_page_number %}#rolling-stock" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">Previous</span>
</li>
{% endif %}
{% for i in rolling_stock.paginator.page_range %}
{% if rolling_stock.number == i %}
{% for i in page_range %}
{% if consist.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ i }}#rolling-stock">{{ i }}</a></li>
{% if i == rolling_stock.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'consists_pagination' page=i %}#rolling-stock">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if rolling_stock.has_next %}
{% if consist.has_next %}
<li class="page-item">
<a class="page-link" href="/portal/consist/{{ consist.uuid }}/{{ rolling_stock.next_page_number }}#rolling-stock" tabindex="-1">Next</a>
<a class="page-link" href="{% url 'consists_pagination' page=consist.next_page_number %}#rolling-stock" tabindex="-1">Next</a>
</li>
{% else %}
<li class="page-item disabled">

View File

@@ -2,7 +2,7 @@
{% load markdown %}
{% block header %}
<h1 class="fw-light">About</h1>
{% if site_conf.about %}<h1 class="fw-light">About</h1>{% endif %}
<p class="lead text-muted">{{ site_conf.about | markdown | safe }}</p>
{% endblock %}
@@ -12,25 +12,29 @@
<ul class="pagination justify-content-center mt-4">
{% if rolling_stock.has_previous %}
<li class="page-item">
<a class="page-link" href="/portal/{{ rolling_stock.previous_page_number }}#rolling_stock" tabindex="-1">Previous</a>
<a class="page-link" href="{% url 'index_pagination' page=rolling_stock.previous_page_number %}#rolling-stock" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">Previous</span>
</li>
{% endif %}
{% for i in rolling_stock.paginator.page_range %}
{% for i in page_range %}
{% if rolling_stock.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="/portal/{{ i }}#rolling-stock">{{ i }}</a></li>
{% if i == rolling_stock.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'index_pagination' page=i %}#rolling-stock">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if rolling_stock.has_next %}
<li class="page-item">
<a class="page-link" href="/portal/{{ rolling_stock.next_page_number }}#rolling-stock" tabindex="-1">Next</a>
<a class="page-link" href="{% url 'index_pagination' page=rolling_stock.next_page_number %}#rolling-stock" tabindex="-1">Next</a>
</li>
{% else %}
<li class="page-item disabled">

View File

@@ -12,9 +12,9 @@
{{ site_conf.footer_extended | markdown | safe }}
</div>
</div>
{% if site_conf.show_version %}
<div class="container">
<p class="small text-muted">Version: {{ site_conf.version }}</p>
<p class="small text-muted">Made with ❤️ for 🚂 and <a href="https://github.com/daniviga/django-ram">django-ram</a>
{% if site_conf.show_version %}<br/>Version {{ site_conf.version }}{% endif %}
</div>
{% endif %}
</footer>

View File

@@ -0,0 +1,7 @@
<section class="py-4 text-center container">
<div class="row">
<div class="mx-auto">
{% block header_content %}{% endblock %}
</div>
</div>
</section>

View File

@@ -1,5 +1,24 @@
<form class="d-flex" action="/portal/search" method="post">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" name="search">
<button class="btn btn-outline-primary" type="submit">Search</button>
<form class="d-flex needs-validation" action="{% url 'search' %}" method="post" novalidate>
<div class="input-group has-validation">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" name="search" id="searchValidation" required>
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</form>
<script>
(function () {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
form.classList.add('was-validated')
event.preventDefault()
event.stopPropagation()
}
}, false)
})
})()
</script>

View File

@@ -5,8 +5,8 @@
<h1 class="fw-light">{{ rolling_stock }}</h1>
{% if rolling_stock.tags.all %}
<p><small>Tags:</small>
{% for t in rolling_stock.tags.all %}<span class="badge bg-primary">
{{ t.name }}</span>{# new line is required #}
{% for t in rolling_stock.tags.all %}<a href="{% url 'filtered' _filter="tag" search=t.slug %}" class="badge rounded-pill bg-primary">
{{ t.name }}</a>{# new line is required #}
{% endfor %}
</p>
{% endif %}
@@ -29,7 +29,6 @@
<button class="nav-link" id="nav-class-tab" data-bs-toggle="tab" data-bs-target="#nav-class" type="button" role="tab" aria-controls="nav-class" aria-selected="false">Class data</button>
{% if rolling_stock.decoder %}<button class="nav-link" id="nav-dcc-tab" data-bs-toggle="tab" data-bs-target="#nav-dcc" type="button" role="tab" aria-controls="nav-dcc" aria-selected="false">DCC</button>{% endif %}
{% if rolling_stock.notes %}<button class="nav-link" id="nav-notes-tab" data-bs-toggle="tab" data-bs-target="#nav-notes" type="button" role="tab" aria-controls="nav-notes" aria-selected="false">Notes</button>{% endif %}
{% if rolling_stock.property.count > 0 %}<button class="nav-link" id="nav-properties-tab" data-bs-toggle="tab" data-bs-target="#nav-properties" type="button" role="tab" aria-controls="nav-properties" aria-selected="false">Properties</button>{% endif %}
{% if rolling_stock.document.count > 0 %}<button class="nav-link" id="nav-documents-tab" data-bs-toggle="tab" data-bs-target="#nav-documents" type="button" role="tab" aria-controls="nav-documents" aria-selected="false">Documents</button>{% endif %}
</div>
</nav>
@@ -73,7 +72,7 @@
<tbody>
<tr>
<th width="35%" scope="row">Manufacturer</th>
<td>{% if rolling_stock.manufacturer.website %}<a href="{{ rolling_stock.manufacturer.website }}">{% endif %}{{ rolling_stock.manufacturer }}{% if rolling_stock.manufacturer.website %}</a>{% endif %}</th>
<td>{% if rolling_stock.manufacturer.website %}<a href="{{ rolling_stock.manufacturer.website }}">{% endif %}{{ rolling_stock.manufacturer|default_if_none:"" }}{% if rolling_stock.manufacturer.website %}</a>{% endif %}</th>
</tr>
<tr>
<th scope="row">Scale</th>
@@ -115,7 +114,7 @@
<tbody>
<tr>
<th width="35%" scope="row">Manufacturer</th>
<td>{% if rolling_stock.manufacturer.website %}<a href="{{ rolling_stock.manufacturer.website }}">{% endif %}{{ rolling_stock.manufacturer }}{% if rolling_stock.manufacturer.website %}</a>{% endif %}</th>
<td>{% if rolling_stock.manufacturer.website %}<a href="{{ rolling_stock.manufacturer.website }}">{% endif %}{{ rolling_stock.manufacturer|default_if_none:"" }}{% if rolling_stock.manufacturer.website %}</a>{% endif %}</th>
</tr>
<tr>
<th scope="row">Scale</th>
@@ -139,6 +138,23 @@
</tr>
</tbody>
</table>
{% if rolling_stock_properties %}
<table class="table table-striped">
<thead>
<tr>
<th colspan="2" scope="row">Properties</th>
</tr>
</thead>
<tbody>
{% for p in rolling_stock_properties %}
<tr>
<th width="35%" scope="row">{{ p.property }}</th>
<td>{{ p.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
<div class="tab-pane fade" id="nav-class" role="tabpanel" aria-labelledby="nav-class-tab">
<table class="table table-striped">
@@ -166,10 +182,27 @@
</tr>
<tr>
<th scope="row">Manufacturer</th>
<td>{{ rolling_stock.rolling_class.manufacturer }}</td>
<td>{{ rolling_stock.rolling_class.manufacturer|default_if_none:"" }}</td>
</tr>
</tbody>
</table>
{% if class_properties %}
<table class="table table-striped">
<thead>
<tr>
<th colspan="2" scope="row">Properties</th>
</tr>
</thead>
<tbody>
{% for p in class_properties %}
<tr>
<th width="35%" scope="row">{{ p.property }}</th>
<td>{{ p.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
<div class="tab-pane fade" id="nav-dcc" role="tabpanel" aria-labelledby="nav-dcc-tab">
<table class="table table-striped">
@@ -189,7 +222,7 @@
</tr>
<tr>
<th scope="row">Manufacturer</th>
<td>{{ rolling_stock.decoder.manufacturer }}</td>
<td>{{ rolling_stock.decoder.manufacturer|default_if_none:"" }}</td>
</tr>
<tr>
<th scope="row">Version</th>
@@ -209,23 +242,6 @@
<div class="tab-pane fade" id="nav-notes" role="tabpanel" aria-labelledby="nav-notes-tab">
{{ rolling_stock.notes | markdown | safe }}
</div>
<div class="tab-pane fade" id="nav-properties" role="tabpanel" aria-labelledby="nav-properties-tab">
<table class="table table-striped">
<thead>
<tr>
<th colspan="2" scope="row">Properties</th>
</tr>
</thead>
<tbody>
{% for p in rolling_stock.property.all %}
<tr>
<th scope="row">{{ p.property }}</th>
<td>{{ p.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="tab-pane fade" id="nav-documents" role="tabpanel" aria-labelledby="nav-documents-tab">
<table class="table table-striped">
<thead>

View File

@@ -10,25 +10,29 @@
<ul class="pagination justify-content-center mt-4">
{% if rolling_stock.has_previous %}
<li class="page-item">
<a class="page-link" href="/portal/search/{{ search }}/{{ rolling_stock.previous_page_number }}#rolling_stock" tabindex="-1">Previous</a>
<a class="page-link" href="{% url 'search_pagination' search=search page=rolling_stock.previous_page_number %}#rolling-stock" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">Previous</span>
</li>
{% endif %}
{% for i in rolling_stock.paginator.page_range %}
{% for i in page_range %}
{% if rolling_stock.number == i %}
<li class="page-item active">
<span class="page-link">{{ i }}</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="/portal/search/{{ search }}/{{ i }}#rolling-stock">{{ i }}</a></li>
{% if i == rolling_stock.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="{% url 'index_pagination' page=i %}#rolling-stock">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if rolling_stock.has_next %}
<li class="page-item">
<a class="page-link" href="/portal/search/{{ search }}/{{ rolling_stock.next_page_number }}#rolling-stock" tabindex="-1">Next</a>
<a class="page-link" href="{% url 'search_pagination' search=search page=rolling_stock.next_page_number %}#rolling-stock" tabindex="-1">Next</a>
</li>
{% else %}
<li class="page-item disabled">

View File

@@ -22,7 +22,10 @@ urlpatterns = [
GetHomeFiltered.as_view(),
name="search_pagination",
),
path("consist", Consists.as_view(), name="consists"),
path("consists", Consists.as_view(), name="consists"),
path(
"consists/<int:page>", Consists.as_view(), name="consists_pagination"
),
path("consist/<uuid:uuid>", GetConsist.as_view(), name="consist"),
path(
"consist/<uuid:uuid>/<int:page>",

View File

@@ -13,20 +13,37 @@ from roster.models import RollingStock
from consist.models import Consist
def order_by_fields():
order_by = get_site_conf().items_ordering
fields = [
"rolling_class__type",
"rolling_class__company",
"rolling_class__identifier",
"road_number_int",
]
if order_by == "type":
return (fields[0], fields[1], fields[2], fields[3])
elif order_by == "company":
return (fields[1], fields[0], fields[2], fields[3])
elif order_by == "identifier":
return (fields[2], fields[0], fields[1], fields[3])
class GetHome(View):
def get(self, request, page=1):
site_conf = get_site_conf()
rolling_stock = RollingStock.objects.all()
rolling_stock = RollingStock.objects.order_by(*order_by_fields())
paginator = Paginator(rolling_stock, site_conf.items_per_page)
rolling_stock = paginator.get_page(page)
page_range = paginator.get_elided_page_range(rolling_stock.number)
try:
rolling_stock = paginator.page(page)
except PageNotAnInteger:
rolling_stock = paginator.page(1)
except EmptyPage:
rolling_stock = paginator.page(paginator.num_pages)
return render(request, "home.html", {"rolling_stock": rolling_stock})
return render(
request,
"home.html",
{"rolling_stock": rolling_stock, "page_range": page_range},
)
class GetHomeFiltered(View):
@@ -57,24 +74,25 @@ class GetHomeFiltered(View):
)
elif _filter == "scale":
query = Q(scale__scale__icontains=search)
elif _filter == "tag":
query = Q(tags__slug__icontains=search)
else:
raise Http404
rolling_stock = RollingStock.objects.filter(query)
rolling_stock = RollingStock.objects.filter(query).order_by(
*order_by_fields()
)
matches = len(rolling_stock)
paginator = Paginator(rolling_stock, site_conf.items_per_page)
rolling_stock = paginator.get_page(page)
page_range = paginator.get_elided_page_range(rolling_stock.number)
try:
rolling_stock = paginator.page(page)
except PageNotAnInteger:
rolling_stock = paginator.page(1)
except EmptyPage:
rolling_stock = paginator.page(paginator.num_pages)
return rolling_stock, matches
return rolling_stock, matches, page_range
def get(self, request, search, _filter=None, page=1):
rolling_stock, matches = self.run_search(
request, search, _filter, page)
rolling_stock, matches, page_range = self.run_search(
request, search, _filter, page
)
return render(
request,
@@ -84,6 +102,7 @@ class GetHomeFiltered(View):
"filter": _filter,
"matches": matches,
"rolling_stock": rolling_stock,
"page_range": page_range,
},
)
@@ -91,8 +110,9 @@ class GetHomeFiltered(View):
search = request.POST.get("search")
if not search:
raise Http404
rolling_stock, matches = self.run_search(
request, search, _filter, page)
rolling_stock, matches, page_range = self.run_search(
request, search, _filter, page
)
return render(
request,
@@ -102,6 +122,7 @@ class GetHomeFiltered(View):
"filter": _filter,
"matches": matches,
"rolling_stock": rolling_stock,
"page_range": page_range,
},
)
@@ -113,11 +134,26 @@ class GetRollingStock(View):
except ObjectDoesNotExist:
raise Http404
class_properties = (
rolling_stock.rolling_class.property.all()
if request.user.is_authenticated
else rolling_stock.rolling_class.property.filter(
property__private=False
)
)
rolling_stock_properties = (
rolling_stock.property.all()
if request.user.is_authenticated
else rolling_stock.property.filter(property__private=False)
)
return render(
request,
"page.html",
{
"rolling_stock": rolling_stock,
"class_properties": class_properties,
"rolling_stock_properties": rolling_stock_properties,
},
)
@@ -126,16 +162,16 @@ class Consists(View):
def get(self, request, page=1):
site_conf = get_site_conf()
consist = Consist.objects.all()
paginator = Paginator(consist, site_conf.items_per_page)
consist = paginator.get_page(page)
page_range = paginator.get_elided_page_range(consist.number)
try:
consist = paginator.page(page)
except PageNotAnInteger:
consist = paginator.page(1)
except EmptyPage:
consist = paginator.page(paginator.num_pages)
return render(request, "consists.html", {"consist": consist})
return render(
request,
"consists.html",
{"consist": consist, "page_range": page_range},
)
class GetConsist(View):
@@ -146,17 +182,17 @@ class GetConsist(View):
except ObjectDoesNotExist:
raise Http404
rolling_stock = consist.consist_item.all()
paginator = Paginator(rolling_stock, site_conf.items_per_page)
try:
rolling_stock = paginator.page(page)
except PageNotAnInteger:
rolling_stock = paginator.page(1)
except EmptyPage:
rolling_stock = paginator.page(paginator.num_pages)
paginator = Paginator(rolling_stock, site_conf.items_per_page)
rolling_stock = paginator.get_page(page)
page_range = paginator.get_elided_page_range(rolling_stock.number)
return render(
request,
"consist.html",
{"consist": consist, "rolling_stock": rolling_stock},
{
"consist": consist,
"rolling_stock": rolling_stock,
"page_range": page_range,
},
)

View File

@@ -1,4 +1,4 @@
from ram.utils import git_suffix
__version__ = "0.0.2"
__version__ = "0.0.6"
__version__ += git_suffix(__file__)

View File

@@ -0,0 +1,20 @@
# vim: syntax=python
"""
Django local_settings for ram project.
"""
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = (
"django-insecure-1fgtf05rwp0qp05@ef@a7%x#o+t6vk6063py=vhdmut0j!8s4u"
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
STORAGE_DIR = BASE_DIR / "storage"
ALLOWED_HOSTS = ["127.0.0.1"]
CSRF_TRUSTED_ORIGINS = ["https://myhost"]
ROOT_URLCONF = "ram.urls"
STATIC_URL = "static/"
MEDIA_URL = "media/"

View File

@@ -161,3 +161,10 @@ ROLLING_STOCK_TYPES = [
("equipment", "Equipment"),
("other", "Other"),
]
try:
from ram.local_settings import *
except ImportError:
# If a local_setting.py does not exist
# settings in this file only will be used
pass

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title>Swagger</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script>
const ui = SwaggerUIBundle({
url: "{% url schema_url %}",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
layout: "BaseLayout"
})
</script>
</body>
</html>

View File

@@ -20,7 +20,7 @@ from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", lambda r: redirect("/portal/")),
path("", lambda r: redirect("portal/")),
path("portal/", include("portal.urls")),
path("ht/", include("health_check.urls")),
path("admin/", admin.site.urls),
@@ -29,18 +29,26 @@ urlpatterns = [
path("api/v1/dcc/", include("driver.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# if settings.DEBUG:
# from django.views.generic import TemplateView
# from rest_framework.schemas import get_schema_view
#
# urlpatterns += [
# path('swagger/', TemplateView.as_view(
# template_name='swagger.html',
# extra_context={'schema_url': 'openapi-schema'}
# ), name='swagger'),
# path('openapi', get_schema_view(
# title="BITE - A Basic/IoT/Example",
# description="BITE API for IoT",
# version="1.0.0"
# ), name='openapi-schema'),
# ]
if settings.DEBUG:
from django.views.generic import TemplateView
from rest_framework.schemas import get_schema_view
urlpatterns += [
path(
"swagger/",
TemplateView.as_view(
template_name="swagger.html",
extra_context={"schema_url": "openapi-schema"},
),
name="swagger",
),
path(
"openapi",
get_schema_view(
title="RAM - Railroad Assets Manager",
description="RAM API",
version="1.0.0",
),
name="openapi-schema",
),
]

View File

@@ -0,0 +1,24 @@
# Generated by Django 4.0.6 on 2022-07-13 17:58
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('roster', '0005_alter_rollingstockproperty_rolling_stock'),
]
operations = [
migrations.AlterField(
model_name='rollingclassproperty',
name='rolling_class',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='property', to='roster.rollingclass', verbose_name='Class'),
),
migrations.AlterField(
model_name='rollingstock',
name='rolling_class',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rolling_class', to='roster.rollingclass', verbose_name='Class'),
),
]

View File

@@ -0,0 +1,25 @@
# Generated by Django 4.0.6 on 2022-07-15 15:24
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('metadata', '0004_alter_rollingstocktype_options_and_more'),
('roster', '0006_alter_rollingclassproperty_rolling_class_and_more'),
]
operations = [
migrations.AlterField(
model_name='rollingclass',
name='company',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='metadata.company'),
),
migrations.AlterField(
model_name='rollingclass',
name='type',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='metadata.rollingstocktype'),
),
]

View File

@@ -0,0 +1,34 @@
# Generated by Django 4.0.6 on 2022-07-15 15:55
from django.db import migrations, models
def gen_road_number_cleaned(apps, schema_editor):
RollingStock = apps.get_model('roster', 'RollingStock')
for row in RollingStock.objects.all():
row.road_number_cleaned = row.road_number.lstrip('#').lstrip('0')
row.save(update_fields=['road_number_cleaned'])
class Migration(migrations.Migration):
dependencies = [
('roster', '0007_alter_rollingclass_company_alter_rollingclass_type'),
]
operations = [
migrations.AddField(
model_name='rollingstock',
name='road_number_cleaned',
field=models.CharField(default='', max_length=128),
preserve_default=False,
),
migrations.RunPython(
gen_road_number_cleaned,
reverse_code=migrations.RunPython.noop
),
migrations.AlterModelOptions(
name='rollingstock',
options={'ordering': ['rolling_class', 'road_number_cleaned'], 'verbose_name_plural': 'Rolling stock'},
),
]

View File

@@ -0,0 +1,41 @@
# Generated by Django 4.0.6 on 2022-07-16 15:38
import re
from django.db import migrations, models
def gen_road_number_cleaned(apps, schema_editor):
RollingStock = apps.get_model('roster', 'RollingStock')
for row in RollingStock.objects.all():
try:
row.road_number_int = int(re.findall(r"\d+", row.road_number)[0])
row.save(update_fields=['road_number_int'])
except IndexError:
pass
class Migration(migrations.Migration):
dependencies = [
('roster', '0008_rollingstock_road_number_cleaned'),
]
operations = [
migrations.AlterModelOptions(
name='rollingstock',
options={'ordering': ['rolling_class', 'road_number_int'], 'verbose_name_plural': 'Rolling stock'},
),
migrations.RemoveField(
model_name='rollingstock',
name='road_number_cleaned',
),
migrations.AddField(
model_name='rollingstock',
name='road_number_int',
field=models.PositiveSmallIntegerField(default=0),
),
migrations.RunPython(
gen_road_number_cleaned,
reverse_code=migrations.RunPython.noop
),
]

View File

@@ -1,10 +1,11 @@
import os
import re
from uuid import uuid4
from django.db import models
from django.urls import reverse
from django.dispatch import receiver
# from django.core.files.storage import FileSystemStorage
# from django.dispatch import receiver
from ram.utils import get_image_preview
from metadata.models import (
@@ -25,12 +26,8 @@ from metadata.models import (
class RollingClass(models.Model):
identifier = models.CharField(max_length=128, unique=False)
type = models.ForeignKey(
RollingStockType, on_delete=models.CASCADE, null=True, blank=True
)
company = models.ForeignKey(
Company, on_delete=models.CASCADE, null=True, blank=True
)
type = models.ForeignKey(RollingStockType, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
description = models.CharField(max_length=256, blank=True)
manufacturer = models.ForeignKey(
Manufacturer,
@@ -55,6 +52,7 @@ class RollingClassProperty(models.Model):
on_delete=models.CASCADE,
null=False,
blank=False,
related_name="property",
verbose_name="Class",
)
property = models.ForeignKey(Property, on_delete=models.CASCADE)
@@ -74,9 +72,11 @@ class RollingStock(models.Model):
on_delete=models.CASCADE,
null=False,
blank=False,
related_name="rolling_class",
verbose_name="Class",
)
road_number = models.CharField(max_length=128, unique=False)
road_number_int = models.PositiveSmallIntegerField(default=0, unique=False)
manufacturer = models.ForeignKey(
Manufacturer,
on_delete=models.CASCADE,
@@ -101,7 +101,7 @@ class RollingStock(models.Model):
updated_time = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["rolling_class", "road_number"]
ordering = ["rolling_class", "road_number_int"]
verbose_name_plural = "Rolling stock"
def __str__(self):
@@ -117,6 +117,16 @@ class RollingStock(models.Model):
return str(self.rolling_class.company)
@receiver(models.signals.pre_save, sender=RollingStock)
def pre_save_running_number(sender, instance, *args, **kwargs):
try:
instance.road_number_int = int(
re.findall(r"\d+", instance.road_number)[0]
)
except IndexError:
pass
class RollingStockDocument(models.Model):
rolling_stock = models.ForeignKey(
RollingStock, on_delete=models.CASCADE, related_name="document"

View File

@@ -1,9 +1,9 @@
from django.urls import path
from roster.views import RosterList, RosterGet, RosterAddress, RosterIdentifier
from roster.views import RosterList, RosterGet, RosterAddress, RosterClass
urlpatterns = [
path("list", RosterList.as_view()),
path("get/<str:uuid>", RosterGet.as_view()),
path("address/<int:address>", RosterAddress.as_view()),
path("identifier/<str:identifier>", RosterIdentifier.as_view()),
path("class/<str:class>", RosterClass.as_view()),
]

View File

@@ -1,4 +1,5 @@
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.schemas.openapi import AutoSchema
from roster.models import RollingStock
from roster.serializers import RollingStockSerializer
@@ -14,14 +15,24 @@ class RosterGet(RetrieveAPIView):
serializer_class = RollingStockSerializer
lookup_field = "uuid"
schema = AutoSchema(operation_id_base="retrieveRollingStockByUUID")
class RosterAddress(ListAPIView):
queryset = RollingStock.objects.all()
serializer_class = RollingStockSerializer
lookup_field = "address"
schema = AutoSchema(operation_id_base="retrieveRollingStockByAddress")
def get_queryset(self):
address = self.kwargs["address"]
return RollingStock.objects.filter(address=address)
class RosterIdentifier(RetrieveAPIView):
queryset = RollingStock.objects.all()
class RosterClass(ListAPIView):
serializer_class = RollingStockSerializer
lookup_field = "identifier"
schema = AutoSchema(operation_id_base="retrieveRollingStockByClass")
def get_queryset(self):
_class = self.kwargs["class"]
return RollingStock.objects.filter(rolling_class__identifier=_class)

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

BIN
sample_data/images/scrr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

376
sample_data/metadata.json Normal file
View File

@@ -0,0 +1,376 @@
[
{
"model": "metadata.property",
"pk": 1,
"fields": {
"name": "Wheel arrangement"
}
},
{
"model": "metadata.property",
"pk": 2,
"fields": {
"name": "Trucks"
}
},
{
"model": "metadata.manufacturer",
"pk": 1,
"fields": {
"name": "Roco",
"category": "model",
"website": "https://www.roco.cc/",
"logo": "images/roco_logo.png"
}
},
{
"model": "metadata.manufacturer",
"pk": 2,
"fields": {
"name": "Liliput",
"category": "model",
"website": "https://liliput.de/",
"logo": "images/liliput_logo.png"
}
},
{
"model": "metadata.manufacturer",
"pk": 3,
"fields": {
"name": "Stängl",
"category": "model",
"website": "https://www.halling.at/",
"logo": "images/staengl_logo.png"
}
},
{
"model": "metadata.manufacturer",
"pk": 4,
"fields": {
"name": "Ferro-Train",
"category": "model",
"website": "https://www.halling.at/",
"logo": "images/ferro_logo.png"
}
},
{
"model": "metadata.manufacturer",
"pk": 5,
"fields": {
"name": "Bachmann",
"category": "model",
"website": "https://bachmanntrains.com/",
"logo": "images/bachmann_logo.png"
}
},
{
"model": "metadata.manufacturer",
"pk": 6,
"fields": {
"name": "ESU",
"category": "model",
"website": "https://www.esu.eu/",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 7,
"fields": {
"name": "Soundtraxx",
"category": "model",
"website": "https://soundtraxx.com/",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 8,
"fields": {
"name": "Lima Locomotive Works",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 9,
"fields": {
"name": "Baldwin Locomotive Works",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 10,
"fields": {
"name": "Rogers Locomotive and Machine Works",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 11,
"fields": {
"name": "Porter Locomotive Works",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 12,
"fields": {
"name": "Krauss & Comp. / Österreichische Siemens-Schuckert Werke",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 13,
"fields": {
"name": "Simmering-Graz-Pauker",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.manufacturer",
"pk": 14,
"fields": {
"name": "Davenport Locomotive Works",
"category": "real",
"website": "",
"logo": ""
}
},
{
"model": "metadata.company",
"pk": 1,
"fields": {
"name": "ÖBB",
"extended_name": "Österreichische Bundesbahnen",
"country": "AT",
"freelance": false,
"logo": "images/oebb_logo.png"
}
},
{
"model": "metadata.company",
"pk": 2,
"fields": {
"name": "SCRR",
"extended_name": "Sand Creek Railroad",
"country": "US",
"freelance": true,
"logo": "images/scrr.png"
}
},
{
"model": "metadata.company",
"pk": 3,
"fields": {
"name": "RhB",
"extended_name": "Rhätische Bahn",
"country": "CH",
"freelance": false,
"logo": ""
}
},
{
"model": "metadata.company",
"pk": 4,
"fields": {
"name": "FO",
"extended_name": "Furka-Oberalp",
"country": "CH",
"freelance": false,
"logo": ""
}
},
{
"model": "metadata.decoder",
"pk": 1,
"fields": {
"name": "Basic",
"manufacturer": 5,
"version": "",
"interface": 2,
"sound": false,
"image": ""
}
},
{
"model": "metadata.decoder",
"pk": 2,
"fields": {
"name": "Tsunami",
"manufacturer": 7,
"version": "1",
"interface": 2,
"sound": true,
"image": ""
}
},
{
"model": "metadata.decoder",
"pk": 3,
"fields": {
"name": "LokPilot Micro",
"manufacturer": 6,
"version": "v4",
"interface": 1,
"sound": false,
"image": ""
}
},
{
"model": "metadata.scale",
"pk": 1,
"fields": {
"scale": "H0e",
"ratio": "1:87",
"gauge": "9 mm"
}
},
{
"model": "metadata.scale",
"pk": 2,
"fields": {
"scale": "H0",
"ratio": "1:87",
"gauge": "16.5 mm"
}
},
{
"model": "metadata.scale",
"pk": 3,
"fields": {
"scale": "0n30",
"ratio": "1:48",
"gauge": "16.5 mm"
}
},
{
"model": "metadata.scale",
"pk": 4,
"fields": {
"scale": "H0m",
"ratio": "1:87",
"gauge": "11 mm"
}
},
{
"model": "metadata.tag",
"pk": 1,
"fields": {
"name": "Mariazellerbahn",
"slug": "mariazellerbahn"
}
},
{
"model": "metadata.tag",
"pk": 2,
"fields": {
"name": "Colorado",
"slug": "colorado"
}
},
{
"model": "metadata.tag",
"pk": 3,
"fields": {
"name": "Narrow gauge",
"slug": "narrow-gauge"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 1,
"fields": {
"type": "Electric",
"category": "engine"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 2,
"fields": {
"type": "Diesel",
"category": "engine"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 3,
"fields": {
"type": "Steam",
"category": "engine"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 4,
"fields": {
"type": "EMU",
"category": "railcar"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 5,
"fields": {
"type": "DMU",
"category": "railcar"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 6,
"fields": {
"type": "Electric",
"category": "railcar"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 7,
"fields": {
"type": "Diesel",
"category": "railcar"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 8,
"fields": {
"type": "Passenger",
"category": "car"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 9,
"fields": {
"type": "Freight",
"category": "car"
}
},
{
"model": "metadata.rollingstocktype",
"pk": 10,
"fields": {
"type": "Caboose",
"category": "equipment"
}
}
]