Ejemplo n.º 1
0
def user_streams(request):
    '''
    Grab all relevent streams to display as checkboxes in the user portal.  We make sure to remove any duplicate streams from each various section.
    Presedence is given to owner, then to readable, then to public for the duplicate removal.
    '''

    response = check_access(request)
    if(response):
        return response

    if request.method == 'GET':
        #Pull streams that are owned by this user.
        owned_streams = DataStream.objects.filter(userpermission__user = request.user, userpermission__owner = True) 
        
        #Pull streams that are viewable by this user.
        viewable_streams = DataStream.objects.filter(userpermission__user = request.user, userpermission__read = True).exclude(id__in=owned_streams)

        #Pull any public streams as well
        public_streams = DataStream.objects.filter(is_public = True).exclude(id__in=viewable_streams).exclude(id__in=owned_streams)


        t = loader.get_template('user_streams.html')
        c = Context({'user':request.user,'owned_streams':owned_streams, 'public_streams':public_streams,'viewable_streams':viewable_streams})
        c.update(csrf(request))
        return HttpResponse(t.render(c))
Ejemplo n.º 2
0
def display_graph(request):
    '''
    ' Use JSON data to retrieve the appropriate datastream for rendering.
    '''
    
    user = check_access(request)
    if isinstance(user, HttpResponse):
        return user.content
    
    json_data = json.loads(request.GET['json_data'])

    try:
        stream = DataStream.objects.get(id = int(json_data['stream']))
        if not stream.can_view(user):
            raise Http404("Sorry, but you do not have permission to view this graph.")
    except ObjectDoesNotExist:
        raise Http404()

    reductions = reductFunc.keys()    
    t_graph = loader.get_template('graph.html')
    c_graph = Context({
        'id': stream.id,
        'reduction': stream.reduction_type,
        'reductions': reductions
    })

    return HttpResponse(t_graph.render(c_graph), mimetype="text/html")
Ejemplo n.º 3
0
def model_grid(request, app_name, model_name):
    '''
    ' View to return the html that will hold a models chucho.
    '''
    if check_access(request) is None:
        return HttpResponse('User not authenticated.')
    return render(request, 'chucho.html', {'model_name': model_name, 'app_name': app_name})
Ejemplo n.º 4
0
def csv(request):

    response = check_access(request)
    if(response):
        return response

    if(request.method == 'GET'):
        stream_id = request.GET.get('data_stream')
        start = request.GET.get('start')
        end = request.GET.get('end')

        if(start is None):
            #TODO display error message
            return render(request,'csv.html', context_instance=RequestContext(request))        

        if(end is None):
            #TODO display error message
            return render(request,'csv.html', context_instance=RequestContext(request))        

        data_stream = DataStream.objects.get(id = stream_id)

        readings = SensorReading.objects.filter(date_entered__gte = start, date_entered__lte = end, datastream = data_stream).order_by('date_entered')


        data = {
                    'start':start,
                    'end':end,
                    'data_stream':data_stream,
                    'readings':readings
               }
        
        return render(request,'csv.html', data, context_instance=RequestContext(request))        
Ejemplo n.º 5
0
def api_view(request, app_name, model_name, id=None):
    '''
    ' Router view for the RESTful api portion of Chucho.  All RESTFull requests come through here
    ' and gets routed to the appropriate functions when requests are for managing the data of the models.
    '
    ' Keyword Args:
    '   app_name   - (string) The application the desired model resides in
    '   model_name - (string) The model name to get serialized data from or save to
    '   id         - (string) The id of an object to delete/update for DELETE/PUSH
    '
    ' Returns: HttpResponse with serialized json data
    '''

    user = check_access(request)
    if user is None:
        errors = 'Sorry, but you must be logged in.'
        return HttpResponse(json.dumps({'errors': errors}, indent=4), content_type="application/json")

    print "app_name = {}".format(app_name)
    print "model_name = {}".format(model_name)
    if request.method == "GET":
        return read_source(request, app_name, model_name, user)
    if request.method == "POST":
        return update(request, app_name, model_name, user)
    if request.method == "PUT":
        return update(request, app_name, model_name, user, id)
    if request.method == "DELETE":
        return destroy(request, app_name, model_name, user, id)
