server/src/metaeducation/signals.py
author ymh <ymh.work@gmail.com>
Fri, 11 Mar 2016 00:03:15 +0100
changeset 20 af79d3dd81f7
parent 12 76bcfd35a363
child 29 23de98e32b3b
permissions -rw-r--r--
Added tag 00.00.02 for changeset dc3f8366729d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     1
from django.db.models.signals import post_save
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     2
from django.dispatch import receiver
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     3
from django.contrib.auth import get_user_model
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     4
from django.conf import settings
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     5
from django.core.urlresolvers import reverse
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
     6
import requests, json, sys
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     8
def reference_created_renkan(sender, instance, created, **kwargs):
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
     9
    from renkanmanager.models import Renkan
9
fdbc47f06361 adding custom user model + corrected provider to correctly create user according to new model
durandn
parents: 8
diff changeset
    10
    if created and sender == Renkan and not instance.source_revision_guid:
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    11
        token_response = requests.post(
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    12
            settings.MTDC_CLIENT_CREDENTIALS_TOKEN_URL+"?grant_type=client_credentials",
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    13
            data = {},
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    14
            headers = {
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    15
                "Authorization": "Basic %s" % settings.MTDC_AUTH_CODE
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    16
            }          
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    17
        )
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    18
        if token_response.status_code == 200:
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    19
            token = json.loads(token_response.text)['access_token']
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    20
            post_data = json.dumps({
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    21
                "id": str(instance.renkan_guid),
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    22
                "idOutil": settings.RENKAN_TOOL_ID,
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    23
                "idUser": instance.creator.external_id,
12
76bcfd35a363 transferred reference request arg into settings
durandn
parents: 11
diff changeset
    24
                "pf": settings.ITOP_PF_CODE,
76bcfd35a363 transferred reference request arg into settings
durandn
parents: 11
diff changeset
    25
                "uai": "",
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    26
                "redirect_uri": settings.OAUTH_REDIRECT_URI,
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    27
                "title": instance.title
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    28
            })
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    29
            resource_ws_url = settings.MTDC_REFERENCE_RESOURCE_BASE_URL# + str(instance.renkan_guid)
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    30
            
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    31
            reference_response = requests.post(
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    32
                resource_ws_url,
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    33
                data = post_data,
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    34
                headers = {
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    35
                    "Authorization": "Bearer %s" % token,
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    36
                    "content-type": "application/json"
7
cb21b50b7793 small corrections on oauth server and allauth provider + implemented post_save signals to reference resources into itop ged
durandn
parents:
diff changeset
    37
                }                             
11
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    38
            )
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    39
            
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    40
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    41
if not 'test' in sys.argv:
cfc868991b82 Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents: 9
diff changeset
    42
    post_save.connect(reference_created_renkan)