<commit_before>try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId

from django.http import HttpRequest

from gnowsys_ndf.ndf.models import Node, GSystemType
from gnowsys_ndf.ndf.models import node_collection, triple_collection
from gnowsys_ndf.ndf.views.methods import get_group_name_id, get_language_tuple, create_grelation
from gnowsys_ndf.settings import GSTUDIO_DEFAULT_LANGUAGE

# gst_asset = node_collection.one({'_type': u'GSystemType', 'name': u'Asset'})
gst_asset_name, gst_asset_id = GSystemType.get_gst_name_id(u'Asset')
gst_page_name, gst_page_id = GSystemType.get_gst_name_id(u'Page')
gst_file_name, gst_file_id = GSystemType.get_gst_name_id(u'File')

def create_asset(name,
				group_id,
				created_by,
				node_id=None,
				content=None,
				request=HttpRequest(),
				**kwargs):
	'''
	This method is equivalent to write_files() but
	also (about to) incorporate page creation.

	So plan is to not to change write_files() which is working smoothly at various places.
	But to create another equivalent function and in future, replace this for write_files()
	'''
	if not name:
		name = request.POST.get('name') if request else None

	if not created_by:
		created_by = request.user.id if request else None

	group_name, group_id = get_group_name_id(group_id)

	# compulsory values, if not found raise error.
	# if not all([name, created_by, group_id, uploaded_files]):
	if not all([name, created_by, group_id]):
		raise ValueError('"name", "created_by", "group" are mandatory args."')

	author_obj     = node_collection.one({'_type': u'Author', 'created_by': created_by})
	author_obj_id  = author_obj._id

	group_set = [ObjectId(group_id), ObjectId(author_obj_id)]
	if node_id:
		asset_gs_obj = node_collection.one({'_id': ObjectId(node_id)})
	else:
		asset_gs_obj = node_collection.collection.GSystem()

	asset_gs_obj.fill_gstystem_values(request=request,
									name=name,
									member_of=gst_asset_id,
									group_set=group_set,
									created_by=created_by,
									content=content,
									**kwargs)

	asset_gs_obj.save(group_id=group_id)
	return asset_gs_obj

def create_assetcontent(asset_id,
						name,
						group_name_or_id,
						created_by,
						node_id=None,
						content=None,
						files=[None],
						resource_type='Page',
						request=HttpRequest(),
						**kwargs):

	# Mandatory arg: 'asset_id'. AssetContent should fall under Asset.
	# And if no Asset exists with supplied arg, can't proceeed.
	asset_obj = Node.get_node_by_id(asset_id)
	if not asset_obj:
		raise ValueError('No Asset exists with supplied asset_id.')

	if not name:
		name = request.POST.get('name') if request else None
	if not created_by:
		created_by = request.user.id if request else None

	group_name, group_id = get_group_name_id(group_name_or_id)
	if group_id not in asset_obj['group_set']:
		# AssetContent should fall under Asset. If 'group_id' arg is
		# supplied, it should fall under asset's group id.
		raise Exception('Supplied group_id and group_id of Asset does not match.')

	test_content = True if (content or files) else False

	# compulsory values, if not found raise error.
	# if not all([name, created_by, group_id, uploaded_files]):

	if not all([name, created_by, group_id, test_content]):
		raise ValueError('"asset_id", "name", "created_by", "group" and ("content" or "files") are mandatory args.')

	author_obj     = node_collection.one({'_type': u'Author', 'created_by': created_by})
	author_obj_id  = author_obj._id

	group_set = [ObjectId(group_id), ObjectId(author_obj_id)]
	gst_name_id_dict = {
		'Page': gst_page_id, 'page': gst_page_id,
		'File': gst_file_id, 'file': gst_file_id
	}

	try:
		member_of_gst_id = gst_name_id_dict[resource_type]
	except Exception, e:
		print "resource_type arg is not supplied."
		# handle condition based on files.
		member_of_gst_id = gst_file_id if files[0] else gst_page_id
	kwargs.update({'name': name})
	kwargs.update({'created_by': created_by})
	kwargs.update({'member_of': member_of_gst_id})
	kwargs.update({'group_set': group_set})
	kwargs.update({'unique_gs_per_file': True})
	if content:
		kwargs.update({'content': content})

	asset_content_obj = None
	if node_id:
		asset_content_obj = node_collection.one({'_id': ObjectId(node_id)})
		asset_content_obj.fill_node_values(**kwargs)
	else:
		asset_content_obj = node_collection.collection.GSystem()
		asset_content_obj.fill_gstystem_values(request=request,
												uploaded_file=files[0],
												**kwargs)
	asset_content_obj.save(groupid=group_id)<commit_msg>repeat call to fill_node_values<commit_after>try:
    from bson import ObjectId
except ImportError:  # old pymongo
    from pymongo.objectid import ObjectId

from django.http import HttpRequest