Ejemplo n.º 6
0
def sensorsIndex(request):

    user = check_access(request)
    if not isinstance(user, User):
        return user 

    t = loader.get_template('sensors.html') 
    c = RequestContext(request, {})
    return HttpResponse(t.render(c))
Ejemplo n.º 7
0
def model_grid(request, app_name, model_name):
    '''
    ' View to return the html that will hold a models chucho.
    '''
    if check_access(request) is None:
        return HttpResponse('User not authenticated.')
    t = loader.get_template('chucho.html')
    c = RequestContext(request, {'model_name': model_name, 'app_name': app_name})
    return HttpResponse(t.render(c), content_type="text/html")
Ejemplo n.º 8
0
def streams(request):
    '''
    Grab all relevent streams to display as checkboxes in the user portal.  We make sure to remove any duplicate streams from each various section.
    Presedence is given to owner, then to readable, then to public for the duplicate removal.
    '''

    user = check_access(request)

    if isinstance(user, HttpResponse):
        return user

    t_subtree = loader.get_template('stream_subtree.html')

    #Only get owned and viewable streams if we have a logged in User
    owned_streams = []
    viewable_streams = []
    owned_subtree = None
    viewable_subtree = None
    if user is not None:
        #Pull streams that are owned by this user.
        owned_streams = DataStream.objects.filter(owner = user)
        c_dict = stream_tree_top(owned_streams)
        c_dict.update({'group':'owned'})
        owned_subtree = t_subtree.render(Context(c_dict))

        #Pull streams that are viewable by this user.
        viewable_streams = DataStream.objects.get_viewable(user).exclude(id__in=owned_streams).distinct()
        c_dict = stream_tree_top(viewable_streams)
        c_dict.update({'group':'viewable'})
        viewable_subtree = t_subtree.render(Context(c_dict))

    #Pull any public streams as well
    public_streams = DataStream.objects.filter(is_public = True).exclude(id__in=viewable_streams).exclude(id__in=owned_streams).distinct()
    c_dict = stream_tree_top(public_streams)
    c_dict.update({'group':'public'})
    public_subtree = t_subtree.render(Context(c_dict))

    t_streams = loader.get_template('user_streams.html')
    c_streams = RequestContext(request, {
            'user':request.user,
            'owned_streams': owned_streams,
            'viewable_streams': viewable_streams,
            'public_streams': public_streams,
            'owned_subtree': owned_subtree,
            'public_subtree': public_subtree,
            'viewable_subtree': viewable_subtree
            })
    t_controls = loader.get_template('graph_controls.html')
    c_controls = RequestContext(request)
    
    return HttpResponse(t_controls.render(c_controls) + t_streams.render(c_streams), mimetype='text/html')
Ejemplo n.º 9
0
def get_filter_operators(request):
    '''
    ' Return HttpResponse with JSON dump of dict of list of select option elements.
    ' This is used by the filter tool in the ui.
    '''

    user = check_access(request)
    if user is None:
        errors = 'User is not logged in properly.'
        return HttpResponse(json.dumps({'errors': errors}, indent=4), content_type="application/json")

    operators = filter_operators.keys()
    operators.sort()
    return HttpResponse(json.dumps(operators, indent=4), content_type="application/json")
Ejemplo n.º 10
0
    def get(self, request):
        returnData = {
            'errors': []
            ,'sensors': []
        }

        user = check_access(request)
        if not isinstance(user, User):
            returnData['errors'].append('User authentication failed.')
            return HttpResponse(json.dumps(returnData), content_type="application/json", status_code=401)
       
        sensors = Sensor.objects.filter(datastream__owner=user)
        returnData['sensors'] = [s.toDict() for s in sensors]
        
        return HttpResponse(json.dumps(returnData), content_type="application/json")
