Skip to content

Commit

Permalink
Merge pull request #20 from ExCiteS/mstevens-0.6.1
Browse files Browse the repository at this point in the history
v0.6.1
  • Loading branch information
mstevens83 committed Dec 22, 2015
2 parents a304bd5 + 310175e commit 83f9ece
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 98 deletions.
37 changes: 37 additions & 0 deletions geokey_sapelli/helper/install_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import commands
import re

from django.conf import settings

from geokey.applications.models import Application

from .sapelli_exceptions import SapelliException
from .sapelli_loader import get_sapelli_dir_path, get_sapelli_jar_path

MINIMAL_JAVA_VERSION = '1.7.0'

def check_extension():
# Check if SAPELLI_CLIENT_ID value is set in settings.py:
try:
client_id = settings.SAPELLI_CLIENT_ID
except AttributeError:
raise SapelliException('no SAPELLI_CLIENT_ID value set in geokey settings.py.')
# Check if an application is registered with this client_id:
try:
Application.objects.get(client_id=client_id, authorization_grant_type='password')
except Application.DoesNotExist:
raise SapelliException('geokey_sapelli is not registered as an application (with password authorisation) on the server.')
# Check if java 1.7.0 or more recent is installed:
try:
status_output = commands.getstatusoutput('java -version')
if(status_output[0] != 0):
raise SapelliException('java not installed, please install JRE v7 or later.')
java_version = re.match(r'java version "(?P<java_version>[0-9]+\.[0-9]+\.[0-9]+)_.*', status_output[1]).group('java_version')
if(java_version < MINIMAL_JAVA_VERSION):
raise SapelliException('installed version of java is too old (installed: %s, minimum required: %s).' % (java_version, MINIMAL_JAVA_VERSION))
except BaseException, e:
raise SapelliException('could not run java command (%s).' % str(e))
# Check if there is a sapelli working directory:
get_sapelli_dir_path() # raises SapelliException
# Check if we have the sapelli JAR:
get_sapelli_jar_path() # raises SapelliException
27 changes: 14 additions & 13 deletions geokey_sapelli/helper/project_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.files import File

from geokey.projects.models import Project
from geokey.projects.base import EVERYONE_CONTRIB
from geokey.categories.models import Category, Field, LookupValue

