<commit_before>import logging

from markdown2 import Markdown

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.views.generic import CreateView, DetailView, UpdateView, View
from django.views.generic.list import ListView
from django.contrib.auth.decorators import login_required

from experiments_manager.helper import verify_and_get_experiment
from experiments_manager.models import ChosenExperimentSteps
from experiments_manager.mixins import ActiveExperimentsList
from helpers.helper_mixins import ExperimentPackageTypeMixin
from user_manager.models import get_workbench_user
from requirements_manager.helper import add_internalpackage_to_experiment
from git_manager.helpers.github_helper import GitHubHelper
from git_manager.mixins.repo_file_list import get_files_for_repository

from marketplace.forms import InternalPackageForm
from marketplace.helpers.helper import create_tag_for_package_version
from marketplace.helpers.helper import update_setup_py_with_new_version
from marketplace.models import InternalPackage, PackageVersion, PackageResource
from marketplace.tasks import task_create_package_from_experiment, task_publish_update_package
from marketplace.tasks import task_remove_package
from marketplace.mixins import IsInternalPackageMixin, ObjectTypeIdMixin

logger = logging.getLogger(__name__)

class InternalPackageBaseView(ObjectTypeIdMixin, IsInternalPackageMixin):
    class Meta:
        abstract = True

class InternalPackageCreateView(ExperimentPackageTypeMixin, CreateView):
    model = InternalPackage
    form_class = InternalPackageForm
    template_name = 'marketplace/package_create/package_form.html'
    success_url = '/packages/new/status'

    def get_context_data(self, **kwargs):
        context = super(InternalPackageCreateView, self).get_context_data(**kwargs)
        context['experiment_id'] = self.kwargs['experiment_id']
        context['step_id'] = self.kwargs['step_id']

        logger.debug('%s started on package creation for %d', self.request.user, self.kwargs['experiment_id'])
        return context

    def form_valid(self, form):
        step_folder = self.get_step().location
        experiment = self.get_experiment()
        form.instance.owner = experiment.owner
        form.instance.template_id = 1
        response = super(InternalPackageCreateView, self).form_valid(form)
        task_create_package_from_experiment.delay(form.instance.pk, experiment.pk, step_folder)
        return response

    def get_experiment(self):
        experiment_id = self.kwargs['experiment_id']
        experiment = verify_and_get_experiment(self.request, experiment_id)
        return experiment

    def get_step(self):
        step_id = self.kwargs['step_id']
        step = ChosenExperimentSteps.objects.get(pk=step_id)
        return step

class InternalPackageListView(ListView):
    model = InternalPackage

    def get_queryset(self):
        qs = super(InternalPackageListView, self).get_queryset()
        return qs.filter(owner__user_id=self.request.user.id)

class InternalPackageDashboard(ExperimentPackageTypeMixin, View):
    def get(self, request, pk):
        package = get_object_or_404(InternalPackage, pk=pk)

        assert package.owner.user == self.request.user

        context = {'docs': package.docs,
                   'package': package,
                   'object_id': package.pk,
                   'object_type': package.get_object_type(),
                   'edit_form': InternalPackageForm(instance=package),
                   'dashboard_active': True,
                   'is_internal': True}
        return render(request, 'marketplace/package_detail/internalpackage_dashboard.html', context)

class InternalPackageUpdateView(UpdateView):
    model = InternalPackage
    form_class = InternalPackageForm

    def get_success_url(self):
        return reverse('internalpackage_dashboard', kwargs={'pk': self.kwargs['pk']})

    def form_valid(self, form):
        assert form.instance.owner.user == self.request.user
        messages.add_message(self.request, messages.SUCCESS, 'Package successfully updated')
        return super(InternalPackageUpdateView, self).form_valid(form)

class InternalPackageVersionCreateView(CreateView):
    model = PackageVersion
    fields = ['version_nr', 'changelog', 'pre_release']
    template_name = 'marketplace/package_detail/packageversion_form.html'

    def get_context_data(self, **kwargs):
        context = super(InternalPackageVersionCreateView, self).get_context_data(**kwargs)
        context['package'] = InternalPackage.objects.get(id=self.kwargs['package_id'])
        return context

    def form_valid(self, form):
        assert form.instance.owner.user == self.request.user
        package = InternalPackage.objects.get(id=self.kwargs['package_id'])
        form.instance.package = package
        form.instance.added_by = get_workbench_user(self.request.user)
        response = super(InternalPackageVersionCreateView, self).form_valid(form)
        create_tag_for_package_version(form.instance.id)
        update_setup_py_with_new_version(form.instance.id)
        return response

    def get_success_url(self):
        return reverse('internalpackage_dashboard', kwargs={'pk': self.kwargs['package_id']})