Ejemplo n.º 11
0
def password_form(request):
    '''
    ' This view will render the change password request form.
    '''

    portcullisUser = check_access(request)

    if isinstance(portcullisUser, HttpResponse):
        return cors_http_response_json({'errors': portcullisUser.content})
    if not isinstance(portcullisUser, AuthUser):
        return cors_http_response_json({'errors': 'User must be logged in to change password.'})

    t = loader.get_template('passwordForm.html')
    c = RequestContext(request, {'user': portcullisUser})
    return cors_http_response_json({'html': t.render(c)})
Ejemplo n.º 12
0
    def get(self, request):
        
        returnData = {
            'errors': []
            ,'streams': []
        }

        user = check_access(request)
        if not isinstance(user, User):
            returnData['errors'].append('User authentication failed.')
            return HttpResponse(json.dumps(returnData), content_type="application/json", status_code=401)
   
        streams = DataStream.objects.get_viewable(user)
        returnData['streams'] = [s.toDict() for s in streams]

        return HttpResponse(json.dumps(returnData, indent=4), content_type="application/json")
Ejemplo n.º 13
0
def skeleton(request):
    '''
    Render the side_pane skeleton.  The other stuff will be loaded in later with ajax.
    '''

    user = check_access(request)
    logged_in = True
    if user is None or isinstance(user, HttpResponse):
        logged_in = False


    t = loader.get_template('side_pane.html')
    c = RequestContext(request, {
            'streams': streams(request).content,
            'logged_in': logged_in
            })
    return HttpResponse(t.render(c), mimetype='text/html')
Ejemplo n.º 14
0
def change_password(request):
    '''
    ' This view will allow a user to change their password.
    '
    ' POST arguments:
    '   jsonData - JSON data containing:
    '              oldPassword - string containing user's current password.
    '              newPassword - string containing password to change to.
    '''

    portcullisUser = check_access(request)

    if isinstance(portcullisUser, HttpResponse):
        return cors_http_response_json({'errors': portcullisUser.content})
    if not isinstance(portcullisUser, AuthUser):
        return cors_http_response_json({'errors': 'Please log in before changing your password.'})

    jsonData = request.REQUEST.get('jsonData', None)

    try:
        jsonData = json.loads(jsonData)
    except Exception as e:
        return cors_http_response_json({'errors': 'JSON Exception: %s: %s' % (type(e), e.message)})

    try:
        oldPassword = jsonData['oldPassword']
        newPassword = jsonData['newPassword']
    except KeyError as e:
        return cors_http_response_json({'errors': 'KeyError: %s' % e.message})

    # Make sure old password is valid
    user = authenticate(username=portcullisUser.get_username(), password=oldPassword)
    if user is None or user != portcullisUser:
        return cors_http_response_json({'errors': 'Authentication Error: Username and password are not correct'})
    elif not user.is_active:
        error = 'Authentication Error: User is not active.  You must be active to change password.'
        return cors_http_response_json({'errors': error})

    # Change the password
    portcullisUser.set_password(newPassword)
    portcullisUser.save()
    return cors_http_response_json({'success': 'Password successfully changed!'})
Ejemplo n.º 15
0
def get_columns(request, app_name, model_name):
    '''
    ' Return a HttpResponse with JSON serialized column list for rendering a grid representing a
    ' model.
    '
    ' Keyword args:
    '   app_name   - The application the desired model resides in.
    '   model_name - The name of the model to represent.
    '''

    user = check_access(request)
    if user is None:
        errors = 'User is not logged in properly.'
        return HttpResponse(json.dumps({'errors': errors}, indent=4), content_type="application/json")

    cls = apps.get_model(app_name, model_name)

    filter_depth = None
    if hasattr(cls, 'fk_filter_depth'):
        filter_depth = cls.fk_filter_depth

    return HttpResponse(json.dumps(gen_columns(cls, False, filter_depth), indent=4), content_type="application/json")
