def retrieve(self, request, pk=None, *args, **kwargs): """Overridden GET For Particular Directory Informations about Shared & its Tags""" try: file_view_object = FileView() directory_request = pk directory_info = crud.get(self.table, "*", "where id = %s" % (directory_request)) directory_info = directory_info and directory_info[0] shared_info = crud.get( ShareView.table, "*", "where directory_id = %s " % (directory_request)) tag_info = crud.execute( "select tag_id from file_manager_directory_tags where directory_id = %s " % (directory_request)) tag_info = zip(*tag_info)[0] if tag_info and tag_info[0] else [] file_info = crud.get( FileView.table, "*", "where directory_id = {0}".format(directory_request)) file_data = file_view_object.get_file_data(request, file_info) sub_directories = crud.get( self.table, "*", "where parent_id=" + str(directory_info['id'])) if directory_info: directory_info.update({ "shared_dirs": shared_info, "tags": tag_info, "file_ids": file_data, "sub_directories": sub_directories }) return Response(directory_info) except Exception as e: return Response(e)
def get_child_dirs(self, request, pk=None, *args, **kwargs): file_view_object = FileView() # child_object = crud.get(self.table,c.all,"where # parent_id={}".format(pk)) # child_object = Directory.objects.filter(parent_id=pk,owner_id=request.user.id).order_by(c.name) files = crud.get( 'file_manager_file', "*", "where directory_id ={0} and owner_id = {1}".format( int(pk), request.user.id)) child_object = crud.get( self.table, "*", "where parent_id={0} and owner_id={1} order by name".format( int(pk), request.user.id)) child_dirs = map( lambda i: i.update({ 'file_ids': file_view_object.get_file_data(request, files), 'shared_dirs': self.get_directory_data(request, int(pk)), 'sub_directories': [ item for sublist in crud.execute( "select id from file_manager_directory where parent_id = {0}" .format(i['id'])) for item in sublist ] }), child_object) logger.debug("Dictionary:: get_child_dirs: Child Object {} ".format( child_object)) # child_dirs = DirectorySerializer(child_object, many=True) # logger.debug( # "Dictionary:: get_child_dirs: Child DIrs{}".format(child_dirs)) return Response(child_object)
def totaldocuments(self, request, *args, **kwargs): """Total Documents for an Owner/User in Dashboard""" total_documents_userid = request.user.id my_total_documents = crud.get( "file_manager_file", ["count(id) as my_file"], "where owner_id = %s" % (total_documents_userid)) my_total_documents = my_total_documents and my_total_documents[0] my_shared_documents = crud.execute( "SELECT share_id FROM file_manager_share_user_ids WHERE user_id = %s" % (total_documents_userid)) my_shared_documents = zip(*my_shared_documents) try: my_shared_documents1 = crud.get( "file_manager_share", ["count(file_id)"], " WHERE file_id is not null and id in {}".format( my_shared_documents[0]) ) if len(my_shared_documents) > 1 else crud.get( "file_manager_share", ["count(file_id)"], " WHERE file_id is not null and id = {}".format( my_shared_documents[0][0])) except Exception as err: return Response(err) my_shared_documents1 = my_shared_documents1 and my_shared_documents1[0] my_total_documents.update(my_shared_documents1) return Response(my_total_documents)
def activity_data(self,request,activity_id=None, *args,**kwargs): activity_type=request.data.get(config.type) print "ACTIVITY TYPE +++===",activity_type if activity_id and activity_type is not int : if activity_type==config.dir: data = Log.objects.get(directory_id=activity_id) elif activity_type==config.file: data = Log.objects.get(file_id=activity_id) elif activity_type=="wf": data = Log.objects.get(workflow_id=activity_id) elif activity_type==config.version: version_data=crud.execute("select activity_log_activity.id from activity_log_log inner join activity_log_activity on activity_log_log.id=activity_log_activity.log_id where activity_log_activity.name like '%version%' and file_id="+activity_id) version_data = zip(*version_data) if version_data: if len(version_data[0])>1: response = crud.get(ActivityView.table,"*", "where id in {}".format(version_data[0])) else: response = crud.get(ActivityView.table,"*", "where id={}".format(version_data[0][0])) data=response[0] return Response(response) else: logger.debug(config.Data_before_update_not_awailable) return Response({config.status:0}) if data: response_data = LogSerializer(data) return Response(response_data.data) else: data=[] else: data=[] return Response(data)
def get_mypendingtasks(self, request, *args, **kwargs): """My Pending Requests in Dashboard""" my_pending_tasks_userid = request.user.id my_pendingtasks_fileinfo = crud.execute( crud.fetch_query( c.file_manager, c.my_pendingtasks_fileinfo).format(my_pending_tasks_userid)) my_pendingtasks_fileinfo1 = map( lambda i: crud.get(self.table, "*", "where id = %s" % (i[0]))[0], my_pendingtasks_fileinfo) return Response(my_pendingtasks_fileinfo1)
def get_mytasks(self, request, *args, **kwargs): "Custom Service for My Tasks in Dashboard for an Owner/User" my_tasks_userid = request.user.id my_taks_fileinfo = crud.execute( """select id from file_manager_file where workflow_id in(select id from ntwf_workflow where is_template='false' and id in (select workflow_id from ntwf_transition where auth_user_id = %s ) and active_state_id in (select state_from_id from ntwf_transition) )""" % (my_tasks_userid)) my_tasks_fileinfo1 = map( lambda i: crud.get(self.table, "*", "where id = %s" % (i))[0], my_taks_fileinfo) return Response(my_tasks_fileinfo1)
def get_file_data(self, request, files): l = {} crud.execute( "create or replace view file_share as select fs.id,fs.file_id,fsu.user_id,fsg.group_id,fs.can_read,fs.can_write,fs.can_delete from file_manager_share as fs left join file_manager_share_group_ids as fsg on fs.id = fsg.share_id left join file_manager_share_user_ids as fsu on fs.id = fsu.share_id where file_id is not null" ) file_share_data = crud.get('file_share', "*", '') map( lambda x: l.get(x.get('file_id')).get('shared_ids').append(x) if x.get('file_id') in l else l.update( {x.get('file_id'): { 'shared_ids': [x] }}), file_share_data) map( lambda (k, v): v.update({ 'user_ids': list( set(map(lambda fd: fd.get('user_id'), v.get('shared_ids'))) ), 'group_ids': list( set(map(lambda fd: fd.get('group_id'), v.get('shared_ids')) )) }), l.iteritems()) tags = map( lambda f: f.update({ 'tags': [ i[0] for i in crud.execute( "select tag_id from {0} where file_id = {1}".format( "file_manager_file_tags", f.get('id'))) ] }) if f.get('id') else '', files) workflow_info = map( lambda i: i.update( {'workflow_id': obj.get_workflow_status(i['workflow_id'])}) if i.get('workflow_id') else '', files) shared_files = map( lambda f: f.update({'shared_files': l.get(f.get('id'))}), files) return files
def get_directory_data(self, request, parent_id='null'): l = {} directories = crud.get( self.table, "*", "where parent_id = {0} and owner_id = {1}".format( parent_id, request.user.id)) crud.execute( "create or replace view directory_share as select ds.id,ds.directory_id,dsu.user_id,dsg.group_id,ds.can_read,ds.can_write,ds.can_delete from file_manager_share as ds left join file_manager_share_group_ids as dsg on ds.id = dsg.share_id left join file_manager_share_user_ids as dsu on ds.id = dsu.share_id where directory_id is not null" ) directory_share_data = crud.get('directory_share', "*", '') map( lambda x: l.get(x.get('directory_id')).get('shared_ids').append(x) if x.get('directory_id') in l else l.update( {x.get('directory_id'): { 'shared_ids': [x] }}), directory_share_data) map( lambda (k, v): v.update({ 'user_ids': list( set(map(lambda fd: fd.get('user_id'), v.get('shared_ids'))) ), 'group_ids': list( set(map(lambda fd: fd.get('group_id'), v.get('shared_ids')) )) }), l.iteritems()) tags = map( lambda f: f.update({ 'tags': [ i[0] for i in crud. execute("select tag_id from {0} where directory_id = {1}". format("file_manager_directory_tags", f.get('id'))) ] }) if f.get('id') else '', directories) shared_dirs = map( lambda f: f.update({'shared_dirs': [l.get(f.get('id'))]}), directories) return directories
def tag_change(self, pk, requst_data, key): tags = crud.execute( "select name, file_manager_tag.id from file_manager_tag inner join file_manager_" + key + "_tags on file_manager_tag.id=file_manager_" + str(key) + "_tags.tag_id where " + key + "_id={}".format(pk)) old_tag = [] new_tag = [] new_tag_ids = {} old_tag_ids = {} if tags: for tag in tags: old_tag_ids.update({str(tag[0]): tag[1]}) old_tag.append(str(tag[0])) else: old_tag = [] directory_update = requst_data new_tags = directory_update.get('tags') newTags = "" directory_update = requst_data new_tags = directory_update.get(c.tags) newTags = "" if new_tags: new_tags = tuple(new_tags) if len(new_tags) > 1: newTags = crud.get("file_manager_tag", ['id,name'], 'where id in ' + str(new_tags)) elif len(new_tags) == 1: newTags = crud.get("file_manager_tag", ['id,name'], 'where id = ' + str(new_tags[0])) if newTags: for nt in newTags: new_tag_ids.update({str(nt.get(c.name)): nt.get('id')}) new_tag.append(str(nt.get(c.name))) # else: def lr_diff(l, r): return list(set(l).difference(r)) deleted_tag = lr_diff(old_tag, new_tag) added_tag = set(new_tag).difference(old_tag) tag_details = {} tag_details['added_tag'] = added_tag tag_details['deleted_tag'] = deleted_tag tag_details['new_tag_ids'] = new_tag_ids tag_details['old_tag_ids'] = old_tag_ids tag_details['new_tag'] = new_tag return tag_details
def get_related_files(self, request, pk=None, *args): try: files = crud.execute( "select id from file_manager_file where id in (select file_id from file_manager_file_tags where tag_id in (select tag_id from file_manager_file_tags where file_id = {0})) order by created_date desc" .format(int(pk))) files = zip(*files)[0] if files and files[0] else False if files is not False: file_object = File.objects.filter(id__in=files).order_by( c.name) shared = FileSerializer(file_object, many=True) return Response(shared.data) else: return Response([]) except Exception as e: return Response({"error": e})
def destroy(self, request, pk=None): try: deletedFileName = crud.execute( "delete from {0} where id = {1} RETURNING name".format( "file_manager_file", int(pk))) deletedFileName = zip( *deletedFileName )[0] if deletedFileName and deletedFileName[0] else False file_path = os.path.join(settings.MEDIA_ROOT, str(deletedFileName[0])) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception as e: return Response({"error": e}) return Response({"Deleted": "File Removed"}) except Exception as e: return Response({"error": e})
def retrieve(self, request, pk=None, *args, **kwargs): """Overridden GET For Particular File Informations about Shared & its Tags""" try: file_request = int(pk) file_info = crud.get( self.table, "*", "where id = %s order by created_date desc" % (file_request)) file_info = file_info and file_info[0] shared_info = crud.get(ShareView.table, "*", "where file_id = %s " % (file_request)) tag_info = crud.execute( "select tag_id from file_manager_file_tags where file_id = %s " % (file_request)) tag_info = zip(*tag_info)[0] if tag_info and tag_info[0] else [] shared_files = self.get_file_data(request, [file_info]) return Response(file_info) except Exception as e: return Response(e)
def get_shared_files(self, request, *args, **kwargs): """Provide Response about Shared Files for a particular User and the Group """ user_id = request.user.id try: files = crud.execute( """select fs.file_id,fu.user_id,fg.group_id from file_manager_share as fs inner join file_manager_share_user_ids as fu on (fs.id = fu.share_id and fu.user_id = {0}) and file_id is not null inner join auth_user_groups as f*g on f*g.user_id = {0} left join file_manager_share_group_ids as fg on fg.group_id = f*g.group_id and fs.id = fg.share_id """ .format(user_id)) files = zip(*files)[0] if files and files[0] else False if files is not False: file_object = File.objects.filter(id__in=files).order_by( c.name) shared = FileSerializer(file_object, many=True) return Response(shared.data) else: return Response({"status": "No Files"}) except Exception as e: return Response({"error": e})
def get_shared_dirs(self, request, *args, **kwargs): """Provide Response about Shared Directories for a particular User and the Group""" user_id = request.user.id try: dirs = crud.execute( """select s.directory_id,u.user_id,g.group_id from file_manager_share as s inner join file_manager_share_user_ids as u on (s.id = u.share_id and u.user_id = {0}) and directory_id is not null inner join auth_user_groups as ag on ag.user_id = {0} left join file_manager_share_group_ids as g on g.group_id= ag.group_id and s.id = g.share_id """ .format(user_id)) dirs = zip(*dirs)[0] if dirs and dirs[0] else False logger.info("dirs======================>", dirs) if dirs is not False: dir_object = Directory.objects.filter(id__in=dirs).order_by( c.name) shared = DirectorySerializer(dir_object, many=True) return Response(shared.data) else: return Response({"status": "No Directories"}) except Exception as e: return Response({"error": e})
def list(self, request, *args, **kwargs): """Overridden GET For File Informations Created by a Particular Owner """ try: response = request.user.id file_info = crud.get( self.table, "*", "where owner_id = " + str(response) + " order by created_date desc") shared_files = self.get_file_data(request, file_info) shared_info = map( lambda i: i.update({ 'tags': [ item for sublist in crud.execute( "select tag_id from file_manager_file_tags where file_id = %s " % (i['id'])) for item in sublist ], 'shared_ids': crud.get(ShareView.table, "*", "where file_id = %s " % (i['id'])) }), file_info) return Response(file_info) except Exception as e: return Response(e)
def update(self, request, pk=None): try: uam_func_sync(request, c.strUAMLink, c.strAppName, c.strAppClient, "File Manager ", "Update", request.session.session_key, request.META.get('HTTP_X_FORWARDED_FOR')) except Exception as e: logger.info(e) user_name = request.user.first_name + request.user.last_name try: # request = dict(request.data) param = { 'field': 'directory_id', 'label': 'Directory', 'user_name': user_name } # print "\n\n\n@@@@@@data @@",request.data # payload = request.data # #payload=serializer.data # print "original data:%%%%%%%%,",payload original_data = crud.get(self.table, c.all, 'where id=' + str(pk))[0] # tags = crud.execute("select tag_id from file_manager_directory_tags where directory_id={}".format(pk)) # shared_ids = crud.execute("select id from file_manager_share where directory_id={}".format(pk)) # original_data.update({c.tags:list(tags and zip(*tags)[0]), 'shared_ids':list(shared_ids and zip(*shared_ids)[0])}) # logger.debug("Files:: get_parent_dirs: Parent Files{}".format(original_data)) # #try: # payload.update({"id":pk}) # tags = payload.pop(c.tags) # file_ids=payload.pop("file_ids") # parent_id=payload.pop("parent_id") # shared_ids = payload.pop("shared_ids") # sub_directories=payload.pop("sub_directories") # workflow_id=payload.pop(c.workflow_id) # response = crud.update(self.table,payload ) # print "Response",response param = { 'field': 'directory_id', 'label': 'Directory', 'user_name': user_name } original_data = crud.get(self.table, c.all, 'where id=' + str(pk))[0] dir_obj = Directory.objects.get(id=pk) serializer = DirectorySerializer(dir_obj, data=request.data) if serializer.is_valid(): tag_details = self.tag_change(pk, request.data, "directory") new_tag_ids = {} old_tag_ids = {} added_tag = tag_details.get("added_tag") deleted_tag = tag_details.get("deleted_tag") new_tag_ids.update(tag_details.get("new_tag_ids")) old_tag_ids.update(tag_details.get("old_tag_ids")) directory_update = request.data for i in directory_update.keys(): if i in [ c.tags, 'parent_id', 'workflow_id', 'shared_dirs', 'file_ids', 'sub_directories' ]: if i == c.tags: update_tag = directory_update.pop(i) else: directory_update.pop(i) directory_id = crud.update(self.table, directory_update) if added_tag: for tag in added_tag: directory_tag_data = { c.directory_id: directory_id, "tag_id": new_tag_ids.get(tag) } crud.add(c.tag_dir_rel, directory_tag_data) if deleted_tag: for tag in deleted_tag: try: crud.execute( "delete from file_manager_directory_tags where tag_id={} and directory_id={}" .format(old_tag_ids.get(tag), pk), True) except Exception as err: return Response(err) logger.debug("Files:: Serializer{}".format(request.data)) log_view = LogView() original_data1 = {} map( lambda x: original_data1.update( {str(x): original_data.get(x)}), original_data) # original_data1.update({c.tags:old_tag}) current_data = request.data current_data.update({'user_id': request.user.id}) ChangeTag = {} if added_tag: ChangeTag.update({ "Added": { c.field: "Tag", "tag": [Tag for Tag in added_tag], "action": "Added" } }) if deleted_tag: ChangeTag.update({ "Removed": { c.field: "Tag", "tag": [Tag for Tag in deleted_tag], "action": "Removed" } }) log_view.generate_log( current_data, param, original_data1, self.track_fields, ChangeTag, ) return Response( crud.get(self.table, c.all, c.where_id_param.format(directory_id))) else: logger.debug("Files:: Serializer{}".format(serializer.errors)) return Response(serializer.errors) except Exception as err: return Response(err)
def generate_log(self, request, param=None, original_data=None, fields=None, ChangeTag=None): activity = ActivityView() #print "1=============================>param:",param,"\nreques",request,'\n === fileds',fields if param is not None: if fields is None: table = Log._meta.db_table name=config.Created log_data = { config.created_by_id: request.get(config.user_id), config.created_date: datetime.now(), config.modified_date: datetime.now(),config.modified_by_id:request.get(config.user_id)} log_data.update({param.get(config.field):request.get(config.id)}) id = crud.add(table,log_data) logger.debug(config.Generating_log.format(id)) if id: if isinstance(id,int): message=str(param.get(config.label))+" "+str(events.get(config.Created)) logger.debug(config.Create_message.format(message)) activity.generate_activity(request,events.get(config.Created),message,id) else: logger.error(config.Log_id_not_found,id) else: # print " 2 ====================================================param==>",param,'\n==> reques=>',request,'\n change tag',ChangeTag,'\n====,fileds',fields updated_value=[] message_data=[] table = Log._meta.db_table if original_data and fields: #change_value=jd.diff(original_data[0],request) change_value=jd.diff((original_data),dict(request),syntax=config.symmetric) updated_value = {key: value for key, value in change_value.items() if fields.get(key)} if updated_value: #updated_value = filter(lambda key:key in fields,change_value) mes={message_data.append(str(param.get(config.label))+' '+str(fields.get(k).get(config.label))+config.has_been_changed_from+str(updated_value.get(k)[0])+" to "+str(updated_value.get(k)[1])) for k in updated_value.keys()} id=original_data.get(config.id) else: logger.debug(config.actvity_update.format(config.Track_fields_not_match)) id=None if ChangeTag: if config.Added in ChangeTag.keys(): name=config.Updated msg=message_data.append(str(ChangeTag.get(config.Added).get(config.field)+" "+str( [j for i, j in enumerate(ChangeTag.get(config.Added).get(config.tag))])+" - "+ ChangeTag.get(config.Added).get(config.action)+" for "+str(param.get(config.label))+" "+request.get(config.name) )) id=original_data.get(config.id) if config.Removed in ChangeTag.keys(): msg=message_data.append(str(ChangeTag.get(config.Removed).get(config.field)+" "+str( [j for i, j in enumerate(ChangeTag.get(config.Removed).get(config.tag))])+" - "+ ChangeTag.get(config.Removed).get(config.action)+" From "+str(param.get(config.label))+" "+request.get(config.name) )) id=original_data.get(config.id) name=config.Updated elif param.get(config.label)==config.Workflow: id=param.get(config.workflow_id) name=config.Updated mes={message_data.append(str(param.get(config.label))+" "+config.has_been_changed_from+str(fields[0])+" to "+str(fields[1]))} message={} message.update({config.message:message_data}) elif param.get(config.action)==config.share: id=request.get(param.get(config.field)) name=config.Updated uname=[] def get_name(self,table_name,parameters,id): name=crud.get(table_name,parameters,config.where_id.format(id)) if table_name==config.auth_user: name=str(name[0].get(parameters[0])+" "+name[0].get(parameters[1])).title() if table_name==config.auth_group: name=str(name[0].get(parameters[0])).title() return name permission=map(lambda k:fields.get(k) if request.get(k)!=False else None,[i for i in fields.keys()]) if request.get(config.user_ids): uname=map(lambda id:get_name(self,config.auth_user,[config.first_name,config.last_name],id),request.get(config.user_ids)) mes={message_data.append(str(param.get(config.label))+" "+config.share_user_msg+" "+str(uname)+" "+config.with_key+" "+str([p for p in permission if p != None ])+" "+config.permissions)} if request.get(config.group_ids): groups=map(lambda gid:get_name(self,config.auth_group,[config.name],gid),request.get(config.group_ids)) mes={message_data.append(str(param.get(config.label))+" "+config.share_group_msg+" "+str(groups)+" "+config.with_key+" "+str([p for p in permission if p != None ])+" "+config.permissions )} message={} message.update({config.message:message_data}) elif param.get(config.label)==config.version: id=param.get(config.file_id) name=config.version #verion_count=crud.execute(crud.fetch_query("activity_log","version_count").format(param.get(config.file_id))) verion_count=crud.execute("select count(activity_log_log.id)from activity_log_log inner join activity_log_activity on activity_log_log.id=activity_log_activity.log_id where activity_log_activity.name like '%version%' and file_id="+id) verion_count=verion_count[0][0] mes={message_data.append(str(param.get(config.label))+" "+str(verion_count+1))} message={} message.update({config.message:message_data}) if id: log=crud.get(self.table,config.all,config.where +param.get(config.field)+'='+str(id)) log_id=log[0].get(config.id) if isinstance(log_id,int): for msg in message_data: activity.generate_activity(request,events.get(name),msg,log_id) else: logger.debug(config.Log_id_not_found,log_id) else: logger.debug(config.Data_before_update_not_awailable) else: Response(config.Parametter_missing)
def update(self, request, pk=None): user_name = request.user.first_name + request.user.last_name track_fields = { c.name: { c.field: c.name, c.label: "Name" }, c.tags: { c.field: c.tags, c.label: "Tags" } } param = {'field': 'file_id', 'label': 'File', 'user_name': user_name} original_data = crud.get(self.table, c.all, 'where id=' + pk)[0] logger.debug( "Files:: get_parent_dirs: Parent Files{}".format(original_data)) dir_obj = File.objects.get(id=pk) serializer = FileSerializer(dir_obj, data=request.data) if serializer.is_valid(): dir_view = DirectoryView() tag_details = dir_view.tag_change(pk, request.data, "file") new_tag_ids = {} old_tag_ids = {} added_tag = tag_details.get("added_tag") new_tag = tag_details.get("new_tag") deleted_tag = tag_details.get("deleted_tag") new_tag_ids.update(tag_details.get("new_tag_ids")) old_tag_ids.update(tag_details.get("old_tag_ids")) file_update = request.data update_tag = file_update.pop(c.tags) serializer.save() file_id = pk # if added_tag: # for tag in added_tag: # file_tag_data = {"file_id": file_id, # "tag_id": new_tag_ids.get(tag)} # crud.add('file_manager_file_tags', file_tag_data) if deleted_tag: for tag in deleted_tag: try: crud.execute( "delete from file_manager_file_tags where tag_id={} and file_id={}" .format(old_tag_ids.get(tag), pk), True) except Exception as err: return Response(err) log_view = LogView() original_data1 = {} map( lambda x: original_data1.update({str(x): original_data.get(x)} ), original_data) ChangeTag = {} if added_tag: ChangeTag.update({ "Added": { c.field: "Tag", "tag": [Tag for Tag in added_tag], "action": "Added" } }) if deleted_tag: ChangeTag.update({ "Removed": { c.field: "Tag", "tag": [Tag for Tag in deleted_tag], "action": "Removed" } }) request_data = request.data request_data.update({'user_id': request.user.id}) log_view.generate_log(request.data, param, original_data1, track_fields, ChangeTag) # settings.connection_es.update(index='dms_test',doc_type='post',body={'doc':{'name':file.name,'type':'File','content_type':file.content_type,'owner_id':request.user.id,'tags':''}}) return Response(serializer.data) else: return Response(serializer.errors)