/SmartSystemCore

This repository will contain the implementation of the Core of the smart system concept. Only Release versions will be found here


Fraunhofer


Fraunhofer Fraunhofer Fraunhofer Fraunhofer

Setup

Prerequisites for development enviorment

  1. Make sure you have installed python on your machine. Download Python
  2. pip package manager is installed. If you have a version of python 2 >= 2.7.9 or Python 3 >= 3.4 you will probably have pip installed if not download pip and in Terminal run python get-pip.py. If you have problems pip documentation
  3. This step is not mandatory but recommended is to make a python virtual environment sudo pip install virtualenv
    • Unix based operating systems
      • Installation of virtualenv sudo pip install virtualenv
      • Create a directory to hold the project mkdir ~/projectname
      • Create a virtual environment inside the project folder virtualenv nameofprojectenv
  4. PostgreSQL is the database in use so make sure you have it installed.
    • Unix based operating systems
      • updating system variables via Terminal command sudo apt-get update
      • Installing PostgreSQL via Terminal command sudo apt-get install libpq-dev postgresql postgresql-contrib updating system variables
      • Your operating system will make a default user name called postgres log in with the command sudo su - postgres
      • Creating a new database CREATE DATABASE databasename
      • Creating a new user CREATE USER username WITH PASSWORD 'secret'
      • To test if you have installed it correctly use command psql -U username -d databasename
    • Windows operationg systems
      • coming soon!

Development environment setup

  1. Install all requirments run command in the project directory pip install -r requirements.txt
  2. Setting up database, add this code to setings.py
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": "databasename",
            "USER": "username",
            "PASSWORD": "secret",
            "HOST": "localhost",
            "PORT": "",
        }
    }
  3. Check if the DEBUG = True is set in settings.py
  4. Make migrations for database python manage.py makemigrations
  5. Migrate models.py to postgres with command python manage.py migrate
  6. Run a the test server with command python manage.py runserver this will run the server on the default port 8000 if that port is taken you can change the port that the test server runs on with python manage.py runserver 0.0.0.0:8090

Production environment setup

Coming soon

Extending a models.py for database

Prerequisites

  1. Import models from framework from django.db import models
  2. Import settings from framework from django.conf import settings
  3. Import timestampedmodel from database models from django_extensions.db.models import TimeStampedModel

Overview

Example

class Example(models.Model):
       city = models.CharField(max_length=50)
       street = models.CharField(max_length=45)
       postcode = models.CharField(max_length=45)
       country = models.CharField(max_length=45)
       nr_Of_Floors = models.IntegerField()
       building_name = models.CharField(max_length=45)  

If you need to implement enum as a field gender = models.CharField(max_length=1, choices=ENUMGENDER)

   ENUMGENDER = (
  ('M', 'Male'),
  ('F', 'Female'),
  )

Extending serializables

Overview

serializable.py is resopnsible for serializing the data from the orm to json format.

Create a new serializable class

  1. Importing serializers

    Example

    from rest_framework import serializers
    from .models import ModelClass
  2. Creating a new serializable class to json

    Note: That these classes work with serializers.HyperlinkedModelSerializer passed as a parameter

    • serializing all fields

    Example

    class ModelClassSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = ModelClass
            fields = "__all__"
    • serializing custom fields

    Example

    class ModelClassSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = ModelClass
            fields = ('modelfield', 'modelfield', 'modelfield', 'modelfield')

Extending urls

Overview

The REST framework supports automatic urls routing this is a quick way yo make ulrs.

Prerequisites

  1. Import urls from framework

    Example

    from django.conf.urls import url, include
    from django.urls import path
  2. Import views from gatherer directory

    Example

    from . import views

Router

  1. Create a Router

    • DefaultRouter

    Example

    router = routers.DefaultRouter()
    • SimpleRouter

    Example

    router = routers.SimpleRouter()
  2. Register a Serializer

    Example

    router.register('url_name', views.ModelClassViewSet)
  3. Add router to application urls

    Example

    urlpatterns = [
      path('', include(router.urls))
    ]

Extending views

Overview

Creating view sets from the models in the model class.

Prerequisites

  1. Import HttpResponse, render from framework from django.shortcuts import render, HttpResponse
  2. Import rest_framework Example
    from rest_framework import generics, viewsets
    from rest_framework.response import Response
    from rest_framework import status
    from rest_framework.reverse import reverse
    from rest_framework.views import APIView
  3. Import serializers from .serializers import ExampleSerializer
  4. Import models from .models import Example

Create a view

class ExampleViewSet(viewsets.ModelViewSet):
    queryset = Example.objects.all()
    serializer_class = ExampleSerializer

Extending admin

  1. Customizing a admin model

    • Custom class Example sdadasdasd
     class ModelClassAdmin(admin.ModelAdmin):
     list_display = ('modelfield', 'modelfield')
     model = models.ModelClass
    • Register a custom class

    Example

    admin.site.register(models.ModelClass, ModelClassAdmin)
    • Register with framework default

    Example

    admin.site.register(models.ModelClass)

Authors

  • Martin Savov - Initial work - MIT120
  • Hristiyan Tarnev - Initial work - Beasteca
  • Ralitsa Kehayova - Initial work - Shortcut21
  • Artjom Leonov - Initial work - qniff

See also the list of contributors who participated in this project.

License

Copyright (c) <2018>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.