Ejemplo n.º 16
0
def stream_subtree(request):
    '''
    ' This function will take a partial datastream name, delimited
    ' with '|' and return the next level of the subtree
    ' that matches.
    '''

    portcullisUser = check_access(request)
    if isinstance(portcullisUser, HttpResponse):
        dump = json.dumps({'access_error': 'Sorry, but you are not logged in.'})
        return HttpResponse(dump, content_type="application/json")

    try:
        jsonData = json.loads(request.GET.get('jsonData'))
    except:
        dump = json.dumps({'errors': 'Error getting json data'})
        return HttpResponse(dump, content_type="application/json")

    name = jsonData['name']
    group = jsonData['group']

    # Check that we are logged in before trying to filter the streams
    if isinstance(portcullisUser, AuthUser):
        if group == 'owned':
            streams = DataStream.objects.filter(name__startswith=name)
            streams = streams.filter(owner=portcullisUser)
        elif group == 'public':
            streams = DataStream.objects.filter(name__startswith=name)
            streams = streams.filter(is_public=True).exclude(owner=portcullisUser)
        else:
            dump = json.dumps({'errors': 'Error: %s is not a valid datastream type.' % group})
            return HttpResponse(dump, content_type="application/json")

    elif group == 'public':
        streams = DataStream.objects.filter(name__startswith=name)
        streams = streams.filter(is_public=True)
    else:
        dump = json.dumps({'errors': 'Error: You must be logged in to see the %s datastream type.' % group})
        return HttpResponse(dump, content_type="application/json")

    level = name.count('|')
    nodes = {}
    leaves = {}

    for s in streams:
        split_name = s.name.split('|')
        n_name = split_name[level]

        # Is this a node or leaf?
        if len(split_name) > level + 1:
            if (n_name) not in nodes:
                nodes[n_name] = None
        elif n_name not in leaves:
            leaves[n_name] = s.id
        else:
            dump = json.dumps({'errors': 'Duplicate name in Database!'})
            return HttpResponse(dump, content_type="application/json")

    t = loader.get_template('stream_subtree.html')
    nodes = OrderedDict(sorted(nodes.iteritems(), key=lambda t: t[0]))
    leaves = OrderedDict(sorted(leaves.iteritems(), key=lambda t: t[0]))
    c = Context({
            'nodes':  nodes,
            'leaves': leaves,
            'path':   name,
            'group':  group
            })
    return HttpResponse(json.dumps({'html': t.render(c)}), content_type="application/json")
