def username_add_update(name, description, source=None, method='', reference='', analyst=None, datasets=None, bucket_list=None, ticket=None, is_validate_only=False, cache={}, related_id=None, related_type=None, relationship_type=None): retVal = {} if not source: return {"success" : False, "message" : "Missing source information."} is_item_new = False username_object = None cached_results = cache.get(form_consts.UserName.CACHED_RESULTS) if cached_results != None: username_object = cached_results.get(username) else: username_object = UserName.objects(name=name).first() if not username_object: username_object = UserName() username_object.name = name username_object.description = description is_item_new = True if cached_results != None: cached_results[username] = username_object if not username_object.description: username_object.description = description or '' elif username_object.description != description: username_object.description += "\n" + (description or '') if isinstance(source, basestring): source = [create_embedded_source(source, reference=reference, method=method, analyst=analyst)] if source: for s in source: username_object.add_source(s) else: return {"success" : False, "message" : "Missing source information."} if bucket_list: username_object.add_bucket_list(bucket_list, analyst) if ticket: username_object.add_ticket(ticket, analyst) related_obj = None if related_id: related_obj = class_from_id(related_type, related_id) if not related_obj: retVal['success'] = False retVal['message'] = 'Related Object not found.' return retVal resp_url = reverse('cripts.usernames.views.username_detail', args=[username_object.username_id]) if is_validate_only == False: username_object.save(username=analyst) #set the URL for viewing the new data if is_item_new == True: # Update the username stats counts = mongo_connector(settings.COL_COUNTS) count_stats = counts.find_one({'name': 'counts'}) if not count_stats or ('counts' not in count_stats): count_stats = {'counts':{}} if 'UserNames' not in count_stats['counts']: count_stats['counts']['UserNames'] = 0 else: count_stats['counts']['UserNames'] = count_stats['counts']['UserNames'] + 1 counts.update({'name': "counts"}, {'$set': {'counts': count_stats['counts']}}, upsert=True) retVal['message'] = ('Success! Click here to view the new UserName: '******'<a href="%s">%s</a>' % (resp_url, username_object.name)) else: message = ('Updated existing UserName: '******'<a href="%s">%s</a>' % (resp_url, username_object.name)) retVal['message'] = message retVal['status'] = form_consts.Status.DUPLICATE retVal['warning'] = message elif is_validate_only == True: if username_object.id != None and is_item_new == False: message = ('Warning: UserName already exists: ' '<a href="%s">%s</a>' % (resp_url, username_object.name)) retVal['message'] = message retVal['status'] = form_consts.Status.DUPLICATE retVal['warning'] = message if related_obj and username_object and relationship_type: relationship_type=RelationshipTypes.inverse(relationship=relationship_type) username_object.add_relationship(related_obj, relationship_type, analyst=analyst, get_rels=False) username_object.save(username=analyst) # run username triage if is_item_new and is_validate_only == False: username_object.reload() run_triage(username_object, analyst) retVal['success'] = True retVal['object'] = username_object return retVal
def username_add_update(name, description, source=None, method='', reference='', analyst=None, datasets=None, bucket_list=None, ticket=None, is_validate_only=False, cache={}, related_id=None, related_type=None, relationship_type=None): retVal = {} if not source: return {"success": False, "message": "Missing source information."} is_item_new = False username_object = None cached_results = cache.get(form_consts.UserName.CACHED_RESULTS) if cached_results != None: username_object = cached_results.get(username) else: username_object = UserName.objects(name=name).first() if not username_object: username_object = UserName() username_object.name = name username_object.description = description is_item_new = True if cached_results != None: cached_results[username] = username_object if not username_object.description: username_object.description = description or '' elif username_object.description != description: username_object.description += "\n" + (description or '') if isinstance(source, basestring): source = [ create_embedded_source(source, reference=reference, method=method, analyst=analyst) ] if source: for s in source: username_object.add_source(s) else: return {"success": False, "message": "Missing source information."} if bucket_list: username_object.add_bucket_list(bucket_list, analyst) if ticket: username_object.add_ticket(ticket, analyst) related_obj = None if related_id: related_obj = class_from_id(related_type, related_id) if not related_obj: retVal['success'] = False retVal['message'] = 'Related Object not found.' return retVal resp_url = reverse('cripts.usernames.views.username_detail', args=[username_object.username_id]) if is_validate_only == False: username_object.save(username=analyst) #set the URL for viewing the new data if is_item_new == True: # Update the username stats counts = mongo_connector(settings.COL_COUNTS) count_stats = counts.find_one({'name': 'counts'}) if not count_stats or ('counts' not in count_stats): count_stats = {'counts': {}} if 'UserNames' not in count_stats['counts']: count_stats['counts']['UserNames'] = 0 else: count_stats['counts'][ 'UserNames'] = count_stats['counts']['UserNames'] + 1 counts.update({'name': "counts"}, {'$set': { 'counts': count_stats['counts'] }}, upsert=True) retVal['message'] = ( 'Success! Click here to view the new UserName: '******'<a href="%s">%s</a>' % (resp_url, username_object.name)) else: message = ('Updated existing UserName: '******'<a href="%s">%s</a>' % (resp_url, username_object.name)) retVal['message'] = message retVal['status'] = form_consts.Status.DUPLICATE retVal['warning'] = message elif is_validate_only == True: if username_object.id != None and is_item_new == False: message = ('Warning: UserName already exists: ' '<a href="%s">%s</a>' % (resp_url, username_object.name)) retVal['message'] = message retVal['status'] = form_consts.Status.DUPLICATE retVal['warning'] = message if related_obj and username_object and relationship_type: relationship_type = RelationshipTypes.inverse( relationship=relationship_type) username_object.add_relationship(related_obj, relationship_type, analyst=analyst, get_rels=False) username_object.save(username=analyst) # run username triage if is_item_new and is_validate_only == False: username_object.reload() run_triage(username_object, analyst) retVal['success'] = True retVal['object'] = username_object return retVal