Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhaney committed Jan 31, 2020
2 parents 2f1336b + 81de6c5 commit 608c3cd
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ lambda-refresh_materialized_views/*
!lambda-refresh_materialized_views/lambda_function.py
!lambda-refresh_materialized_views/requirements.txt

lambda-update_database_record_utility/*
!lambda-update_database_record_utility/lambda_function.py
!lambda-update_database_record_utility/requirements.txt

vault-password\.txt
.vault/

Expand Down
46 changes: 46 additions & 0 deletions lambda-update_database_record_utility/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# --------------- IMPORTS ---------------
import os, psycopg2

# Database Connection Info
database = os.environ.get('DB_NAME')
username = os.environ.get('DB_USER')
password = os.environ.get('DB_PASSWORD')
host = os.environ.get('DB_HOST')
port = os.environ.get('DB_PORT')

conn_string = "dbname='%s' user='%s' host='%s' password='%s' port='%s'" % (database, username, host, password, port)

# --------------- Main handler ------------------

def lambda_handler(event, context):
print(event)
table = event['table']
field = event['field']
value = event['value']
try:
where = event['where']
except:
print('No "where" key in event!!!')
event['where'] = None
# connect to database
conn = psycopg2.connect(conn_string)
cur = conn.cursor()

if isinstance(value, str):
value = "'" + value + "'"

q = "UPDATE %s SET %s = %s;" % (table, field, value)

if event['where'] != None:
q = q.replace(";", " WHERE %s;" % where)

print(q)
cur.execute(q)
conn.commit()

cur.close()
conn.close()
print("that's all folks!!")

if __name__ == '__main__':
lambda_handler(event='event', context='context')
2 changes: 2 additions & 0 deletions lambda-update_database_record_utility/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg2==2.7.7
psycopg2-binary==2.7.7
35 changes: 28 additions & 7 deletions src/data_hub/msd/forms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from django import forms
from django.core.exceptions import ValidationError
# from django.db.utils import ProgrammingError

from .models import MapCollection, MapDataRelate, MapDownload, MapSize, PixelsPerInch
from lcd.models import Collection

from string import Template
from django.utils.safestring import mark_safe

import boto3, os
from django.db.utils import ProgrammingError
from .models import (
MapCollection,
MapDataRelate,
MapDownload,
MapSize,
PixelsPerInch
)

import os
import boto3

from lcd.models import Collection


class PictureWidget(forms.widgets.Widget):
Expand All @@ -19,7 +27,20 @@ def render(self, name, value, attrs=None):

class PdfWidget(forms.widgets.Widget):
def render(self, name, value, attrs=None):
html = Template("""<input type="file" name="$name" id="id_$name"><label for="img_$name" style='font-weight:bold;'>Current: $link</label>""")
js = """
<script type="text/javascript">
function copyFunction() {
var copyText = document.getElementById("currentUrl");
copyText.select();
document.execCommand("copy");
}
</script>
"""

if value:
html = Template("""{0}<div style="margin-bottom:10px;"><a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="copyFunction();">COPY URL</a></div><div alt="$link" style="margin-bottom:10px;"><input id="currentUrl" value="$link" readonly style="width: 80%;padding:3px;cursor:default;"></input></div>""".format(js))
else:
html = Template("""<input type="file" name="$name" id="id_$name"></input>""")
return mark_safe(html.substitute(link=value,name=name))


Expand All @@ -33,7 +54,7 @@ class Meta:
'label'
)

download_url = forms.FileField(required=False, widget=PdfWidget, help_text="Upload map download file. PDF is recommended. 75MB max size.")
download_url = forms.FileField(required=False, widget=PdfWidget, help_text="Upload map download file. PDF is recommended. 75MB max size. Overwriting files is not allowed. Delete the record and create a new one with the new file if you are attempting to overwrite.")

# FILE UPLOAD FOR DOWNLOAD FILE (PDF SUGGESTED)
# boto3 s3 object
Expand Down
19 changes: 3 additions & 16 deletions src/data_hub/tnris_org/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def image_url_link(self, obj):
""".replace('{id}', htmlId)
js = format_html(js)
return format_html(
u'{0}<a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="{1}Function();">COPY URL</a><input style="width:90%;margin-left:5px;" type="text" id="{2}" value="{3}" readonly>',
u'{0}<div style="margin-bottom:10px;"><a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="{1}Function();">COPY URL</a></div><div><input style="width:90%;" type="text" id="{2}" value="{3}" readonly></input></div>',
js,
htmlId,
htmlId,
Expand All @@ -72,7 +72,7 @@ class TnrisDocumentAdmin(admin.ModelAdmin):
model = TnrisDocument
form = DocumentForm
ordering = ('document_name',)
list_display = ('document_name', 'document_url_link', 'tiny_preview', 'created')
list_display = ('document_name', 'document_url_link', 'created')
search_fields = ('document_name', 'document_url')

def document_url_link(self, obj):
Expand All @@ -89,26 +89,13 @@ def document_url_link(self, obj):
""".replace('{id}', htmlId)
js = format_html(js)
return format_html(
u'{0}<a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="{1}Function();">COPY URL</a><input style="width:90%;margin-left:5px;" type="text" id="{2}" value="{3}" readonly>',
u'{0}<div style="margin-bottom:10px;"><a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="{1}Function();">COPY URL</a></div><div><input style="width:90%;" type="text" id="{2}" value="{3}" readonly></input></div>',
js,
htmlId,
htmlId,
obj.document_url
)

def tiny_preview(self, obj):
# tiny preview of .pdf files errors/breaks on page load when src is cached.
# this means all tiny previews display on hard reload, but .pdfs don't on
# normal reload. so, we append a current datetime query string to the request
# so the browser recognizes each load as a new request and doesn't load the
# cached version.
no_cache = str(datetime.today())
return format_html(
u'<embed style="max-width: 120px;" src="{0}?d={1}"></embed>',
obj.document_url,
no_cache
)


@admin.register(TnrisTraining)
class TnrisTrainingAdmin(admin.ModelAdmin):
Expand Down
9 changes: 5 additions & 4 deletions src/data_hub/tnris_org/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def render(self, name, value, attrs=None):
if value is None:
html = Template("""<input type="file" name="$name" id="id_$name"><label for="img_$name">Current: <a href="#">$link</a></label>""")
else:
html = Template("""{0}<a style="cursor:pointer;border:solid 1px;padding:3px;margin-left:15px;" onclick="copyFunction();">COPY URL</a><input style="width:50%;margin-left:5px;" type="text" id="currentUrl" value="$link" readonly><br><img id="img_$name" src="$link"/>""".format(js))
html = Template("""{0}<div style="margin-bottom:10px;"><a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="copyFunction();">COPY URL</a></div><div style="margin-bottom:10px;"><input style="width:50%;" type="text" id="currentUrl" value="$link" readonly></input></div><div style="margin-bottom:10px;"><img id="img_$name" src="$link"/></div>""".format(js))
return mark_safe(html.substitute(link=value,name=name))


Expand All @@ -51,7 +51,7 @@ def render(self, name, value, attrs=None):
if value is None:
html = Template("""<input type="file" name="$name" id="id_$name"><label for="doc_$name">Current: <a href="#">$link</a></label>""")
else:
html = Template("""{0}<a style="cursor:pointer;border:solid 1px;padding:3px;margin-left:15px;" onclick="copyFunction();">COPY URL</a><input style="width:50%;margin-left:5px;" type="text" id="currentUrl" value="$link" readonly><br><embed style="max-width:80%;max-height:600px;" src="$link"></embed>""".format(js))
html = Template("""{0}<div style="margin-bottom:10px;"><a style="cursor:pointer;border:solid 1px;padding:3px;" onclick="copyFunction();">COPY URL</a></div><div style="margin-bottom:10px;"><input style="width:50%;" type="text" id="currentUrl" value="$link" readonly></input></div>""".format(js))
return mark_safe(html.substitute(link=value,name=name))


Expand All @@ -69,7 +69,7 @@ class Meta:
model = TnrisImage
fields = ('__all__')

image_url = forms.FileField(required=False, widget=PictureWidget, help_text="Choose an image file and 'Save' this form to upload & save it to the database. Attempting to overwrite with a new file will only create a new record.")
image_url = forms.FileField(required=False, widget=PictureWidget, help_text="Choose an image file and 'Save' this form to upload & save it to the database. Attempting to overwrite with a new file will only create a new record. The best method to overwrite would be to delete the existing file and re-upload a new file with the same name.")

# boto3 s3 object
client = boto3.client('s3')
Expand Down Expand Up @@ -128,7 +128,8 @@ class Meta:
model = TnrisDocument
fields = ('__all__')

document_url = forms.FileField(required=False, widget=DocumentWidget, help_text="Choose a document file and 'Save' this form to upload & save it to the database. Attempting to overwrite with a new file will only create a new record.")
document_url = forms.FileField(required=False, widget=DocumentWidget, help_text="Choose a document file and 'Save' this form to upload & save it to the database. Attempting to overwrite with a new file will only create a new record. The best method to overwrite would be to delete the existing file and re-upload a new file with the same name.")
sgm_note = forms.BooleanField(required=False, label="GIS Solutions Group Notes", help_text="Check this box to identify this as a Solutions Group Notes document. This is required to view the document on tnris.org. Be sure to name the file correctly - 'YYYY-MM-DD-GIS-SG-Meeting-Notes.pdf'. The file name is important for the order these documents are presented on tnris.org.")

# boto3 s3 object
client = boto3.client('s3')
Expand Down
18 changes: 18 additions & 0 deletions src/data_hub/tnris_org/migrations/0024_tnrisdocument_sgm_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.13 on 2020-01-31 17:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tnris_org', '0023_tnrisgiocalendarevent_community_meeting_agenda_url'),
]

operations = [
migrations.AddField(
model_name='tnrisdocument',
name='sgm_note',
field=models.BooleanField(default=False, verbose_name='Solutions Group Note'),
),
]
5 changes: 5 additions & 0 deletions src/data_hub/tnris_org/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class Meta:
'Document URL',
max_length=255,
)
sgm_note = models.BooleanField(
'Solutions Group Note',
default=False,
null=False
)
created = models.DateTimeField(
'Created',
auto_now_add=True
Expand Down
14 changes: 13 additions & 1 deletion src/data_hub/tnris_org/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
TnrisForumTraining,
TnrisInstructorType,
CompleteForumTrainingView,
TnrisGioCalendarEvent
TnrisGioCalendarEvent,
TnrisDocument
)
from datetime import datetime

Expand Down Expand Up @@ -148,3 +149,14 @@ def get_pretty_time(self, obj):
pt = "%s-%s" % (st.strftime('%I:%M%p').lstrip("0"),
et.strftime('%I:%M%p').lstrip("0"))
return pt


class TnrisSGMDocumentSerializer(serializers.ModelSerializer):
class Meta:
model = TnrisDocument
fields = ('document_id',
'document_name',
'document_url',
'sgm_note',
'created',
'last_modified',)
2 changes: 2 additions & 0 deletions src/data_hub/tnris_org/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
TnrisInstructorTypeViewSet,
CompleteForumTrainingViewSet,
TnrisGioCalendarEventViewSet,
TnrisSGMDocumentViewSet,
)

router = routers.DefaultRouter(trailing_slash=False)
Expand All @@ -32,6 +33,7 @@
router.register(r'complete_forum_training/?', CompleteForumTrainingViewSet, base_name="CompleteForumTrainingView")
router.register(r'instructor_type/?', TnrisInstructorTypeViewSet, base_name="TnrisInstructorType")
router.register(r'gio_calendar/?', TnrisGioCalendarEventViewSet, base_name="TnrisGioCalendarEvent")
router.register(r'sgm_note/?', TnrisSGMDocumentViewSet, base_name="TnrisDocument")

schema_view = get_swagger_view(title='TNRIS.org API')

Expand Down
23 changes: 20 additions & 3 deletions src/data_hub/tnris_org/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
TnrisForumTraining,
TnrisInstructorType,
CompleteForumTrainingView,
TnrisGioCalendarEvent
TnrisGioCalendarEvent,
TnrisDocument
)
from .serializers import (
TnrisTrainingSerializer,
TnrisForumTrainingSerializer,
TnrisInstructorTypeSerializer,
CompleteForumTrainingViewSerializer,
TnrisGioCalendarEventSerializer
TnrisGioCalendarEventSerializer,
TnrisSGMDocumentSerializer
)


Expand Down Expand Up @@ -130,4 +132,19 @@ def get_queryset(self):
args[field] = value
# get records using query. order by chronological start
queryset = TnrisGioCalendarEvent.objects.filter(**args).order_by('start_date', 'start_time')
return queryset
return queryset


class TnrisSGMDocumentViewSet(viewsets.ReadOnlyModelViewSet):
"""
Retrieve all Solutions Group Meeting Documents for tnris.org frontend
"""
serializer_class = TnrisSGMDocumentSerializer
http_method_names = ['get']

def get_queryset(self):
# get records using query
args = {'sgm_note': True}
# order by document file name
queryset = TnrisDocument.objects.filter(**args).order_by('document_name')
return queryset

0 comments on commit 608c3cd

Please sign in to comment.