Ejemplo n.º 17
0
def create_saved_view(request):
    '''
    ' Create a savedView and return the token or link for that shared view.
    '''

    portcullisUser = check_access(request)

    if isinstance(portcullisUser, HttpResponse):
        transaction.rollback()
        return cors_http_response_json({'errors': portcullisUser.content})

    if request.user.is_anonymous():
        transaction.rollback()
        return cors_http_response_json({'errors': "Must be logged in to create saved view"})

    if 'jsonData' not in request.POST:
        transaction.rollback()
        return cors_http_response_json({'errors': "Unrecongnized data"})

    try:
        jsonData = json.loads(request.POST['jsonData'])
    except Exception as e:
        transaction.rollback()
        return cors_http_response_json({'errors': 'Ivalid json: %s' % e.message})

    expires = timezone.now() + timedelta(days=7)
    key = Key.objects.generateKey(portcullisUser, 'Saved view', expires, 20)

    # Create a SavedView object and graphs, and save data.
    savedView = SavedView.objects.create(key = key)

    try:
        start = jsonData['start']
        end = jsonData['end']
        gran = jsonData['granularity']
    except Exception as e:
        transaction.rollback()
        return cors_http_response_json({'errors': 'Error getting json data: %s: %s:' % (type(e), e.message)})

    for graphData in jsonData['graphs']:
        try:
            ds = DataStream.objects.get(id = graphData['ds_id'])
        except DataStream.DoesNotExist:
            transaction.rollback()
            return cors_http_response_json({'errors': 'Datastream does not exist.'})
        except Exception as e:
            transaction.rollback()
            return cors_http_response_json({'errors': 'Unknown error occurred: %s: %s' % (type(e), e.message)})
                               
        # Make sure not to add the key if not the owner.
        if key not in ds.can_read.all() and portcullisUser == ds.owner and not ds.is_public:
            ds.can_read.add(key)
                               
        try:
            reduction = graphData['reduction']
            zoom_start = graphData['zoom_start']
            zoom_end = graphData['zoom_end']
        except Exception as e:
            transaction.rollback()
            return cors_http_response_json({'errors': 'Error getting json data for datastream %d: %s' % (ds.id, e.message)})
        
        graph = SavedDSGraph(datastream = ds, start = start, end = end,
                             reduction_type = reduction, granularity = gran,
                             zoom_start = zoom_start, zoom_end = zoom_end)
        try:
            graph.save()
            savedView.widget.add(graph)
        except Exception as e:
            transaction.rollback()
            return cors_http_response_json({'errors': 'Error saving graph: %s' % e.message})
        
    link = reverse('portcullis-saved-view', args = ['saved_view', key.key])
    transaction.commit()
    return cors_http_response_json({'html':'<a href="%s">%s</a>' % (link, link)})
Ejemplo n.º 18
0
def stream_subtree(request, name, group):
    '''
    ' This function will take a partial datastream name, delimited
    ' with '|' and return the next level of the subtree
    ' that matches.
    '
    ' Keyword Args:
    '    name - The 'path' of the subtree (beggining of the name of
    '           the items interested in.)
    '    group - The group to get the subtree for.  Is this a public
    '            group, or an owned group or a
    '            viewable group.
    '''

    portcullisUser = check_access(request)
    if isinstance(portcullisUser, HttpResponse):
        return portcullisUser.content

    # Check that we are logged in before trying to filter the streams
    if isinstance(portcullisUser, AuthUser):
        if group == 'owned':
            streams = DataStream.objects.filter(name__startswith=name)
            streams = streams.filter(owner=portcullisUser)
        elif group == 'viewable':
            streams = DataStream.objects.get_viewable(portcullisUser)
            streams = streams.filter(name__startswith=name)
            streams = streams.exclude(owner=portcullisUser)
        elif group == 'public':
            streams = DataStream.objects.filter(name__startswith=name)
            viewableStreams = DataStream.objects.get_viewable(portcullisUser)
            streams = streams.filter(is_public=True).exclude(owner=portcullisUser)
            streams = streams.exclude(id__in=viewableStreams)
        else:
            return json.dumps({'errors': 'Error: %s is not a valid datastream type.' % group})

    elif group == 'public':
        streams = DataStream.objects.filter(name__startswith=name)
        streams = streams.filter(is_public=True)
    else:
        return json.dumps({'errors': 'Error: You must be logged in to see the %s datastream type.' % group})

    level = name.count('|')
    nodes = {}
    leaves = {}

    for s in streams:
        split_name = s.name.split('|')
        n_name = split_name[level]

        # Is this a node or leaf?
        if len(split_name) > level + 1:
            if (n_name) not in nodes:
                nodes[n_name] = None
        elif n_name not in leaves:
            leaves[n_name] = s.id
        else:
            return json.dumps({'errors': 'Duplicate name in Database!'})

    t = loader.get_template('stream_subtree.html')
    nodes = OrderedDict(sorted(nodes.iteritems(), key=lambda t: t[0]))
    leaves = OrderedDict(sorted(leaves.iteritems(), key=lambda t: t[0]))
    c = Context({
            'nodes':  nodes,
            'leaves': leaves,
            'path':   name,
            'group':  group
            })
    return json.dumps({'html': t.render(c)})
