Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Switch map to run from APIs
Add sidebar with building information
Add map locations filtering based on seismic risk

Additional changes:
- Fix an_construire
  • Loading branch information
cnstlungu committed Oct 2, 2019
2 parents 726a48b + e90c77a commit 76eb0a3
Show file tree
Hide file tree
Showing 24 changed files with 399 additions and 44 deletions.
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ venv.bak/
# mypy
.mypy_cache/

#Pycharm
.idea
# IDEs
.idea
.vscode


*.xlsx
*.csv
# files
seismic_site/media/*
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ sqlparse==0.3.0
django-import-export==1.2.0
tablib==0.13.0
django-jet==1.0.8
django-crispy-forms>=1.7.2,<2.0.0
django-ckeditor>=5.7
Empty file added seismic_site/cms/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions seismic_site/cms/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib import admin

from .models import Page, Category, Attachment


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name',)


class DocumentInline(admin.TabularInline):
model = Attachment


@admin.register(Page)
class PageAdmin(admin.ModelAdmin):
list_display = ('title', 'category', 'updated_on', 'is_published')
list_filter = ('is_published',)
# prepopulated_fields = {"slug": ("title",)}
inlines = [
DocumentInline,
]
5 changes: 5 additions & 0 deletions seismic_site/cms/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CmsConfig(AppConfig):
name = 'cms'
41 changes: 41 additions & 0 deletions seismic_site/cms/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.2.4 on 2019-09-28 12:30

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=150, unique=True)),
],
options={
'verbose_name_plural': 'categories',
},
),
migrations.CreateModel(
name='Page',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(blank=True, help_text='Page title', max_length=150)),
('slug',
models.SlugField(blank=True, help_text='Unique URL slug (leave empty to auto-generate)', unique=True)),
('content', models.TextField(blank=True, help_text='Page content')),
('updated_on', models.DateTimeField(auto_now=True, help_text='Last update time', null=True)),
('publishing_date',
models.DateTimeField(blank=True, help_text='Public page publishing date', null=True)),
('is_published',
models.BooleanField(db_index=True, default=False, help_text='Is this page visible on the website')),
('category', models.ForeignKey(blank=True, help_text='Page category', null=True,
on_delete=django.db.models.deletion.SET_NULL, to='cms.Category')),
],
),
]
25 changes: 25 additions & 0 deletions seismic_site/cms/migrations/0002_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2.4 on 2019-09-28 14:07

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('cms', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Attachment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Attachment name', max_length=150)),
('upload_date', models.DateTimeField(auto_now=True, help_text='Attachment upload date')),
('uploaded_file', models.FileField(upload_to='uploads/%Y/%m/%d/')),
('page',
models.ForeignKey(blank=True, help_text='Page attachment', on_delete=django.db.models.deletion.CASCADE,
to='cms.Page')),
],
),
]
18 changes: 18 additions & 0 deletions seismic_site/cms/migrations/0003_auto_20190928_1454.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.4 on 2019-09-28 14:54

import ckeditor.fields
from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
('cms', '0002_attachment'),
]

operations = [
migrations.AlterField(
model_name='page',
name='content',
field=ckeditor.fields.RichTextField(),
),
]
Empty file.
82 changes: 82 additions & 0 deletions seismic_site/cms/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from ckeditor.fields import RichTextField
from django.db import models
from django.utils import timezone
from django.utils.text import slugify


class Category(models.Model):
name = models.CharField(
blank=False, null=False, max_length=150, unique=True)

def __str__(self):
return "{}".format(self.name)

class Meta:
verbose_name_plural = "categories"


class Page(models.Model):
category = models.ForeignKey(
Category, blank=True, null=True, on_delete=models.SET_NULL,
help_text="Page category")
title = models.CharField(
blank=True, max_length=150,
help_text="Page title")
slug = models.SlugField(
unique=True, blank=True, null=False,
help_text="Unique URL slug (leave empty to auto-generate)")
content = RichTextField()
updated_on = models.DateTimeField(
auto_now=timezone.now,
blank=True, null=True, editable=False,
help_text="Last update time")
publishing_date = models.DateTimeField(
blank=True, null=True,
help_text="Public page publishing date")
is_published = models.BooleanField(
default=False, db_index=True,
help_text="Is this page visible on the website")

def __str__(self):
return "{}".format(self.title)

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
is_unique = False
suffix = 0
while not is_unique:
try:
existing_page = Page.objects.get(slug=self.slug)
except Page.DoesNotExist:
is_unique = True
else:
if existing_page.id == self.id:
is_unique = True
else:
suffix += 1
self.slug = slugify("{} {}".format(self.title, suffix))

super().save(*args, **kwargs)


class Attachment(models.Model):
page = models.ForeignKey(
Page, blank=True, null=False, on_delete=models.CASCADE,
help_text="Page attachment")
name = models.CharField(
max_length=150,
help_text="Attachment name")
upload_date = models.DateTimeField(
auto_now=timezone.now,
help_text="Attachment upload date")
uploaded_file = models.FileField(
upload_to='uploads/%Y/%m/%d/', max_length=100)

def __str__(self):
return "{}{}".format(
self.uploaded_file.url,
self.name)

# class InlineResource(models.Model):
# pass
18 changes: 18 additions & 0 deletions seismic_site/cms/templates/cms/view_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<title>{{ page.title }}</title>
</head>
<body>

<div>
{{ page.content|safe }}
</div>

<ul>
{% for document in page.attachment_set.all %}
<li>{{ document.uploaded_file.url }}</li>
{% endfor %}
</ul>

</body>
</html>
1 change: 1 addition & 0 deletions seismic_site/cms/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
7 changes: 7 additions & 0 deletions seismic_site/cms/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from . import views

urlpatterns = [
path('<slug:slug>/', views.view_page, name='view_page')
]
12 changes: 12 additions & 0 deletions seismic_site/cms/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.shortcuts import render, get_object_or_404

from .models import Page


def view_page(request, slug):
page = get_object_or_404(Page, slug=slug, is_published=True)
return render(
request,
'cms/view_page.html', {
'page': page
})
43 changes: 38 additions & 5 deletions seismic_site/map_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
from django.contrib import admin
from .models import CsvFile, Building, CSVFileAdmin
import tablib
from django.contrib import admin, messages

# Register your models here.
admin.site.register(CsvFile, CSVFileAdmin)
admin.site.register(Building) # ld-l
from . import models


@admin.register(models.Building)
class BuildingAdmin(admin.ModelAdmin):
list_filter = ['status', 'clasa_categorie', 'nr_postal']
list_display = ['adresa', 'clasa_categorie', 'an_expertiza', 'expert_atestat', 'status']
search_fields = ['adresa']


@admin.register(models.CsvFile)
class CSVFileAdmin(admin.ModelAdmin):
actions = ['import_files']
list_display = ['name', 'status']

def import_files(self, request, query_set):
for q in query_set:
data = tablib.import_set(open(q.file.file.name, 'rb').read())
changed_headers = []
for header in data.headers:
changed_headers.append(header.lower().replace(':', '').replace('.', '').strip().replace(' ', '_'))
data.headers = changed_headers
building_res = models.BuildingResource()
res = building_res.import_data(data, False, False)
csv_file = models.CsvFile.objects.get(name=q.__str__())
if res.has_errors() or res.has_validation_errors():
csv_file.status = models.CsvFile.UNSUCCESS
else:
csv_file.status = models.CsvFile.SUCCESS
self.message_user(request, "File with name {} {} imported.".format(q.__str__(),
"was" if csv_file.status == models.CsvFile.SUCCESS
else "wasn't"),
messages.WARNING if csv_file.status == models.CsvFile.UNSUCCESS else messages.SUCCESS)
csv_file.save()

import_files.short_description = "Import selected files"
16 changes: 16 additions & 0 deletions seismic_site/map_app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from crispy_forms.helper import FormHelper
from django import forms

from . import models


class BuildingForm(forms.ModelForm):
class Meta:
model = models.Building
exclude = ['id_general', 'status', 'actualizare_pmb', 'editare_admin', 'lat', 'long']

def __init__(self, *args, **kwargs):
super(BuildingForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.error_text_inline = False
17 changes: 17 additions & 0 deletions seismic_site/map_app/migrations/0010_building_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2.4 on 2019-09-28 13:55

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('map_app', '0009_auto_20190928_1214'),
]

operations = [
migrations.AddField(
model_name='building',
name='status',
field=models.SmallIntegerField(choices=[(0, 'Alege'), (1, 'Acceptă'), (-1, 'Respinge')], default=0),
),
]
13 changes: 13 additions & 0 deletions seismic_site/map_app/migrations/0011_merge_20191002_2225.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 2.2.4 on 2019-10-02 22:25

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
('map_app', '0010_building_status'),
('map_app', '0010_building_an_construire'),
]

operations = [
]
Loading

0 comments on commit 76eb0a3

Please sign in to comment.