Пример #1
0
def create_dashboard(drop=False):
    from cripts.dashboards.dashboard import SavedSearch, Dashboard
    if drop:
        Dashboard.drop_collection()
        SavedSearch.drop_collection()
    defaultDashboard = Dashboard.objects(name="Default",
                                         analystId__not__exists=1,
                                         isPublic=True).first()
    if not defaultDashboard:
        defaultDashboard = Dashboard()
        defaultDashboard.name = "Default"
        defaultDashboard.isPublic = True
        defaultDashboard.save()
        for title in [
                "Counts",
        ]:
            savedSearch = SavedSearch()
            savedSearch.name = title
            savedSearch.dashboard = defaultDashboard.id
            savedSearch.isDefaultOnDashboard = True
            savedSearch.tableColumns = getColumnsForTable(title)
            if title == "Counts":
                savedSearch.sizex = 10

            savedSearch.save()
        print "Default Dashboard Created."
    else:
        print "Default Dashboard already exists."
Пример #2
0
def generate_search_for_saved_table(user, id=None,request=None):
    """
    Called by edit_save_search in views.py. This is for editing a previously
    saved table or one of the default dashboard tables
    """
    from cripts.core.handlers import data_query
    response = {}
    savedSearch = None
    try:
        savedSearch = SavedSearch.objects(id=id).first()
        if not savedSearch:
            response['Result'] = "ERROR"
            response['Message'] = "Error finding table, please try again later."
            return response
    except:
        savedSearch = SavedSearch()
        savedSearch.isDefaultOnDashboard = True
        savedSearch.name = id.replace("_", " ")
        id = None
    results = []
    records = []
    term = ""
    url = ""
    if not savedSearch.isDefaultOnDashboard:
        objType = get_obj_type_from_string(savedSearch.objType)
        resp = get_query_without_request(objType, user.username, savedSearch.searchTerm, "global")
        if resp['Result'] == "ERROR":
            return resp
        formatted_query = resp['query']
        term = resp['term']
        resp = data_query(objType, user.username, query=formatted_query, count=True)
        results.append({'count': resp['count'],
                                      'name': savedSearch.objType}) 
    else:
        results = {"name":savedSearch.name,
                   "count":str(len(records)),
                   "type":get_obj_name_from_title(savedSearch.name)}
        #special url to get the records of a default dashboard since their queries are different 
        url = reverse("cripts.dashboards.views.get_dashboard_table_data", 
                      kwargs={"tableName":str(savedSearch.name.replace(" ", "_"))})
    args = {'term': term,
            'results': results,
            'dataUrl':url,
            'Result': "OK"
            }
    if savedSearch:
        args.update({'tableId':id,
                'tableName': savedSearch.name,
                'columns': savedSearch.tableColumns,
                'sortBy': savedSearch.sortBy,
                'sizex' : savedSearch.sizex,
                'maxRows': savedSearch.maxRows,
                'isDefaultOnDashboard': savedSearch.isDefaultOnDashboard,
                })
        if savedSearch.dashboard:
            args["currentDash"] = str(savedSearch.dashboard)
            args["dashtheme"] = Dashboard.objects(id=savedSearch.dashboard).first().theme
    return args
Пример #3
0
def save_data(userId, columns, tableName, searchTerm="", objType="", sortBy=None, 
              tableId=None, isDefaultOnDashboard=False, maxRows=0,
              dashboard=None, clone=False, row=0, grid_col=0, sizex=0,
              sizey=0):
    """
    Saves the customized table in the dashboard. Called by save_search and
    save_new_dashboard via ajax in views.py.
    """
    try:
        if searchTerm:
            searchTerm = HTMLParser.HTMLParser().unescape(searchTerm)
        #if user is editing a table
        if tableId :
            newSavedSearch = SavedSearch.objects(id=tableId).first()
            if not newSavedSearch:
                raise Exception("Cannot find Table")
            elif clone:
                clonedSavedSearch = cloneSavedSearch(newSavedSearch, dashboard.id)
        else:
            newSavedSearch = SavedSearch()
        cols = []
        for col in columns:
            if "field" not in col or "caption" not in col:
                continue
            cols.append(col)
        if not cols:
            raise("There are no columns to save")
        newSavedSearch.tableColumns = cols
        newSavedSearch.name = tableName
        oldDashId = None
        if dashboard and newSavedSearch.dashboard != dashboard.id:
            newSavedSearch.dashboard= dashboard.id
        #if it is not a deault dashboard table, it must have a searchterm and objtype
        if searchTerm:
            newSavedSearch.searchTerm = searchTerm
        if objType:
            newSavedSearch.objType = objType
        #this is to identify the default tables on every user dashboard
        newSavedSearch.isDefaultOnDashboard = isDefaultOnDashboard
        if sortBy:
            newSavedSearch.sortBy = sortBy
        if sizex:
            newSavedSearch.sizex = sizex
        elif not newSavedSearch.sizex:
            newSavedSearch.sizex = 50
        if sizey:
            newSavedSearch.sizey = sizey
        elif maxRows and maxRows != newSavedSearch.maxRows:
            newSavedSearch.sizey = int(maxRows)+1
        elif not newSavedSearch.sizey:
            newSavedSearch.sizey = 7
        if row:
            newSavedSearch.row = row
        elif not newSavedSearch.row:
            newSavedSearch.row = 1
        if grid_col:
            newSavedSearch.col = grid_col
        elif not newSavedSearch.col:
            newSavedSearch.col = 1
        if maxRows:
            newSavedSearch.maxRows = maxRows;
        newSavedSearch.save()
        #if the old dashboard is empty, delete it
        if oldDashId:
            deleteDashboardIfEmpty(oldDashId)
    except Exception as e:
        print "ERROR: "
        print e
        return {'success': False,
                'message': "An unexpected error occurred while saving table. Please refresh and try again"}
    return {'success': True,'message': tableName+" Saved Successfully!"}