Ejemplo n.º 19
0
def display_graphs(request):
    '''
    Grabs all relevent data streams that are to be displayed and returns them to the display_nodes
    template. There are two ways for this to be called. The first is through the user portal after
    logging in.  This requires them to check off all streams they want to see and we grab them. 
    Otherwise we get url parameters through a GET and obtain the proper streams that way.

    TODO: The share this view linkage needs GET in order to work.  Would like to seperate that 
          functionality out to have POST as well from the user portal perhaps.  
    '''

    response = check_access(request)
    if(response):
        return response

    if(request.method == 'GET'):
        node = request.GET.get('node')
        port = request.GET.get('port')
        start = request.GET.get('start')
        end = request.GET.get('end')
        granularity = request.GET.get('granularity')
        show_public= request.GET.get('show_public')

        data = {
                    'granularity':granularity,
                    'start':start,
                    'end':end,
                    'streams':DataStream.objects.none(),
               }
        
        if(granularity != None):
            data['granularity'] = int(granularity)

        #Grab all read-able streams
        if(request.GET.getlist('view')):
            for stream in request.GET.getlist('view'):
                data['streams'] = data['streams'] | DataStream.objects.filter(id = stream) 

        #Grab all owned streams by this user
        if(request.GET.getlist('owned')):
            for stream in request.GET.getlist('owned'):
                data['streams'] = data['streams'] | DataStream.objects.filter(id = stream) 

        #Grab all public streams
        if(request.GET.getlist('public')):
            for stream in request.GET.getlist('public'):
                data['streams'] = data['streams'] | DataStream.objects.filter(id = stream) 

        #TODO: Pull shared streams if the user requested them

        #It's possible for this to be empty so check then order the streams and return them
        if(data['streams']):
            data['streams'] = data['streams'].order_by('node_id', 'port_id', 'id')
            return render(request,'display_nodes.html', data, context_instance=RequestContext(request))        

        #If we have a node or node/port pair then pull streams for those otherwise pull streams
        if(node != None and port != None):
           data['streams'] = DataStream.objects.filter(node_id = int(node), port_id = int(port), users = request.user)
        elif(node != None):
            data['streams'] = DataStream.objects.filter(node_id = int(node), users = request.user)
        else:
            data['streams'] = DataStream.objects.filter(users = request.user)

        return render(request,'display_nodes.html', data, context_instance=RequestContext(request))        
conf_path_file = './config/config.json'
current_dir = os.path.dirname(os.path.realpath(__file__))

# create file result
def file_result(message: str):
    file = open(file_with_result, 'a')
    file.write(f'{message}\n')
    file.close()

if __name__ == '__main__':
    all_arr_rnd_file_names = []
    configuration = read_config_and_dir.read_config_file(conf_path_file)
    log.logger.info(f'received groups ending_files')
    # read from config file - ftp_host, ip_addr, port, user_name, password
    for ftp_host in configuration['ftp_hosts']:
        for ip_addr in configuration['ftp_hosts'][f'{ftp_host}']['ip_addrs']:
            for port in configuration['ftp_hosts'][f'{ftp_host}']['ports']:
                if check_access.check_access(ip_addr, port) == True:
                    user_name = configuration["ftp_hosts"][f"{ftp_host}"]["user_name"]
                    password = configuration["ftp_hosts"][f"{ftp_host}"]["password"]
                    log.logger.info(f'received from config file - ftp_host {ftp_host}, ip_addr {ip_addr}, port {port}, /'
                                    f'user_name {user_name}, password {password}')
                    # read arr ending files from config file
                    for key in configuration['ending_files']:
                        log.logger.info(f'{key}')
                        new_file_name = create_dir_and_files.rnd_file_create(current_dir, configuration['ending_files'][f'{key}'], ip_addr, port, user_name, password)
                        # get all new file names
                        all_arr_rnd_file_names.append(new_file_name)
                else:
                    print(f'FTP cервер {ip_addr}:{port} в данный момент не доступен')
    print(all_arr_rnd_file_names)