# This Python file uses the following encoding: utf-8

# Part of the Lookback project (https://github.com/lindegroup/lookback)
# Copyright 2015 The Linde Group Computer Support, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals
from django.db import models
from config.crypt import CryptoHelper


class Configuration(models.Model):
    """This model stores all configuration options for accessing the external
    system."""
    url = models.URLField('URL')
    user = models.CharField('Username', max_length=255)
    password = models.CharField('Password', max_length=512)

    # System type choices
    JSS = 'JSS'
    SYSTEM_TYPE_CHOICES = (
        (JSS, 'JSS'),
    )
    system_type = models.CharField('System Type',
                                   choices=SYSTEM_TYPE_CHOICES,
                                   default=JSS,
                                   max_length=32)

    def save(self, *args, **kwargs):
        """automatically encrypt during save"""
        helper = CryptoHelper()
        self.password = helper.encrypt(self.password)
        super(Configuration, self).save(*args, **kwargs)

    def decrypt_password(self):
        helper = CryptoHelper()
        return helper.decrypt(self.password)