from gnowsys_ndf.ndf.models import Node, GSystemType
from gnowsys_ndf.ndf.models import node_collection, triple_collection
from gnowsys_ndf.ndf.views.methods import get_group_name_id, get_language_tuple, create_grelation
from gnowsys_ndf.settings import GSTUDIO_DEFAULT_LANGUAGE

# gst_asset = node_collection.one({'_type': u'GSystemType', 'name': u'Asset'})
gst_asset_name, gst_asset_id = GSystemType.get_gst_name_id(u'Asset')
gst_page_name, gst_page_id = GSystemType.get_gst_name_id(u'Page')
gst_file_name, gst_file_id = GSystemType.get_gst_name_id(u'File')

def create_asset(name,
				group_id,
				created_by,
				node_id=None,
				content=None,
				request=HttpRequest(),
				**kwargs):
	'''
	This method is equivalent to write_files() but
	also (about to) incorporate page creation.

	So plan is to not to change write_files() which is working smoothly at various places.
	But to create another equivalent function and in future, replace this for write_files()
	'''
	if not name:
		name = request.POST.get('name') if request else None

	if not created_by:
		created_by = request.user.id if request else None

	group_name, group_id = get_group_name_id(group_id)

	# compulsory values, if not found raise error.
	# if not all([name, created_by, group_id, uploaded_files]):
	if not all([name, created_by, group_id]):
		raise ValueError('"name", "created_by", "group" are mandatory args."')

	author_obj     = node_collection.one({'_type': u'Author', 'created_by': created_by})
	author_obj_id  = author_obj._id

	group_set = [ObjectId(group_id), ObjectId(author_obj_id)]
	if node_id:
		asset_gs_obj = node_collection.one({'_id': ObjectId(node_id)})
	else:
		asset_gs_obj = node_collection.collection.GSystem()

	asset_gs_obj.fill_gstystem_values(request=request,
									name=name,
									member_of=gst_asset_id,
									group_set=group_set,
									created_by=created_by,
									content=content,
									**kwargs)

	asset_gs_obj.save(group_id=group_id)
	return asset_gs_obj

def create_assetcontent(asset_id,
						name,
						group_name_or_id,
						created_by,
						node_id=None,
						content=None,
						files=[None],
						resource_type='Page',
						request=HttpRequest(),
						**kwargs):

	# Mandatory arg: 'asset_id'. AssetContent should fall under Asset.
	# And if no Asset exists with supplied arg, can't proceeed.
	asset_obj = Node.get_node_by_id(asset_id)
	if not asset_obj:
		raise ValueError('No Asset exists with supplied asset_id.')

	if not name:
		name = request.POST.get('name') if request else None
	if not created_by:
		created_by = request.user.id if request else None

	group_name, group_id = get_group_name_id(group_name_or_id)
	if group_id not in asset_obj['group_set']:
		# AssetContent should fall under Asset. If 'group_id' arg is
		# supplied, it should fall under asset's group id.
		raise Exception('Supplied group_id and group_id of Asset does not match.')

	test_content = True if (content or files) else False

	# compulsory values, if not found raise error.
	# if not all([name, created_by, group_id, uploaded_files]):

	if not all([name, created_by, group_id, test_content]):
		raise ValueError('"asset_id", "name", "created_by", "group" and ("content" or "files") are mandatory args.')

	author_obj     = node_collection.one({'_type': u'Author', 'created_by': created_by})
	author_obj_id  = author_obj._id

	group_set = [ObjectId(group_id), ObjectId(author_obj_id)]
	gst_name_id_dict = {
		'Page': gst_page_id, 'page': gst_page_id,
		'File': gst_file_id, 'file': gst_file_id
	}

	try:
		member_of_gst_id = gst_name_id_dict[resource_type]
	except Exception, e:
		print "resource_type arg is not supplied."
		# handle condition based on files.
		member_of_gst_id = gst_file_id if files[0] else gst_page_id
	kwargs.update({'name': unicode(name)})
	kwargs.update({'created_by': created_by})
	kwargs.update({'member_of': member_of_gst_id})
	kwargs.update({'group_set': group_set})
	kwargs.update({'unique_gs_per_file': True})
	if content:
		kwargs.update({'content': content})

	asset_content_obj = None
	if node_id:
		asset_content_obj = node_collection.one({'_id': ObjectId(node_id)})
	else:
		asset_content_obj = node_collection.collection.GSystem()
		asset_content_obj.fill_gstystem_values(request=request,
												uploaded_file=files[0],
												**kwargs)
	asset_content_obj.fill_node_values(**kwargs)
	asset_content_obj.save(groupid=group_id)
{'fraction_non_alphanumeric': 0.10364390137464542, 'fraction_numerical': 0.0004363953742090334, 'mean_word_length': 4.941023979261179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '121231', 'n_tokens_mistral': 3539, 'n_tokens_neox': 3152, 'n_words': 758}