from ..models import (
Expand Down Expand Up @@ -41,16 +42,16 @@ def create_implicit_fields(category, stores_end_time=False):

def create_project(sapelli_project_info, user, sap_file_path=None):
geokey_project = Project.create(
sapelli_project_info.get('display_name'),
('Sapelli project id: %s;\nSapelli project fingerprint: %s;' % (
name = sapelli_project_info.get('display_name'),
description = ('Sapelli project id: %s;\nSapelli project fingerprint: %s;' % (
sapelli_project_info.get('sapelli_id'),
sapelli_project_info.get('sapelli_fingerprint'))),
True,
False,
user
isprivate = True,
everyone_contributes = EVERYONE_CONTRIB.false,
creator = user
)

# If anyting below fails the geokey_project will be deleted:
# If anything below fails the geokey_project will be deleted:
try:
sapelli_project = SapelliProject.objects.create(
geokey_project=geokey_project,
Expand Down Expand Up @@ -119,11 +120,6 @@ def create_project(sapelli_project_info, user, sap_file_path=None):
if field_type == 'LookupField':
# Loop over items:
for idx, item in enumerate(field.get('items')):
# Value:
value = LookupValue.objects.create(
name=item.get('value'),
field=geokey_field
)
# Image:
img_relative_path = item.get('img')
img_file = None
Expand All @@ -137,12 +133,17 @@ def create_project(sapelli_project_info, user, sap_file_path=None):
img_file.close()
else:
img_path = None
# Value:
value = LookupValue.objects.create(
name=item.get('value'),
field=geokey_field,
symbol=img_path #pass the path, not the file (otherwise it may be duplicated)
)
# Create SapelliItem:
SapelliItem.objects.create(
lookup_value=value,
sapelli_field=sapelli_field,
number=idx,
image=img_path #pass the path, not the file (otherwise it may be duplicated)
number=idx
)
except BaseException, e:
try: # delete geokey_project:
Expand Down
9 changes: 8 additions & 1 deletion geokey_sapelli/migrations/0010_choice_root_path_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import geokey_sapelli.models


def get_img_path(instance, filename):
if filename is None or instance.sapelli_field.sapelli_form.sapelli_project.dir_path is None:
return None
else:
return os.path.join(instance.sapelli_field.sapelli_form.sapelli_project.dir_path, 'img/', filename)


class Migration(migrations.Migration):

dependencies = [
Expand All @@ -25,6 +32,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='sapelliitem',
name='image',
field=models.ImageField(max_length=500, null=True, upload_to=geokey_sapelli.models.get_img_path),
field=models.ImageField(max_length=500, null=True, upload_to=get_img_path),
),
]
27 changes: 27 additions & 0 deletions geokey_sapelli/migrations/0012_remove_sapelliitem_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


def copy_image_to_lookup_field(apps, schema_editor):
SapelliItem = apps.get_model('geokey_sapelli', 'SapelliItem')
for si in SapelliItem.objects.all():
si.lookup_value.symbol = si.image
si.lookup_value.save()

class Migration(migrations.Migration):

dependencies = [
('geokey_sapelli', '0011_dir_path_sap_path'),
('categories', '0014_category_symbol_lookupvalue_symbol'),
]

operations = [
migrations.RunPython(copy_image_to_lookup_field),

migrations.RemoveField(
model_name='sapelliitem',
name='image',
),
]
22 changes: 22 additions & 0 deletions geokey_sapelli/migrations/0013_sapdownloadqrlink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('oauth2_provider', '0002_08_updates'),
('geokey_sapelli', '0012_remove_sapelliitem_image'),
]

operations = [
migrations.CreateModel(
name='SAPDownloadQRLink',
fields=[
('access_token', models.OneToOneField(primary_key=True, serialize=False, to='oauth2_provider.AccessToken')),
('sapelli_project', models.ForeignKey(to='geokey_sapelli.SapelliProject')),
],
),
]
42 changes: 35 additions & 7 deletions geokey_sapelli/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
import re
import os
import shutil
from datetime import timedelta

from django.db import models
from django.dispatch import receiver
from django.conf import settings
from django.utils import timezone

from geokey.projects.models import Project
from geokey.contributions.models import Observation
from geokey.applications.models import Application

from oauth2_provider.models import AccessToken
from oauthlib.common import generate_token

from .manager import SapelliProjectManager

Expand Down Expand Up @@ -352,12 +359,6 @@ class SapelliField(models.Model):
sapelli_id = models.CharField(max_length=255)
truefalse = models.BooleanField(default=False)


def get_img_path(instance, filename):
if filename is None or instance.sapelli_field.sapelli_form.sapelli_project.dir_path is None:
return None
else:
return os.path.join(instance.sapelli_field.sapelli_form.sapelli_project.dir_path, 'img/', filename)

class SapelliItem(models.Model):
"""
Expand All @@ -369,9 +370,36 @@ class SapelliItem(models.Model):
primary_key=True,
related_name='sapelli_item'
)
image = models.ImageField(upload_to=get_img_path, null=True, max_length=500)
number = models.IntegerField()
sapelli_field = models.ForeignKey(
'SapelliField',
related_name='items'
)


class SAPDownloadQRLink(models.Model):
access_token = models.OneToOneField('oauth2_provider.AccessToken', primary_key=True)
sapelli_project = models.ForeignKey(SapelliProject)

@classmethod
def create(cls, user, sapelli_project, days_valid=1):
a_t = AccessToken.objects.create(
user=user,
application=Application.objects.get(client_id=settings.SAPELLI_CLIENT_ID),
expires=timezone.now() + timedelta(days=days_valid),
token=generate_token(),
scope='read')
qr_link = cls(access_token = a_t, sapelli_project = sapelli_project)
qr_link.save()
return qr_link


@receiver(models.signals.pre_delete, sender=AccessToken)
def pre_delete_access_token(sender, instance, **kwargs):
"""
Receiver that is called after an AccessToken is deleted. Deletes related SAPDownloadQRLink.
"""
try:
SAPDownloadQRLink.objects.get(access_token=instance).delete()
except BaseException:
pass

0 comments on commit 83f9ece

Please sign in to comment.