@login_required
def internalpackage_publish(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    task_publish_update_package.delay(package.pk)
    logger.debug('%s published the package %d', request.user, package)
    return redirect(to=package.get_absolute_url())

@login_required
def internalpackage_publish_checklist(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    dependencies_defined = package.requirements.count() != 0
    getting_started_guide = PackageResource.objects.filter(package=package.id, title='Getting started')
    getting_started = False
    if getting_started_guide:
        getting_started_guide = getting_started_guide[0]
        getting_started = len(getting_started_guide.resource) != 0
    return render(request, 'marketplace/package_publish.html', {'object': package,
                                                                'dependencies_defined': dependencies_defined,
                                                                'getting_started': getting_started})

@login_required
def internalpackage_remove(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    task_remove_package.delay(package.pk)
    return JsonResponse({"publish": "started"})

class InternalPackageDetailView(InternalPackageBaseView, ActiveExperimentsList, DetailView):
    model = InternalPackage
    template_name = 'marketplace/package_detail/package_detail.html'

    def get_context_data(self, **kwargs):
        self.object_type = ExperimentPackageTypeMixin.PACKAGE_TYPE
        context = super(InternalPackageDetailView, self).get_context_data(**kwargs)
        package_id = self.kwargs['pk']
        context['version_history'] = PackageVersion.objects.filter(package=package_id).order_by('-created')[:5]
        context['index_active'] = True
        context['git_list'] = get_files_for_repository(self.object)
        if InternalPackage.objects.filter(pk=self.object.pk):
            context['readme'] = self.readme_file_of_package()
        return context

    def readme_file_of_package(self):
        internalpackage = InternalPackage.objects.get(id=self.kwargs['pk'])
        github_helper = GitHubHelper(internalpackage.owner, internalpackage.git_repo.name)
        readme = github_helper.view_file('README.md')
        md = Markdown()
        content_file = md.convert(readme)
        return content_file

@login_required
def internalpackage_install(request, pk):
    internal_package = InternalPackage.objects.get(pk=pk)
    assert 'experiment_id' in request.POST
    experiment_id = request.POST['experiment_id']
    experiment = verify_and_get_experiment(request, experiment_id)
    result = add_internalpackage_to_experiment(internal_package, experiment)
    if result:
        messages.add_message(request, messages.SUCCESS, 'Added package to your experiment')
        return JsonResponse({'added': True})
    else:
        messages.add_message(request, messages.ERROR, 'Could not add package to your experiment')
        return JsonResponse({'added': False})
<commit_msg>Fixed bug adding package version<commit_after>import logging

from markdown2 import Markdown

from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.views.generic import CreateView, DetailView, UpdateView, View
from django.views.generic.list import ListView
from django.contrib.auth.decorators import login_required

from experiments_manager.helper import verify_and_get_experiment
from experiments_manager.models import ChosenExperimentSteps
from experiments_manager.mixins import ActiveExperimentsList
from helpers.helper_mixins import ExperimentPackageTypeMixin
from user_manager.models import get_workbench_user
from requirements_manager.helper import add_internalpackage_to_experiment
from git_manager.helpers.github_helper import GitHubHelper
from git_manager.mixins.repo_file_list import get_files_for_repository

from marketplace.forms import InternalPackageForm
from marketplace.helpers.helper import create_tag_for_package_version
from marketplace.helpers.helper import update_setup_py_with_new_version
from marketplace.models import InternalPackage, PackageVersion, PackageResource
from marketplace.tasks import task_create_package_from_experiment, task_publish_update_package
from marketplace.tasks import task_remove_package
from marketplace.mixins import IsInternalPackageMixin, ObjectTypeIdMixin

logger = logging.getLogger(__name__)

class InternalPackageBaseView(ObjectTypeIdMixin, IsInternalPackageMixin):
    class Meta:
        abstract = True

class InternalPackageCreateView(ExperimentPackageTypeMixin, CreateView):
    model = InternalPackage
    form_class = InternalPackageForm
    template_name = 'marketplace/package_create/package_form.html'
    success_url = '/packages/new/status'

    def get_context_data(self, **kwargs):
        context = super(InternalPackageCreateView, self).get_context_data(**kwargs)
        context['experiment_id'] = self.kwargs['experiment_id']
        context['step_id'] = self.kwargs['step_id']

        logger.debug('%s started on package creation for %d', self.request.user, self.kwargs['experiment_id'])
        return context

    def form_valid(self, form):
        step_folder = self.get_step().location
        experiment = self.get_experiment()
        form.instance.owner = experiment.owner
        form.instance.template_id = 1
        response = super(InternalPackageCreateView, self).form_valid(form)
        task_create_package_from_experiment.delay(form.instance.pk, experiment.pk, step_folder)
        return response

    def get_experiment(self):
        experiment_id = self.kwargs['experiment_id']
        experiment = verify_and_get_experiment(self.request, experiment_id)
        return experiment

    def get_step(self):
        step_id = self.kwargs['step_id']
        step = ChosenExperimentSteps.objects.get(pk=step_id)
        return step

class InternalPackageListView(ListView):
    model = InternalPackage

    def get_queryset(self):
        qs = super(InternalPackageListView, self).get_queryset()
        return qs.filter(owner__user_id=self.request.user.id)

class InternalPackageDashboard(ExperimentPackageTypeMixin, View):
    def get(self, request, pk):
        package = get_object_or_404(InternalPackage, pk=pk)

        assert package.owner.user == self.request.user

        context = {'docs': package.docs,
                   'package': package,
                   'object_id': package.pk,
                   'object_type': package.get_object_type(),
                   'edit_form': InternalPackageForm(instance=package),
                   'dashboard_active': True,
                   'is_internal': True}
        return render(request, 'marketplace/package_detail/internalpackage_dashboard.html', context)

class InternalPackageUpdateView(UpdateView):
    model = InternalPackage
    form_class = InternalPackageForm

    def get_success_url(self):
        return reverse('internalpackage_dashboard', kwargs={'pk': self.kwargs['pk']})

    def form_valid(self, form):
        assert form.instance.owner.user == self.request.user
        messages.add_message(self.request, messages.SUCCESS, 'Package successfully updated')
        return super(InternalPackageUpdateView, self).form_valid(form)

class InternalPackageVersionCreateView(CreateView):
    model = PackageVersion
    fields = ['version_nr', 'changelog', 'pre_release']
    template_name = 'marketplace/package_detail/packageversion_form.html'

    def get_context_data(self, **kwargs):
        context = super(InternalPackageVersionCreateView, self).get_context_data(**kwargs)
        context['package'] = InternalPackage.objects.get(id=self.kwargs['package_id'])
        return context

    def form_valid(self, form):
        assert form.instance.added_by.user == self.request.user
        package = InternalPackage.objects.get(id=self.kwargs['package_id'])
        form.instance.package = package
        form.instance.added_by = get_workbench_user(self.request.user)
        response = super(InternalPackageVersionCreateView, self).form_valid(form)
        create_tag_for_package_version(form.instance.id)
        update_setup_py_with_new_version(form.instance.id)
        return response

    def get_success_url(self):
        return reverse('internalpackage_dashboard', kwargs={'pk': self.kwargs['package_id']})

@login_required
def internalpackage_publish(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    task_publish_update_package.delay(package.pk)
    logger.debug('%s published the package %d', request.user, package)
    return redirect(to=package.get_absolute_url())

@login_required
def internalpackage_publish_checklist(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    dependencies_defined = package.requirements.count() != 0
    getting_started_guide = PackageResource.objects.filter(package=package.id, title='Getting started')
    getting_started = False
    if getting_started_guide:
        getting_started_guide = getting_started_guide[0]
        getting_started = len(getting_started_guide.resource) != 0
    return render(request, 'marketplace/package_publish.html', {'object': package,
                                                                'dependencies_defined': dependencies_defined,
                                                                'getting_started': getting_started})

@login_required
def internalpackage_remove(request, pk):
    package = InternalPackage.objects.get(id=pk)
    assert package.owner.user == request.user
    task_remove_package.delay(package.pk)
    return JsonResponse({"publish": "started"})

class InternalPackageDetailView(InternalPackageBaseView, ActiveExperimentsList, DetailView):
    model = InternalPackage
    template_name = 'marketplace/package_detail/package_detail.html'

    def get_context_data(self, **kwargs):
        self.object_type = ExperimentPackageTypeMixin.PACKAGE_TYPE
        context = super(InternalPackageDetailView, self).get_context_data(**kwargs)
        package_id = self.kwargs['pk']
        context['version_history'] = PackageVersion.objects.filter(package=package_id).order_by('-created')[:5]
        context['index_active'] = True
        context['git_list'] = get_files_for_repository(self.object)
        if InternalPackage.objects.filter(pk=self.object.pk):
            context['readme'] = self.readme_file_of_package()
        return context

    def readme_file_of_package(self):
        internalpackage = InternalPackage.objects.get(id=self.kwargs['pk'])
        github_helper = GitHubHelper(internalpackage.owner, internalpackage.git_repo.name)
        readme = github_helper.view_file('README.md')
        md = Markdown()
        content_file = md.convert(readme)
        return content_file

@login_required
def internalpackage_install(request, pk):
    internal_package = InternalPackage.objects.get(pk=pk)
    assert 'experiment_id' in request.POST
    experiment_id = request.POST['experiment_id']
    experiment = verify_and_get_experiment(request, experiment_id)
    result = add_internalpackage_to_experiment(internal_package, experiment)
    if result:
        messages.add_message(request, messages.SUCCESS, 'Added package to your experiment')
        return JsonResponse({'added': True})
    else:
        messages.add_message(request, messages.ERROR, 'Could not add package to your experiment')
        return JsonResponse({'added': False})
<|endoftext|>


<commit_before>import socket, string, types
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdgen, mibvar
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.smi import view
from pysnmp import error
from pyasn1.type import univ

# Auth protocol
usmHMACMD5AuthProtocol = config.usmHMACMD5AuthProtocol
usmHMACSHAAuthProtocol = config.usmHMACSHAAuthProtocol
usmNoAuthProtocol = config.usmNoAuthProtocol

# Privacy protocol
usmDESPrivProtocol = config.usmDESPrivProtocol
usmAesCfb128Protocol = config.usmAesCfb128Protocol
usmNoPrivProtocol = config.usmNoPrivProtocol

class CommunityData:
    mpModel=1 # Default is SMIv2
    securityModel=mpModel+1
    securityLevel='noAuthNoPriv'
    def __init__(self, securityName, communityName, mpModel=None):
        self.securityName = securityName
        self.communityName = communityName
        if mpModel is not None:
            self.mpModel = mpModel
            self.securityModel = mpModel + 1
        self.__hash = hash((self.mpModel, self.securityModel, self.securityLevel, self.securityName, self.communityName))

    def __repr__(self): return '%s("%s", <COMMUNITY>, %s)' % (
        self.__class__.__name__,
        self.securityName,
        self.mpModel
        )

    def __hash__(self): return self.__hash
    def __cmp__(self, other): return cmp(self.__hash, other)

class UsmUserData:
    authKey = privKey = None
    authProtocol = usmNoAuthProtocol
    privProtocol = usmNoPrivProtocol
    securityLevel='noAuthNoPriv'
    securityModel=3
    mpModel=2
    def __init__(self, securityName,
                 authKey=None, privKey=None,
                 authProtocol=None, privProtocol=None):
        self.securityName = securityName

        if authKey is not None:
            self.authKey = authKey
            if authProtocol is None:
                self.authProtocol = usmHMACMD5AuthProtocol
            else:
                self.authProtocol = authProtocol
            if self.securityLevel != 'authPriv':
                self.securityLevel = 'authNoPriv'

        if privKey is not None:
            self.privKey = privKey
            if self.authProtocol == usmNoAuthProtocol:
                raise error.PySnmpError('Privacy implies authenticity')
            self.securityLevel = 'authPriv'
            if privProtocol is None:
                self.privProtocol = usmDESPrivProtocol
            else:
                self.privProtocol = privProtocol

        self.__hash = hash((self.mpModel, self.securityModel, self.securityLevel, self.securityName, self.authProtocol, self.authKey, self.privProtocol, self.privKey))

    def __repr__(self): return '%s("%s", <AUTHKEY>, <PRIVKEY>, %s, %s)' % (
        self.__class__.__name__,
        self.securityName,
        self.authProtocol,
        self.privProtocol
        )

    def __hash__(self): return self.__hash
    def __cmp__(self, other): return cmp(self.__hash, other)

class UdpTransportTarget:
    transportDomain = udp.domainName
    def __init__(self, transportAddr, timeout=1, retries=5):
        self.transportAddr = (
            socket.gethostbyname(transportAddr[0]), transportAddr[1]
            )
        self.timeout = timeout
        self.retries = retries

    def __repr__(self): return '%s(("%s", %s), %s, %s)' % (
        self.__class__.__name__,
        self.transportAddr[0], self.transportAddr[1],
        self.timeout, self.retries
        )

    def openClientMode(self):
        self.transport = udp.UdpSocketTransport().openClientMode()
        return self.transport

class AsynCommandGenerator:
    _null = univ.Null('')
    def __init__(self, snmpEngine=None):
        if snmpEngine is None:
            self.snmpEngine = engine.SnmpEngine()
        else:
            self.snmpEngine = snmpEngine
        self.mibViewController = view.MibViewController(
            self.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
            )
        self.__knownAuths = {}
        self.__knownTransports = {}
        self.__knownTransportAddrs = {}

    def __del__(self): self.uncfgCmdGen()

    def cfgCmdGen(self, authData, transportTarget, tagList=''):
        paramsName = 'p-%s' % hash(authData.securityName)
        if not self.__knownAuths.has_key(authData):
            if isinstance(authData, CommunityData):
                config.addV1System(
                    self.snmpEngine,
                    authData.securityName,
                    authData.communityName
                    )
                config.addTargetParams(
                    self.snmpEngine, paramsName,
                    authData.securityName, authData.securityLevel,
                    authData.mpModel
                    )
            elif isinstance(authData, UsmUserData):
                config.addV3User(
                    self.snmpEngine,
                    authData.securityName,
                    authData.authProtocol, authData.authKey,
                    authData.privProtocol, authData.privKey
                    )
                config.addTargetParams(
                    self.snmpEngine, paramsName,
                    authData.securityName, authData.securityLevel
                    )
            else:
                raise error.PySnmpError('Unsupported SNMP version')
            self.__knownAuths[authData] = 1

        if not self.__knownTransports.has_key(transportTarget.transportDomain):
            transport = transportTarget.openClientMode()
            config.addSocketTransport(
                self.snmpEngine,
                transportTarget.transportDomain,
                transport
                )
            self.__knownTransports[transportTarget.transportDomain] = transport

        addrName = 'a-%s' % hash((paramsName, transportTarget.transportAddr))
        if not self.__knownTransportAddrs.has_key(addrName):
            config.addTargetAddr(
                self.snmpEngine, addrName,
                transportTarget.transportDomain,
                transportTarget.transportAddr,
                paramsName,
                transportTarget.timeout * 100,
                transportTarget.retries,
                tagList
                )
            self.__knownTransportAddrs[addrName] = 1

        return addrName, paramsName

    def uncfgCmdGen(self):
        for authData in self.__knownAuths.keys():
            paramsName = 'p-%s' % hash(authData.securityName)
            if isinstance(authData, CommunityData):
                config.delV1System(
                    self.snmpEngine,
                    authData.securityName
                    )
                config.delTargetParams(
                    self.snmpEngine, paramsName
                    )
            elif isinstance(authData, UsmUserData):
                config.delV3User(
                    self.snmpEngine, authData.securityName
                    )
                config.delTargetParams(
                    self.snmpEngine, paramsName
                    )
            else:
                raise error.PySnmpError('Unsupported SNMP version')
        self.__knownAuths.clear()

        for transportDomain, transport in self.__knownTransports.items():
            config.delSocketTransport(
                self.snmpEngine, transportDomain
                )
            transport.closeTransport()
        self.__knownTransports.clear()

        for addrName in self.__knownTransportAddrs.keys():
            config.delTargetAddr(
                self.snmpEngine, addrName
                )
        self.__knownTransportAddrs.clear()

    # Async SNMP apps

    def asyncGetCmd(
        self, authData, transportTarget, varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.GetCommandGenerator().sendReq(
            self.snmpEngine, addrName, varBinds, cbFun, cbCtx
            )

    def asyncSetCmd(
        self, authData, transportTarget, varBinds, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        __varBinds = []
        for varName, varVal in varBinds:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            if not type(varVal) == types.InstanceType:
                ((symName, modName), suffix) = mibvar.oidToMibName(
                    self.mibViewController, name + oid
                    )
                syntax = mibvar.cloneFromMibValue(
                    self.mibViewController, modName, symName, varVal
                    )
                if syntax is None:
                    raise error.PySnmpError(
                        'Value type MIB lookup failed for %s' % repr(varName)
                        )
                varVal = syntax.clone(varVal)
            __varBinds.append((name + oid, varVal))
        return cmdgen.SetCommandGenerator().sendReq(
            self.snmpEngine, addrName, __varBinds, cbFun, cbCtx
            )

    def asyncNextCmd(
        self, authData, transportTarget, varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.NextCommandGenerator().sendReq(
            self.snmpEngine, addrName, varBinds, cbFun, cbCtx
            )

    def asyncBulkCmd(
        self, authData, transportTarget, nonRepeaters, maxRepetitions,
        varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.BulkCommandGenerator().sendReq(
            self.snmpEngine, addrName, nonRepeaters, maxRepetitions,
            varBinds, cbFun, cbCtx
            )

class CommandGenerator(AsynCommandGenerator):
    def getCmd(self, authData, transportTarget, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBinds, appReturn
            ):
            appReturn['errorIndication'] = errorIndication
            appReturn['errorStatus'] = errorStatus
            appReturn['errorIndex'] = errorIndex
            appReturn['varBinds'] = varBinds

        appReturn = {}
        self.asyncGetCmd(
            authData, transportTarget, varNames, (__cbFun, appReturn)
            )
        self.snmpEngine.transportDispatcher.runDispatcher()
        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBinds']
            )

    def setCmd(self, authData, transportTarget, *varBinds):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBinds, appReturn
            ):
            appReturn['errorIndication'] = errorIndication
            appReturn['errorStatus'] = errorStatus
            appReturn['errorIndex'] = errorIndex
            appReturn['varBinds'] = varBinds

        appReturn = {}
        self.asyncSetCmd(
            authData, transportTarget, varBinds, (__cbFun, appReturn)
            )
        self.snmpEngine.transportDispatcher.runDispatcher()
        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBinds']
            )

    def nextCmd(self, authData, transportTarget, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBindTable, (varBindHead, varBindTotalTable, appReturn)
            ):
            if errorIndication or errorStatus:
                appReturn['errorIndication'] = errorIndication
                appReturn['errorStatus'] = errorStatus
                appReturn['errorIndex'] = errorIndex
                appReturn['varBindTable'] = varBindTable
                return
            else:
                varBindTableRow = varBindTable[-1]
                for idx in range(len(varBindTableRow)):
                    name, val = varBindTableRow[idx]
                    # XXX extra rows
                    if val is not None and varBindHead[idx].isPrefixOf(name):
                        break
                else:
                    appReturn['errorIndication'] = errorIndication
                    appReturn['errorStatus'] = errorStatus
                    appReturn['errorIndex'] = errorIndex
                    appReturn['varBindTable'] = varBindTotalTable
                    return
                varBindTotalTable.extend(varBindTable)

            return 1 # continue table retrieval

        varBindHead = map(lambda (x,y): univ.ObjectIdentifier(x+y), map(lambda x,self=self: mibvar.mibNameToOid(self.mibViewController, x), varNames))

        appReturn = {}
        self.asyncNextCmd(
            authData, transportTarget, varNames,
            (__cbFun, (varBindHead,[],appReturn))
            )

        self.snmpEngine.transportDispatcher.runDispatcher()

        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBindTable']
            )

    def bulkCmd(self, authData, transportTarget,
                nonRepeaters, maxRepetitions, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBindTable, (varBindHead, varBindTotalTable, appReturn)
            ):
            if errorIndication or errorStatus:
                appReturn['errorIndication'] = errorIndication
                appReturn['errorStatus'] = errorStatus
                appReturn['errorIndex'] = errorIndex
                appReturn['varBindTable'] = varBindTable
                return
            else:
                varBindTotalTable.extend(varBindTable) # XXX out of table
                                                       # rows possible
                varBindTableRow = varBindTable[-1]
                for idx in range(len(varBindTableRow)):
                    name, val = varBindTableRow[idx]
                    if val is not None and varBindHead[idx].isPrefixOf(name):
                        break
                else:
                    appReturn['errorIndication'] = errorIndication
                    appReturn['errorStatus'] = errorStatus
                    appReturn['errorIndex'] = errorIndex
                    appReturn['varBindTable'] = varBindTotalTable
                    return

            return 1 # continue table retrieval

        varBindHead = map(lambda (x,y): univ.ObjectIdentifier(x+y), map(lambda x,self=self: mibvar.mibNameToOid(self.mibViewController, x), varNames))

        appReturn = {}

        self.asyncBulkCmd(
            authData, transportTarget, nonRepeaters, maxRepetitions,
            varNames, (__cbFun, (varBindHead, [], appReturn))
            )

        self.snmpEngine.transportDispatcher.runDispatcher()

        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBindTable']
            )
<commit_msg>LCD names generation code reworked to avoid accidental clashes.<commit_after>import socket, string, types
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdgen, mibvar
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.smi import view
from pysnmp import nextid, error
from pyasn1.type import univ

# Auth protocol
usmHMACMD5AuthProtocol = config.usmHMACMD5AuthProtocol
usmHMACSHAAuthProtocol = config.usmHMACSHAAuthProtocol
usmNoAuthProtocol = config.usmNoAuthProtocol

# Privacy protocol
usmDESPrivProtocol = config.usmDESPrivProtocol
usmAesCfb128Protocol = config.usmAesCfb128Protocol
usmNoPrivProtocol = config.usmNoPrivProtocol

nextID = nextid.Integer(0xffffffff)

class CommunityData:
    mpModel=1 # Default is SMIv2
    securityModel=mpModel+1
    securityLevel='noAuthNoPriv'
    def __init__(self, securityName, communityName, mpModel=None):
        self.securityName = securityName
        self.communityName = communityName
        if mpModel is not None:
            self.mpModel = mpModel
            self.securityModel = mpModel + 1
        self.__cmp = self.mpModel, self.securityModel, self.securityLevel, self.securityName, self.communityName
        self.__hash = hash(self.__cmp)

    def __repr__(self): return '%s("%s", <COMMUNITY>, %s)' % (
        self.__class__.__name__,
        self.securityName,
        self.mpModel
        )

    def __hash__(self): return self.__hash
    def __cmp__(self, other): return cmp(self.__cmp, other)

class UsmUserData:
    authKey = privKey = None
    authProtocol = usmNoAuthProtocol
    privProtocol = usmNoPrivProtocol
    securityLevel='noAuthNoPriv'
    securityModel=3
    mpModel=2
    def __init__(self, securityName,
                 authKey=None, privKey=None,
                 authProtocol=None, privProtocol=None):
        self.securityName = securityName

        if authKey is not None:
            self.authKey = authKey
            if authProtocol is None:
                self.authProtocol = usmHMACMD5AuthProtocol
            else:
                self.authProtocol = authProtocol
            if self.securityLevel != 'authPriv':
                self.securityLevel = 'authNoPriv'

        if privKey is not None:
            self.privKey = privKey
            if self.authProtocol == usmNoAuthProtocol:
                raise error.PySnmpError('Privacy implies authenticity')
            self.securityLevel = 'authPriv'
            if privProtocol is None:
                self.privProtocol = usmDESPrivProtocol
            else:
                self.privProtocol = privProtocol

        self.__cmp = self.mpModel, self.securityModel, self.securityLevel, self.securityName, self.authProtocol, self.authKey, self.privProtocol, self.privKey
        self.__hash = hash(self.__cmp)

    def __repr__(self): return '%s("%s", <AUTHKEY>, <PRIVKEY>, %s, %s)' % (
        self.__class__.__name__,
        self.securityName,
        self.authProtocol,
        self.privProtocol
        )

    def __hash__(self): return self.__hash
    def __cmp__(self, other): return cmp(self.__cmp, other)

class UdpTransportTarget:
    transportDomain = udp.domainName
    def __init__(self, transportAddr, timeout=1, retries=5):
        self.transportAddr = (
            socket.gethostbyname(transportAddr[0]), transportAddr[1]
            )
        self.timeout = timeout
        self.retries = retries

    def __repr__(self): return '%s(("%s", %s), %s, %s)' % (
        self.__class__.__name__,
        self.transportAddr[0], self.transportAddr[1],
        self.timeout, self.retries
        )

    def __hash__(self): return hash(self.transportAddr)
    def __cmp__(self, other): return cmp(self.transportAddr, other)

    def openClientMode(self):
        self.transport = udp.UdpSocketTransport().openClientMode()
        return self.transport

class AsynCommandGenerator:
    _null = univ.Null('')
    def __init__(self, snmpEngine=None):
        if snmpEngine is None:
            self.snmpEngine = engine.SnmpEngine()
        else:
            self.snmpEngine = snmpEngine
        self.mibViewController = view.MibViewController(
            self.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
            )
        self.__knownAuths = {}
        self.__knownTransports = {}
        self.__knownTransportAddrs = {}

    def __del__(self): self.uncfgCmdGen()

    def cfgCmdGen(self, authData, transportTarget, tagList=''):
        if self.__knownAuths.has_key(authData):
            paramsName = self.__knownAuths[authData]
        else:
            paramsName = 'p%s' % nextID()
            if isinstance(authData, CommunityData):
                config.addV1System(
                    self.snmpEngine,
                    authData.securityName,
                    authData.communityName
                    )
                config.addTargetParams(
                    self.snmpEngine, paramsName,
                    authData.securityName, authData.securityLevel,
                    authData.mpModel
                    )
            elif isinstance(authData, UsmUserData):
                config.addV3User(
                    self.snmpEngine,
                    authData.securityName,
                    authData.authProtocol, authData.authKey,
                    authData.privProtocol, authData.privKey
                    )
                config.addTargetParams(
                    self.snmpEngine, paramsName,
                    authData.securityName, authData.securityLevel
                    )
            else:
                raise error.PySnmpError('Unsupported SNMP version')
            self.__knownAuths[authData] = paramsName

        if not self.__knownTransports.has_key(transportTarget.transportDomain):
            transport = transportTarget.openClientMode()
            config.addSocketTransport(
                self.snmpEngine,
                transportTarget.transportDomain,
                transport
                )
            self.__knownTransports[transportTarget.transportDomain] = transport

        k = transportTarget, tagList
        if self.__knownTransportAddrs.has_key(k):
            addrName = self.__knownTransportAddrs[k]
        else:
            addrName = 'a%s' % nextID()
            config.addTargetAddr(
                self.snmpEngine, addrName,
                transportTarget.transportDomain,
                transportTarget.transportAddr,
                paramsName,
                transportTarget.timeout * 100,
                transportTarget.retries,
                tagList
                )
            self.__knownTransportAddrs[k] = addrName

        return addrName, paramsName

    def uncfgCmdGen(self):
        for authData, paramsName in self.__knownAuths.items():
            if isinstance(authData, CommunityData):
                config.delV1System(
                    self.snmpEngine,
                    authData.securityName
                    )
                config.delTargetParams(
                    self.snmpEngine, paramsName
                    )
            elif isinstance(authData, UsmUserData):
                config.delV3User(
                    self.snmpEngine, authData.securityName
                    )
                config.delTargetParams(
                    self.snmpEngine, paramsName
                    )
            else:
                raise error.PySnmpError('Unsupported SNMP version')
        self.__knownAuths.clear()

        for transportDomain, transport in self.__knownTransports.items():
            config.delSocketTransport(
                self.snmpEngine, transportDomain
                )
            transport.closeTransport()
        self.__knownTransports.clear()

        for addrName in self.__knownTransportAddrs.values():
            config.delTargetAddr(
                self.snmpEngine, addrName
                )
        self.__knownTransportAddrs.clear()

    # Async SNMP apps

    def asyncGetCmd(
        self, authData, transportTarget, varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.GetCommandGenerator().sendReq(
            self.snmpEngine, addrName, varBinds, cbFun, cbCtx
            )

    def asyncSetCmd(
        self, authData, transportTarget, varBinds, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        __varBinds = []
        for varName, varVal in varBinds:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            if not type(varVal) == types.InstanceType:
                ((symName, modName), suffix) = mibvar.oidToMibName(
                    self.mibViewController, name + oid
                    )
                syntax = mibvar.cloneFromMibValue(
                    self.mibViewController, modName, symName, varVal
                    )
                if syntax is None:
                    raise error.PySnmpError(
                        'Value type MIB lookup failed for %s' % repr(varName)
                        )
                varVal = syntax.clone(varVal)
            __varBinds.append((name + oid, varVal))
        return cmdgen.SetCommandGenerator().sendReq(
            self.snmpEngine, addrName, __varBinds, cbFun, cbCtx
            )

    def asyncNextCmd(
        self, authData, transportTarget, varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.NextCommandGenerator().sendReq(
            self.snmpEngine, addrName, varBinds, cbFun, cbCtx
            )

    def asyncBulkCmd(
        self, authData, transportTarget, nonRepeaters, maxRepetitions,
        varNames, (cbFun, cbCtx)
        ):
        addrName, paramsName = self.cfgCmdGen(
            authData, transportTarget
            )
        varBinds = []
        for varName in varNames:
            name, oid = mibvar.mibNameToOid(
                self.mibViewController, varName
                )
            varBinds.append((name + oid, self._null))
        return cmdgen.BulkCommandGenerator().sendReq(
            self.snmpEngine, addrName, nonRepeaters, maxRepetitions,
            varBinds, cbFun, cbCtx
            )

class CommandGenerator(AsynCommandGenerator):
    def getCmd(self, authData, transportTarget, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBinds, appReturn
            ):
            appReturn['errorIndication'] = errorIndication
            appReturn['errorStatus'] = errorStatus
            appReturn['errorIndex'] = errorIndex
            appReturn['varBinds'] = varBinds

        appReturn = {}
        self.asyncGetCmd(
            authData, transportTarget, varNames, (__cbFun, appReturn)
            )
        self.snmpEngine.transportDispatcher.runDispatcher()
        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBinds']
            )

    def setCmd(self, authData, transportTarget, *varBinds):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBinds, appReturn
            ):
            appReturn['errorIndication'] = errorIndication
            appReturn['errorStatus'] = errorStatus
            appReturn['errorIndex'] = errorIndex
            appReturn['varBinds'] = varBinds

        appReturn = {}
        self.asyncSetCmd(
            authData, transportTarget, varBinds, (__cbFun, appReturn)
            )
        self.snmpEngine.transportDispatcher.runDispatcher()
        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBinds']
            )

    def nextCmd(self, authData, transportTarget, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBindTable, (varBindHead, varBindTotalTable, appReturn)
            ):
            if errorIndication or errorStatus:
                appReturn['errorIndication'] = errorIndication
                appReturn['errorStatus'] = errorStatus
                appReturn['errorIndex'] = errorIndex
                appReturn['varBindTable'] = varBindTable
                return
            else:
                varBindTableRow = varBindTable[-1]
                for idx in range(len(varBindTableRow)):
                    name, val = varBindTableRow[idx]
                    # XXX extra rows
                    if val is not None and varBindHead[idx].isPrefixOf(name):
                        break
                else:
                    appReturn['errorIndication'] = errorIndication
                    appReturn['errorStatus'] = errorStatus
                    appReturn['errorIndex'] = errorIndex
                    appReturn['varBindTable'] = varBindTotalTable
                    return
                varBindTotalTable.extend(varBindTable)

            return 1 # continue table retrieval

        varBindHead = map(lambda (x,y): univ.ObjectIdentifier(x+y), map(lambda x,self=self: mibvar.mibNameToOid(self.mibViewController, x), varNames))

        appReturn = {}
        self.asyncNextCmd(
            authData, transportTarget, varNames,
            (__cbFun, (varBindHead,[],appReturn))
            )

        self.snmpEngine.transportDispatcher.runDispatcher()

        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBindTable']
            )

    def bulkCmd(self, authData, transportTarget,
                nonRepeaters, maxRepetitions, *varNames):
        def __cbFun(
            sendRequestHandle, errorIndication, errorStatus, errorIndex,
            varBindTable, (varBindHead, varBindTotalTable, appReturn)
            ):
            if errorIndication or errorStatus:
                appReturn['errorIndication'] = errorIndication
                appReturn['errorStatus'] = errorStatus
                appReturn['errorIndex'] = errorIndex
                appReturn['varBindTable'] = varBindTable
                return
            else:
                varBindTotalTable.extend(varBindTable) # XXX out of table
                                                       # rows possible
                varBindTableRow = varBindTable[-1]
                for idx in range(len(varBindTableRow)):
                    name, val = varBindTableRow[idx]
                    if val is not None and varBindHead[idx].isPrefixOf(name):
                        break
                else:
                    appReturn['errorIndication'] = errorIndication
                    appReturn['errorStatus'] = errorStatus
                    appReturn['errorIndex'] = errorIndex
                    appReturn['varBindTable'] = varBindTotalTable
                    return

            return 1 # continue table retrieval

        varBindHead = map(lambda (x,y): univ.ObjectIdentifier(x+y), map(lambda x,self=self: mibvar.mibNameToOid(self.mibViewController, x), varNames))

        appReturn = {}

        self.asyncBulkCmd(
            authData, transportTarget, nonRepeaters, maxRepetitions,
            varNames, (__cbFun, (varBindHead, [], appReturn))
            )

        self.snmpEngine.transportDispatcher.runDispatcher()

        return (
            appReturn['errorIndication'],
            appReturn['errorStatus'],
            appReturn['errorIndex'],
            appReturn['varBindTable']
            )
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Dissemin: open access policy enforcement tool
# Copyright (C) 2014 Antonin Delpeuch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
from __future__ import unicode_literals

import json

import requests

from deposit.protocol import DepositError
from deposit.protocol import DepositResult
from deposit.protocol import RepositoryProtocol
from deposit.registry import protocol_registry
from deposit.osf.forms import OSFForm
from django.utils.translation import ugettext as __
# from django.utils.translation import ugettext_lazy as __
from papers.utils import kill_html

# NO_LICENSE_ID = "563c1cf88c5e4a3877f9e965"
NO_LICENSE_ID = "58fd62fcda3e2400012ca5cc" # Test server

class OSFProtocol(RepositoryProtocol):
    """
    A protocol to submit using the OSF REST API
    """
    form_class = OSFForm

    def __init__(self, repository, **kwargs):
        super(OSFProtocol, self).__init__(repository, **kwargs)
        # we let the interface define another API endpoint (sandbox…)
        self.api_url = repository.endpoint
        if not self.api_url:
            # self.api_url = "https://api.osf.io/v2/nodes/"
            self.api_url = "https://test-api.osf.io/v2/nodes/" # test server

    def init_deposit(self, paper, user):
        """
        Refuse deposit when the paper is already on OSF.
        """
        super(OSFProtocol, self).init_deposit(paper, user)
        return (True)

    def get_form_initial_data(self):
        data = super(OSFProtocol, self).get_form_initial_data()

        if self.paper.abstract:
            data['abstract'] = kill_html(self.paper.abstract)

        return data

    # Get some basic data needed in different methods.
    def get_primary_data(self, form):
        paper = self.paper.json()
        # authors = paper['authors']
        # records = paper['records']
        # pub_date = paper['date'][:-6]

        abstract = (form.cleaned_data['abstract'] or
                    kill_html(self.paper.abstract))

        return (paper, abstract)

    paper, abstract = get_primary_data(form)
    authors = paper['authors']
    records = paper['records']
    pub_date = paper['date'][:-6]

    def create_tags(self, form):
        tags = list(form.cleaned_data['tags'].split(','))
        tags = [item.strip() for item in tags]
        tags = [item for item in tags if item != ""]

        return tags

    tags = create_tags()

    # Look for a specific subkey.
    def get_key_data(self, key):
        for item in records:
            if item.get(key):
                return (item[key])

        return None

    paper_doi = get_key_data('doi')

    # ---------------------------------------------
    # HERE GO THE DIFFERENT METHODS
    # NEEDED BY submit_deposit()
    # ---------------------------------------------
    # Get a dictionary containing the first and last names
    # of the authors of a Dissemin paper,
    # ready to be implemented in an OSF Preprints data dict.
    def translate_author(dissemin_authors, goal="optional"):
        author = "{} {}".format(dissemin_authors['name']['first'],
                                dissemin_authors['name']['last'])

        if goal == "contrib":
            structure = {
                "data": {
                    "type": "contributors",
                    "attributes": {
                        "full_name": author
                    }
                }
            }
            return structure

        else:
            return author

    # Extract the OSF Storage link.
    def translate_links(node_links):
        upload_link = node_links['links']['upload']
        return upload_link

    # Send the min. structure.
    # The response should contain the node ID.
    def create_node(self, abstract, tags, authors):
        abstract = abstract
        tags = tags
        authors = authors

        # Required to create a new node.
        # The project will then host the preprint.
        min_node_structure = {
            "data": {
                "type": "nodes",
                "attributes": {
                    "title": paper['title'],
                    "category": "project",
                    "description": abstract,
                    "tags": tags
                }
            }
        }

        # Creating the metadata
        self.log("### Creating the metadata")
        self.log(json.dumps(min_node_structure, indent=4)+'')
        self.log(json.dumps(authors, indent=4)+'')

        osf_response = requests.post(self.api_url,
                                     data=json.dumps(min_node_structure),
                                     headers=headers)
        self.log_request(osf_response, 201,
                         __('Unable to create a project on OSF.'))

        osf_response = osf_response.json()

        return osf_response

    osf_response = create_node(abstract, tags, authors)

    # Get OSF Storage link
    # to later upload the Preprint PDF file.
    def get_newnode_osf_storage(node_id):
        self.storage_url = self.api_url + "{}/files/".format(node_id)
        osf_storage_data = requests.get(self.storage_url,
                                        headers=headers)
        self.log_request(osf_storage_data, 200,
                         __('Unable to authenticate to OSF.'))

        osf_storage_data = osf_storage_data.json()
        return osf_storage_data

    # Add contributors
    def add_contributors():
        contrib_url = self.api_url + node_id + "/contributors/"

        for author in authors:
            contrib = translate_author(author, "contrib")
            contrib_response = requests.post(contrib_url,
                                             data=json.dumps(contrib),
                                             headers=headers)
            self.log_request(contrib_response, 201,
                             __('Unable to add contributors.'))

    def create_license():
        node_url = self.api_url + node_id + "/"
        # license_url = "https://api.osf.io/v2/licenses/"
        license_url = "https://test-api.osf.io/v2/licenses/" # Test server
        license_url = license_url + "{}".format(license_id) + "/"
        authors_list = [translate_author(author)
                        for author in authors]

        license_structure = {
                "data": {
                    "type": "nodes",
                    "id": node_id,
                    "attributes": {},
                    "relationships": {
                        "license": {
                            "data": {
                                "type": "licenses",
                                "id": license_id
                            }
                        }
                    }
                }
            }

        if license_id == NO_LICENSE_ID:
            license_structure['data']['attributes'] = {
                "node_license": {
                    "year": pub_date,
                    "copyright_holders": authors_list
                }
            }
        else:
            license_structure['data']['attributes'] = {
                "node_license": {}
            }

        license_req = requests.patch(node_url,
                                     data=json.dumps(license_structure),
                                     headers=headers)
        self.log_request(license_req, 200,
                         __('Unable to update license.'))

        # Updating License
        self.log("### Updating License")
        self.log(str(license_req.status_code))
        self.log(license_req.text)

    def create_preprint():
        # preprint_node_url = "https://api.osf.io/v2/preprints/"
        preprint_node_url = "https://test-api.osf.io/v2/preprints/" # Test server

        # -----------------------------------------------
        # The following structure will be used
        # to send a preprint on OSF once the project
        # has been created there.
        # -----------------------------------------------
        min_preprint_structure = {
            "data": {
                "attributes": {
                    "doi": paper_doi
                },
                "relationships": {
                    "node": {
                        "data": {
                            "type": "nodes",
                            "id": node_id
                        }
                    },
                    "primary_file": {
                        "data": {
                            "type": "primary_files",
                            "id": pf_path
                        }
                    },
                    "provider": {
                        "data": {
                            "type": "providers",
                            "id": "osf"
                        }
                    }
                }
            }
        }

        self.log("### Creating Preprint")
        osf_response = requests.post(preprint_node_url,
                                     data=json.dumps(min_preprint_structure),
                                     headers=headers)
        self.log_request(osf_response, 201,
                         __('Unable to create the preprint.'))

        self.log(str(osf_response.status_code))

        osf_preprint_response = osf_response.json()

        return (osf_preprint_response)

    def update_preprint_license():
        authors_list = [translate_author(author)
                        for author in authors]
        # preprint_node_url = self.api_url + "{}/preprints/".format(node_id)
        preprint_node_url = (
            "https://test-api.osf.io/v2/preprints/{}".format(preprint_id) + "/") # Test server

        updated_preprint_struc = {
            "data": {
                "type": "nodes",
                "id": preprint_id,
                "attributes": {},
                "relationships": {
                    "license": {
                        "data": {
                            "type": "licenses",
                            "id": license_id
                        }
                    }
                }
            }
        }

        if license_id == NO_LICENSE_ID:
            updated_preprint_struc['data']['attributes'] = {
                "license_record": {
                    "year": pub_date,
                    "copyright_holders": authors_list
                }
            }
        else:
            updated_preprint_struc['data']['attributes'] = {
                "license_record": {}
            }

        self.log("### Updating the Preprint License")
        license_req = requests.patch(preprint_node_url,
                                     data=json.dumps(updated_preprint_struc),
                                     headers=headers)
        self.log_request(license_req, 200,
                         __('Unable to update the Preprint License.'))

        self.log(str(license_req.status_code))
        self.log(license_req.text)

    # MAIN FUNCTION
    def submit_deposit(self, pdf, form, dry_run=False):
        if self.repository.api_key is None:
            raise DepositError(__("No OSF token provided."))

        api_key = self.repository.api_key
        license_id = form.cleaned_data['license']

        deposit_result = DepositResult()

        # Creating the metadata
        self.log("### Creating the metadata")
        min_node_structure, authors, paper_doi, pub_date = (
            self.createMetadata(form))
        self.log(json.dumps(min_node_structure, indent=4)+'')
        self.log(json.dumps(authors, indent=4)+'')

        # Creating a new depository
        self.log("### Creating a new depository")
        headers = {
            'Authorization': 'Bearer %s' % api_key,
            'Content-Type': 'application/vnd.api+json'
        }

        osf_response = self.create_node()
        node_id = osf_response['data']['id']

        self.osf_storage_data = get_newnode_osf_storage(node_id)
        osf_links = self.osf_storage_data['data']
        osf_upload_link = str(
            list({translate_links(entry) for entry in osf_links}))
        osf_upload_link = osf_upload_link.replace("[u'", '').replace("']", '')

        # Uploading the PDF
        self.log("### Uploading the PDF")
        upload_url_suffix = "?kind=file&name=article.pdf"
        upload_url = osf_upload_link + upload_url_suffix
        data = open(pdf, 'r')
        primary_file_data = requests.put(upload_url,
                                         data=data,
                                         headers=headers)
        self.log_request(primary_file_data, 201,
                         __('Unable to upload the PDF file.'))
        primary_file_data = primary_file_data.json()
        # Uncomment pf_path when time to test the preprint upload has come
        pf_path = primary_file_data['data']['attributes']['path'][1:]

        add_contributors()

        create_license()

        # Create Preprint
        osf_preprint_response = create_preprint()
        preprint_id = osf_preprint_response['data']['id']

        update_preprint_license()

        return (deposit_result)

protocol_registry.register(OSFProtocol)
<commit_msg>Work on removing the functions from submit_deposit()-- 07<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Dissemin: open access policy enforcement tool
# Copyright (C) 2014 Antonin Delpeuch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
from __future__ import unicode_literals

import json

import requests

from deposit.protocol import DepositError
from deposit.protocol import DepositResult
from deposit.protocol import RepositoryProtocol
from deposit.registry import protocol_registry
from deposit.osf.forms import OSFForm
from django.utils.translation import ugettext as __
# from django.utils.translation import ugettext_lazy as __
from papers.utils import kill_html

# NO_LICENSE_ID = "563c1cf88c5e4a3877f9e965"
NO_LICENSE_ID = "58fd62fcda3e2400012ca5cc" # Test server

class OSFProtocol(RepositoryProtocol):
    """
    A protocol to submit using the OSF REST API
    """
    form_class = OSFForm

    def __init__(self, repository, **kwargs):
        super(OSFProtocol, self).__init__(repository, **kwargs)
        # we let the interface define another API endpoint (sandbox…)
        self.api_url = repository.endpoint
        if not self.api_url:
            # self.api_url = "https://api.osf.io/v2/nodes/"
            self.api_url = "https://test-api.osf.io/v2/nodes/" # test server

    def init_deposit(self, paper, user):
        """
        Refuse deposit when the paper is already on OSF.
        """
        super(OSFProtocol, self).init_deposit(paper, user)
        return (True)

    def get_form_initial_data(self):
        data = super(OSFProtocol, self).get_form_initial_data()

        if self.paper.abstract:
            data['abstract'] = kill_html(self.paper.abstract)

        return data

    # Get some basic data needed in different methods.
    def get_primary_data(self, form):
        paper = self.paper.json()
        # authors = paper['authors']
        # records = paper['records']
        # pub_date = paper['date'][:-6]

        abstract = (form.cleaned_data['abstract'] or
                    kill_html(self.paper.abstract))

        return (paper, abstract)

    paper, abstract = self.get_primary_data(form)
    authors = paper['authors']
    records = paper['records']
    pub_date = paper['date'][:-6]

    def create_tags(self, form):
        tags = list(form.cleaned_data['tags'].split(','))
        tags = [item.strip() for item in tags]
        tags = [item for item in tags if item != ""]

        return tags

    tags = create_tags()

    # Look for a specific subkey.
    def get_key_data(self, key):
        for item in records:
            if item.get(key):
                return (item[key])

        return None

    self.paper_doi = get_key_data('doi')

    # ---------------------------------------------
    # HERE GO THE DIFFERENT METHODS
    # NEEDED BY submit_deposit()
    # ---------------------------------------------
    # Get a dictionary containing the first and last names
    # of the authors of a Dissemin paper,
    # ready to be implemented in an OSF Preprints data dict.
    def translate_author(dissemin_authors, goal="optional"):
        author = "{} {}".format(dissemin_authors['name']['first'],
                                dissemin_authors['name']['last'])

        if goal == "contrib":
            structure = {
                "data": {
                    "type": "contributors",
                    "attributes": {
                        "full_name": author
                    }
                }
            }
            return structure

        else:
            return author

    # Extract the OSF Storage link.
    def translate_links(node_links):
        upload_link = node_links['links']['upload']
        return upload_link

    # Send the min. structure.
    # The response should contain the node ID.
    def create_node(self, abstract, tags, authors):
        abstract = abstract
        tags = tags
        authors = authors

        # Required to create a new node.
        # The project will then host the preprint.
        min_node_structure = {
            "data": {
                "type": "nodes",
                "attributes": {
                    "title": paper['title'],
                    "category": "project",
                    "description": abstract,
                    "tags": tags
                }
            }
        }

        # Creating the metadata
        self.log("### Creating the metadata")
        self.log(json.dumps(min_node_structure, indent=4)+'')
        self.log(json.dumps(authors, indent=4)+'')

        osf_response = requests.post(self.api_url,
                                     data=json.dumps(min_node_structure),
                                     headers=headers)
        self.log_request(osf_response, 201,
                         __('Unable to create a project on OSF.'))

        osf_response = osf_response.json()

        return osf_response

    osf_response = self.create_node(abstract, tags, authors)

    # Get OSF Storage link
    # to later upload the Preprint PDF file.
    def get_newnode_osf_storage(self, node_id):
        self.storage_url = self.api_url + "{}/files/".format(node_id)
        osf_storage_data = requests.get(self.storage_url,
                                        headers=headers)
        self.log_request(osf_storage_data, 200,
                         __('Unable to authenticate to OSF.'))

        osf_storage_data = osf_storage_data.json()
        return osf_storage_data

    # Add contributors
    def add_contributors(self):
        contrib_url = self.api_url + node_id + "/contributors/"

        for author in authors:
            contrib = self.translate_author(author, "contrib")
            contrib_response = requests.post(contrib_url,
                                             data=json.dumps(contrib),
                                             headers=headers)
            self.log_request(contrib_response, 201,
                             __('Unable to add contributors.'))

    def create_license(self):
        node_url = self.api_url + node_id + "/"
        # license_url = "https://api.osf.io/v2/licenses/"
        license_url = "https://test-api.osf.io/v2/licenses/" # Test server
        license_url = license_url + "{}".format(license_id) + "/"
        authors_list = [self.translate_author(author)
                        for author in authors]

        license_structure = {
                "data": {
                    "type": "nodes",
                    "id": node_id,
                    "attributes": {},
                    "relationships": {
                        "license": {
                            "data": {
                                "type": "licenses",
                                "id": license_id
                            }
                        }
                    }
                }
            }

        if license_id == NO_LICENSE_ID:
            license_structure['data']['attributes'] = {
                "node_license": {
                    "year": pub_date,
                    "copyright_holders": authors_list
                }
            }
        else:
            license_structure['data']['attributes'] = {
                "node_license": {}
            }

        license_req = requests.patch(node_url,
                                     data=json.dumps(license_structure),
                                     headers=headers)
        self.log_request(license_req, 200,
                         __('Unable to update license.'))

        # Updating License
        self.log("### Updating License")
        self.log(str(license_req.status_code))
        self.log(license_req.text)

    def create_preprint(self):
        # preprint_node_url = "https://api.osf.io/v2/preprints/"
        preprint_node_url = "https://test-api.osf.io/v2/preprints/" # Test server

        # -----------------------------------------------
        # The following structure will be used
        # to send a preprint on OSF once the project
        # has been created there.
        # -----------------------------------------------
        min_preprint_structure = {
            "data": {
                "attributes": {
                    "doi": paper_doi
                },
                "relationships": {
                    "node": {
                        "data": {
                            "type": "nodes",
                            "id": node_id
                        }
                    },
                    "primary_file": {
                        "data": {
                            "type": "primary_files",
                            "id": pf_path
                        }
                    },
                    "provider": {
                        "data": {
                            "type": "providers",
                            "id": "osf"
                        }
                    }
                }
            }
        }

        self.log("### Creating Preprint")
        osf_response = requests.post(preprint_node_url,
                                     data=json.dumps(min_preprint_structure),
                                     headers=headers)
        self.log_request(osf_response, 201,
                         __('Unable to create the preprint.'))

        self.log(str(osf_response.status_code))

        osf_preprint_response = osf_response.json()

        return osf_preprint_response

    def update_preprint_license(self):
        authors_list = [self.translate_author(author)
                        for author in authors]
        # preprint_node_url = self.api_url + "{}/preprints/".format(node_id)
        preprint_node_url = (
            "https://test-api.osf.io/v2/preprints/{}".format(preprint_id) + "/") # Test server

        updated_preprint_struc = {
            "data": {
                "type": "nodes",
                "id": preprint_id,
                "attributes": {},
                "relationships": {
                    "license": {
                        "data": {
                            "type": "licenses",
                            "id": license_id
                        }
                    }
                }
            }
        }

        if license_id == NO_LICENSE_ID:
            updated_preprint_struc['data']['attributes'] = {
                "license_record": {
                    "year": pub_date,
                    "copyright_holders": authors_list
                }
            }
        else:
            updated_preprint_struc['data']['attributes'] = {
                "license_record": {}
            }

        self.log("### Updating the Preprint License")
        license_req = requests.patch(preprint_node_url,
                                     data=json.dumps(updated_preprint_struc),
                                     headers=headers)
        self.log_request(license_req, 200,
                         __('Unable to update the Preprint License.'))

        self.log(str(license_req.status_code))
        self.log(license_req.text)

    # MAIN FUNCTION
    def submit_deposit(self, pdf, form, dry_run=False):
        if self.repository.api_key is None:
            raise DepositError(__("No OSF token provided."))

        api_key = self.repository.api_key
        license_id = form.cleaned_data['license']

        deposit_result = DepositResult()

        # Creating the metadata
        self.log("### Creating the metadata")
        min_node_structure, authors, paper_doi, pub_date = (
            self.createMetadata(form))
        self.log(json.dumps(min_node_structure, indent=4)+'')
        self.log(json.dumps(authors, indent=4)+'')

        # Creating a new depository
        self.log("### Creating a new depository")
        headers = {
            'Authorization': 'Bearer %s' % api_key,
            'Content-Type': 'application/vnd.api+json'
        }

        osf_response = self.create_node()
        node_id = osf_response['data']['id']

        self.osf_storage_data = get_newnode_osf_storage(node_id)
        osf_links = self.osf_storage_data['data']
        osf_upload_link = str(
            list({translate_links(entry) for entry in osf_links}))
        osf_upload_link = osf_upload_link.replace("[u'", '').replace("']", '')

        # Uploading the PDF
        self.log("### Uploading the PDF")
        upload_url_suffix = "?kind=file&name=article.pdf"
        upload_url = osf_upload_link + upload_url_suffix
        data = open(pdf, 'r')
        primary_file_data = requests.put(upload_url,
                                         data=data,
                                         headers=headers)
        self.log_request(primary_file_data, 201,
                         __('Unable to upload the PDF file.'))
        primary_file_data = primary_file_data.json()
        # Uncomment pf_path when time to test the preprint upload has come
        pf_path = primary_file_data['data']['attributes']['path'][1:]

        add_contributors()

        create_license()

        # Create Preprint
        osf_preprint_response = create_preprint()
        preprint_id = osf_preprint_response['data']['id']

        update_preprint_license()

        return (deposit_result)

protocol_registry.register(OSFProtocol)
<|endoftext|>


<commit_before>"""Helper tools for control system design.

"""

from scipy.linalg import solve, inv, eig, eigvals
from numpy import dot, zeros

def dare(A, B, M, Q, R):
    """Solves the discrete-time algebraic Riccati equation

    Parameters
    ==========

    A : System dynamics matrix (n x n)
    B : Input matrix           (n x m)
    Q : State weighting matrix (n x n), symmetric, positive definite
    R : Input weighting matrix (m x m), symmetric, positive definite

    Returns
    =======

    P_c : solution to Riccati equation
      L : closed loop eigenvalues
      F : optimal state feedback gain u(k) = F * x(k), note sign!

    Notes
    =====

    For the discrete time optimal control problem, we have the system

      x(k + 1) = A * x(k) + B * u(k)
          z(k) = M * x(k)

    The Riccati equation is

      A ^ T * (P_c - P_c * B * (R + B ^ T * P_c * B) ^ (-1) * B ^ T * P_c) * A - P_c + M ^ T * Q * M = 0

    The solution to this equation, P_c, yields the linear state feedback law

      u(k) = F * x(k) = -(R + B ^ T * P_c * B) ^ (-1) B ^ T * P_c * A * x(k)

    which minimizes the cost function

      J(u) = \sum_{k = 0}^{\infty} (z ^ T (k) * Q * z(k) + u ^ T (k) * R * u(k))

    Note that the discrete time optimal control problem is dual to the discrete
    time optimal state estimation problem:

      x(k + 1) = A * x(k) + B * u(k) + G * w(k)
          y(k) = C * x(k) + v(k)

    where w and k are white, zero-mean Gaussian processes with covariances:

      E[w * w ^ T] = W

      E[v * v ^ T] = V

    With W = W ^ T > 0, V = V ^ T > 0.

    The optimal current estimator is

      \bar{x} (k) = \hat{x}(k) + K_c * (y(k) - C * \hat{x}(k))

    where

      \hat{x} = A * \bar{x}(k - 1) + B * u(k - 1)

    where \hat{x} denotes the prior estimate of the state at the time of a
    measurement.  The state error covariance is minimized when the filter gain
    is

      K_c = P_e * C ^ T * (C * P_e * C ^ T + V) ^ (-1)

    where P_e is the solution to the dual of the above Riccati equation
    obtained by substituting:

        A => A ^ T
        B => C ^ T
        M => G ^ T
        R => V
        Q => W

    """

    An, Am = A.shape
    Bn, Bm = B.shape
    Qn, Qm = Q.shape
    Rn, Rm = R.shape
    assert(An == Am == Bn == Qn == Qm)
    assert(Bm == Rn == Rm)

    n = An
    m = Bm

    # Allocate the Hamiltonian matrix
    H = zeros((2*n, 2*n))

    # Solve for B * R ^ -1
    B_R_inv = solve(R.T, B.T).T

    # Solve for  A ^ -T * M ^ T * Q * M
    A_nT_M_T_Q_M = solve(A.T, dot(M.T, dot(Q, M)))

    # Solve for B ^ T * A ^ -T
    B_T_A_nT = solve(A, B).T

    H[:n, :n] = A + dot(dot(B_R_inv, B.T), A_nT_M_T_Q_M)
    H[:n, n:] = -dot(B_R_inv, B_T_A_nT)
    H[n:, :n] = -A_nT_M_T_Q_M
    H[n:, n:] = inv(A).T

    evals, evecs = eig(H)
    stable_indices = []
    num_stable = 0
    for j in range(2*n):
        if abs(evals[j]) < 1:       # inside unit circle
            stable_indices.append(j)
            num_stable += 1
    assert(num_stable == n)         # Verify construction of Hamiltonian

    P_c = solve(evecs[:n, stable_indices].T, evecs[n:, stable_indices].T)
    F = -solve(R + dot(B.T, dot(P_c, B)), dot(B.T, dot(P_c, A))).real

    return P_c, eigvals(A + dot(B, F)), F

<commit_msg>Reimplemented DARE.<commit_after>"""Helper tools for control system design.

"""

from scipy.linalg import inv, schur, solve
from numpy.matlib import empty

def dare(F, G1, G2, H):
    """Solves the discrete-time algebraic Riccati equation

    0 = F ^ T * X * F
        - X - F ^ T * X * G1 * (G2 + G1 ^ T * X * G1) ^ -1 * G1 ^ T * X * F + H

    Under the assumption that X ^ -1 exists, this equation is equivalent to

    0 = F ^ T * (X ^ -1 + G1 * G2 ^ -1 * G1 ^ T) ^ -1 * F - X + H

    Parameters
    ==========
    Inputs are real matrices:

    F : n x n
    G1 : n x m
    G2 : m x m, symmetric, positive definite
    H : n x n, symmetric, positive semi-definite

    Assumptions
    ===========
    (F, G1) is a stabilizable pair
    (C, F) is a detectable pair (where C is full rank factorization of H, i.e.,
        C ^ T * C = H and rank(C) = rank(H).
    F is invertible

    Returns
    =======

    Unique nonnegative definite solution of discrete Algrebraic Ricatti
    equation.

    Notes
    =====
    This is an implementation of the Schur method for solving algebraic Riccati
    eqautions as described in dx.doi.org/10.1109/TAC.1979.1102178

    """
    n = F.shape[0]
    m = G2.shape[0]

    G = G1*inv(G2)*G1.T
    Finv = inv(F)
    Finvt = Finv.T

    # Form symplectic matrix
    Z = empty((2*n, 2*n))
    Z[:n, :n] = F + G*Finvt*H
    Z[:n, n:] = -G*Finvt
    Z[n:, :n] = -Finvt*H
    Z[n:, n:] = Finvt

    S, U, sdim = schur(Z, sort='iuc')

    # Verify that the n eigenvalues of the upper left block stable
    assert(sdim == n)

    U11 = U[:n, :n]
    U21 = U[n:, :n]
    return solve(U[:n, :n].T, U[n:, :n].T).T
<|endoftext|>


<commit_before>from pprint import pprint

from django.conf import settings
from django.contrib import admin

from fo2.admin import intr_adm_site

import logistica.models as models

class NotaFiscalAdmin(admin.ModelAdmin):
    list_per_page = 50
    list_display = ['numero', 'faturamento', 'posicao',
                    'saida', 'entrega', 'confirmada',
                    'natu_venda', 'ativa', 'nf_devolucao',
                    'uf', 'transp_nome',
                    'dest_nome', 'dest_cnpj',
                    'valor', 'volumes',
                    'cod_status',
                    'observacao']
    list_editable = ['saida', 'entrega', 'confirmada']
    # list_filter = ['ativa', 'natu_venda', 'saida',
    #                'entrega', 'confirmada',
    #                'faturamento', 'transp_nome', 'cod_status', 'uf']
    search_fields = ['numero', 'transp_nome', 'dest_nome']
    ordering = ['-numero']
    fields = (('numero', 'ativa', 'nf_devolucao', 'posicao'),
              ('dest_cnpj', 'dest_nome', 'uf', 'transp_nome'),
              ('pedido', 'ped_cliente'),
              ('natu_venda', 'natu_descr'),
              ('volumes', 'valor'),
              ('faturamento', 'cod_status', 'msg_status'),
              'saida', 'entrega', 'confirmada', 'observacao', 'id')
    readonly_fields = ['id', 'numero', 'faturamento', 'posicao',
                       'pedido', 'ped_cliente',
                       'volumes', 'valor',
                       'cod_status', 'msg_status', 'ativa', 'nf_devolucao',
                       'dest_cnpj', 'dest_nome', 'uf',
                       'transp_nome', 'natu_descr', 'natu_venda',
                       ]

intr_adm_site.register(models.NotaFiscal, NotaFiscalAdmin)

class PosicaoCargaAdmin(admin.ModelAdmin):
    list_display = ['nome']

intr_adm_site.register(models.PosicaoCarga, PosicaoCargaAdmin)

class RotinaLogisticaAdmin(admin.ModelAdmin):
    list_display = ['nome', 'slug']
    readonly_fields = ['slug']

intr_adm_site.register(models.RotinaLogistica, RotinaLogisticaAdmin)

class PosicaoCargaAlteracaoAdmin(admin.ModelAdmin):
    list_display = [
        'inicial', 'ordem', 'descricao', 'final', 'efeito', 'so_nfs_ativas']
    ordering = ['inicial', 'ordem']

intr_adm_site.register(
    models.PosicaoCargaAlteracao, PosicaoCargaAlteracaoAdmin)

class NfEntradaAdmin(admin.ModelAdmin):

    def __init__(self, *args):
        super().__init__(*args)
        self._list_display = [
            '__str__', 'emissor', 'descricao', 'qtd',
            'hora_entrada', 'transportadora', 'motorista', 'placa',
            'responsavel', 'usuario', 'quando'
        ]
        self._fields = [
            'cadastro', 'numero', 'emissor', 'descricao', 'qtd',
            'hora_entrada', 'transportadora', 'motorista', 'placa',
            'responsavel', 'usuario', 'quando'
        ]

        self.list_per_page = 50
        self.list_display = self._list_display.copy()
        self.list_display.insert(0, 'empresa')
        self.search_fields = ['emissor', 'numero', 'descricao']
        self.ordering = ['-quando']
        self.fields = self._fields.copy()
        self.fields.insert(0, 'empresa')
        self.readonly_fields = ['usuario', 'quando']

    class Media:
        static_url = getattr(settings, 'STATIC_URL', '/static/')
        js = [static_url+'/admin/logistica/nfEntrada.js', ]

class NfEntradaAgatorAdmin(NfEntradaAdmin):

    def __init__(self, *args):
        super().__init__(*args)

        self.list_display = self._list_display
        self.fields = self._fields

class NfEntradaTussorAdmin(NfEntradaAdmin):

    def __init__(self, *args):
        super().__init__(*args)

        self.list_display = self._list_display
        self.fields = self._fields

intr_adm_site.register(
    models.NfEntrada, NfEntradaAdmin)
intr_adm_site.register(
    models.NfEntradaAgator, NfEntradaAgatorAdmin)
intr_adm_site.register(
    models.NfEntradaTussor, NfEntradaTussorAdmin)
<commit_msg>NfEntradaAdmin utilizando EntradaNfForm<commit_after>from pprint import pprint

from django.conf import settings
from django.contrib import admin

from fo2.admin import intr_adm_site

import logistica.models as models
from logistica.forms import EntradaNfForm

class NotaFiscalAdmin(admin.ModelAdmin):
    list_per_page = 50
    list_display = ['numero', 'faturamento', 'posicao',
                    'saida', 'entrega', 'confirmada',
                    'natu_venda', 'ativa', 'nf_devolucao',
                    'uf', 'transp_nome',
                    'dest_nome', 'dest_cnpj',
                    'valor', 'volumes',
                    'cod_status',
                    'observacao']
    list_editable = ['saida', 'entrega', 'confirmada']
    # list_filter = ['ativa', 'natu_venda', 'saida',
    #                'entrega', 'confirmada',
    #                'faturamento', 'transp_nome', 'cod_status', 'uf']
    search_fields = ['numero', 'transp_nome', 'dest_nome']
    ordering = ['-numero']
    fields = (('numero', 'ativa', 'nf_devolucao', 'posicao'),
              ('dest_cnpj', 'dest_nome', 'uf', 'transp_nome'),
              ('pedido', 'ped_cliente'),
              ('natu_venda', 'natu_descr'),
              ('volumes', 'valor'),
              ('faturamento', 'cod_status', 'msg_status'),
              'saida', 'entrega', 'confirmada', 'observacao', 'id')
    readonly_fields = ['id', 'numero', 'faturamento', 'posicao',
                       'pedido', 'ped_cliente',
                       'volumes', 'valor',
                       'cod_status', 'msg_status', 'ativa', 'nf_devolucao',
                       'dest_cnpj', 'dest_nome', 'uf',
                       'transp_nome', 'natu_descr', 'natu_venda',
                       ]

intr_adm_site.register(models.NotaFiscal, NotaFiscalAdmin)

class PosicaoCargaAdmin(admin.ModelAdmin):
    list_display = ['nome']

intr_adm_site.register(models.PosicaoCarga, PosicaoCargaAdmin)

class RotinaLogisticaAdmin(admin.ModelAdmin):
    list_display = ['nome', 'slug']
    readonly_fields = ['slug']

intr_adm_site.register(models.RotinaLogistica, RotinaLogisticaAdmin)

class PosicaoCargaAlteracaoAdmin(admin.ModelAdmin):
    list_display = [
        'inicial', 'ordem', 'descricao', 'final', 'efeito', 'so_nfs_ativas']
    ordering = ['inicial', 'ordem']

intr_adm_site.register(
    models.PosicaoCargaAlteracao, PosicaoCargaAlteracaoAdmin)

class NfEntradaAdmin(admin.ModelAdmin):

    def __init__(self, *args):
        super().__init__(*args)
        self.form = EntradaNfForm

        self._list_display = [
            '__str__', 'emissor', 'descricao', 'qtd',
            'hora_entrada', 'transportadora', 'motorista', 'placa',
            'responsavel', 'usuario', 'quando'
        ]
        self._fields = [
            'cadastro', 'numero', 'emissor', 'descricao', 'qtd',
            'hora_entrada', 'transportadora', 'motorista', 'placa',
            'responsavel', 'usuario', 'quando'
        ]

        self.list_per_page = 50
        self.list_display = self._list_display.copy()
        self.list_display.insert(0, 'empresa')
        self.search_fields = ['emissor', 'numero', 'descricao']
        self.ordering = ['-quando']
        self.fields = self._fields.copy()
        self.fields.insert(0, 'empresa')
        self.readonly_fields = ['usuario', 'quando']

    class Media:
        static_url = getattr(settings, 'STATIC_URL', '/static/')
        js = [static_url+'/admin/logistica/nfEntrada.js', ]

class NfEntradaAgatorAdmin(NfEntradaAdmin):

    def __init__(self, *args):
        super().__init__(*args)

        self.list_display = self._list_display
        self.fields = self._fields

class NfEntradaTussorAdmin(NfEntradaAdmin):

    def __init__(self, *args):
        super().__init__(*args)

        self.list_display = self._list_display
        self.fields = self._fields

intr_adm_site.register(
    models.NfEntrada, NfEntradaAdmin)
intr_adm_site.register(
    models.NfEntradaAgator, NfEntradaAgatorAdmin)
intr_adm_site.register(
    models.NfEntradaTussor, NfEntradaTussorAdmin)
<|endoftext|>


<commit_before>#!/usr/bin/python

import os
import logging
from subprocess import call
import psutil

INDIHUB_AGENT_OFF = 'off'
INDIHUB_AGENT_DEFAULT_MODE = 'solo'

try:
    INDIHUB_AGENT_CONFIG = os.path.join(os.environ['HOME'], '.indihub')
except KeyError:
    INDIHUB_AGENT_CONFIG = '/tmp/indihub'

if not os.path.exists(INDIHUB_AGENT_CONFIG):
    os.makedirs(INDIHUB_AGENT_CONFIG)

INDIHUB_AGENT_CONFIG += '/indihub.json'

class IndiHubAgent(object):
    def __init__(self, web_addr, hostname, port):
        self.__web_addr = web_addr
        self.__hostname = hostname
        self.__port = port
        self.__mode = INDIHUB_AGENT_OFF

        # stop running indihub-agent, if any
        self.stop()

    def __run(self, profile, mode, conf):
        cmd = 'indihub-agent -indi-server-manager=%s -indi-profile=%s -mode=%s -conf=%s -api-origins=%s > /tmp/indihub-agent.log 2>&1 &' % \
              (self.__web_addr, profile, mode, conf,
               '%s:%d,%s.local:%d' % (self.__hostname, self.__port, self.__hostname, self.__port))
        logging.info(cmd)
        call(cmd, shell=True)

    def start(self, profile, mode=INDIHUB_AGENT_DEFAULT_MODE, conf=INDIHUB_AGENT_CONFIG):
        if self.is_running():
            self.stop()

        self.__run(profile, mode, conf)
        self.__mode = mode

    def stop(self):
        self.__mode = 'off'
        cmd = ['pkill', '-2', 'indihub-agent']
        logging.info(' '.join(cmd))
        ret = call(cmd)
        if ret == 0:
            logging.info('indihub-agent terminated successfully')
        else:
            logging.warn('terminating indihub-agent failed code ' + str(ret))

    def is_running(self):
        for proc in psutil.process_iter():
            if proc.name() == 'indihub-agent':
                return True
        return False

    def get_mode(self):
        return self.__mode
<commit_msg>python linting<commit_after>#!/usr/bin/python

import os
import logging
from subprocess import call
import psutil

INDIHUB_AGENT_OFF = 'off'
INDIHUB_AGENT_DEFAULT_MODE = 'solo'

try:
    INDIHUB_AGENT_CONFIG = os.path.join(os.environ['HOME'], '.indihub')
except KeyError:
    INDIHUB_AGENT_CONFIG = '/tmp/indihub'

if not os.path.exists(INDIHUB_AGENT_CONFIG):
    os.makedirs(INDIHUB_AGENT_CONFIG)

INDIHUB_AGENT_CONFIG += '/indihub.json'

class IndiHubAgent(object):
    def __init__(self, web_addr, hostname, port):
        self.__web_addr = web_addr
        self.__hostname = hostname
        self.__port = port
        self.__mode = INDIHUB_AGENT_OFF

        # stop running indihub-agent, if any
        self.stop()

    def __run(self, profile, mode, conf):
        cmd = 'indihub-agent -indi-server-manager=%s -indi-profile=%s -mode=%s -conf=%s -api-origins=%s > ' \
              '/tmp/indihub-agent.log 2>&1 &' % \
              (self.__web_addr, profile, mode, conf,
               '%s:%d,%s.local:%d' % (self.__hostname, self.__port, self.__hostname, self.__port))
        logging.info(cmd)
        call(cmd, shell=True)

    def start(self, profile, mode=INDIHUB_AGENT_DEFAULT_MODE, conf=INDIHUB_AGENT_CONFIG):
        if self.is_running():
            self.stop()

        self.__run(profile, mode, conf)
        self.__mode = mode

    def stop(self):
        self.__mode = 'off'
        cmd = ['pkill', '-2', 'indihub-agent']
        logging.info(' '.join(cmd))
        ret = call(cmd)
        if ret == 0:
            logging.info('indihub-agent terminated successfully')
        else:
            logging.warn('terminating indihub-agent failed code ' + str(ret))

    def is_running(self):
        for proc in psutil.process_iter():
            if proc.name() == 'indihub-agent':
                return True
        return False

    def get_mode(self):
        return self.__mode
<|endoftext|>