Exemplo n.º 1
0
def testconn(request):
    need_decode = request.data.get('need_decode', '')
    username = request.data.get('username', '').strip()
    db_name = request.data.get('db_name', '').strip()
    db_type = request.data.get('db_type', '')
    hostname = request.data.get('hostname', '').strip()
    port = request.data.get('port')
    password = request.data.get('password', '').strip()
    if need_decode:
        try:
            password = aes_decode(password)
        except:
            pass

        url = type2jdbcurl(db_type, hostname, port, db_name=db_name)
        jsonobj = {
            'user': username,
            'password': password,
            'jdbc_url': url,
            'driver': Driver[db_type].value
        }
        r = get_java_response('test-conn/', jsonobj)
        result = r.json()
    if r.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR:
        return Response(r.json(), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    elif result.get('message') == False:
        return Response({'message': ''},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        (Database.objects.filter(db_name=db_name,
                                 db_type=db_type,
                                 hostname=hostname,
                                 port=port)).update(disabled=False)
        return Response(r.json(), status=r.status_code)
Exemplo n.º 2
0
 def update(self, request, *args, **kwargs):
     partial = kwargs.pop('partial', False)
     instance = self.get_object()
     serializer = self.get_serializer(instance,
                                      data=request.data,
                                      partial=partial)
     serializer.is_valid(raise_exception=True)
     original_is_switch_off = instance.is_switch_off
     current_is_switch_off = serializer.context.get('request').data.get(
         'is_switch_off')
     if original_is_switch_off == True:
         if current_is_switch_off == False:
             instance_count = instance.instance_count or 1
             license_checked = current_target_available()
     if license_checked is False:
         return Response({'error_message': '!'},
                         status=status.HTTP_400_BAD_REQUEST)
     else:
         if license_checked is not True:
             if license_checked - instance_count < 0:
                 return Response(
                     {
                         'error_message':
                         ('{},{},!').format(license_checked, instance_count)
                     },
                     status=status.HTTP_400_BAD_REQUEST)
             if current_is_switch_off == True and original_is_switch_off == False:
                 url = type2jdbcurl(instance.db_type,
                                    instance.hostname,
                                    instance.port,
                                    db_name=instance.db_name)
                 jsonobj = {
                     'user': instance.username,
                     'password': aes_decode(instance.password),
                     'jdbc_url': url,
                     'driver': Driver[instance.db_type].value
                 }
                 get_java_response('close-conn/', jsonobj)
             self.perform_update(serializer)
             if getattr(instance, '_prefetched_objects_cache', None):
                 instance._prefetched_objects_cache = {}
         return Response(serializer.data)
Exemplo n.º 3
0
def run_sql_with_dict(data, sql_text):
    url = type2jdbcurl(data.get('db_type'),
                       data.get('hostname'),
                       data.get('port'),
                       db_name=data.get('db_name'),
                       encoding=data.get('encoding'))
    jsonobj = {
        'user': data.get('username'),
        'password': data.get('password'),
        'jdbc_url': url,
        'driver': Driver[data.get('db_type')].value,
        'sql': sql_text
    }
    r = get_java_response('exec/', jsonobj)
    if is_response_error(r):
        return (False, r.json())
    else:
        return (True, r.json())
Exemplo n.º 4
0
def run_plsql(database, sql_text):
    if database.is_switch_off:
        return (False, {'exception': '', 'message': ''})
    url = type2jdbcurl(database.db_type,
                       database.hostname,
                       database.port,
                       db_name=database.db_name,
                       encoding=database.get_encoding())
    jsonobj = {
        'user': database.username,
        'password': database.get_password(),
        'jdbc_url': url,
        'driver': Driver[database.db_type].value,
        'sql': sql_text
    }
    r = get_java_response('exec_plsql/', jsonobj)
    if is_response_error(r):
        return (False, r.json())
    else:
        return (True, r.json())