Ejemplo n.º 1
0
 def clone_wf(self, request, pk=None):
     user_name = request.user.first_name + request.user.last_name
     custom_name = request.data.get('name')
     workflow_obj = WorkFlow.objects.get(pk=pk)
     workflow_transition = Transition.objects.values("name", "action", "is_initial", "is_final", "auth_type",
                                                     "state_from_id", "state_to_id", "auth_user", "auth_group").filter(workflow_id_id=pk)
     workflow_obj.id = None
     workflow_obj.pk = None
     workflow_obj.is_template = False
     workflow_obj.name = workflow_obj.name + ' - ' + custom_name
     workflow_obj.save()
     for item in workflow_transition:
         item.update({"workflow_id": workflow_obj.id, "created_by": 1})
         t = TransitionSerializer(data=item)
         if t.is_valid():
             t.save()
         else:
             logger.info("Error: " + t.errors)
     request_data = request.data
     request_data.update(
         {"id": workflow_obj.id, 'user_id': request.user.id})
     param = {'field': 'workflow_id',
              'label': 'Workflow', 'user_name': user_name}
     log_view = LogView()
     log_view.generate_log(request_data, param)
     return Response(workflow_obj.id)
Ejemplo n.º 2
0
    def share_dir(self, request, pk=None, *args, **kwargs):
        track_fields = {
            c.can_read: c.read,
            c.can_write: c.write,
            c.can_delete: c.delete
        }
        log_view = LogView()
        share_res = request.data
        share_res.update({c.directory_id: int(pk)})
        share = ShareSerializer(data=share_res)

        if share.is_valid():
            share.save()
            param = {
                c.field: c.directory_id,
                c.label: c.directory,
                c.action: c.share
            }
            request_data = share.data
            request_data.update({c.user_id: request.user.id})
            log_view.generate_log(request_data, param, "", track_fields, "")

            return Response(share.data)
        else:
            return Response(share.errors)
Ejemplo n.º 3
0
 def update_status(self, request, pk=None, state_id=None):
     user_name = request.user.first_name + request.user.last_name
     user = User.objects.get(id=request.user.id)
     logger.debug("Data {} {} ".format(user.__dict__, user.groups))
     payload = request.data
     logger.debug("Payload {} {} ".format(pk, state_id))
     workflow_obj = WorkFlow.objects.get(id=pk)
     stage_from = workflow_obj.active_state
     target_transient = workflow_obj.workflow_transition.get(
         state_from_id=workflow_obj.active_state.id, state_to_id=state_id)
     if target_transient.auth_user is None:
         workflow_obj.active_state = State.objects.get(id=state_id)
         workflow_obj.save()
         return Response({"status": "updated"})
     elif target_transient.auth_user.id == user.id:
         workflow_obj.active_state = State.objects.get(id=state_id)
         stage_to = workflow_obj.active_state
         workflow_obj.save()
         # Activity log
         param = {'field': 'workflow_id', 'label': 'Workflow',
                  'user_name': user_name, 'workflow_id': workflow_obj.id}
         fields = [] 
         fields.append(stage_from)
         fields.append(stage_to)
         log_view = LogView()
         request_data = {}
         request_data = request.data
         request_data.update({"user_id": request.user.id})
         log_view.generate_log(request_data, param, "", fields)
         return Response({"status": "updated"})
     else:
         return Response({"status": "Invalid User/You are not auth to use it"})
     return Response("N/A")
Ejemplo n.º 4
0
 def save_editor(self, request, pk=None, *args, **kwargs):
     file_res = File.objects.get(id=pk)
     decrypt_file(
         os.path.join(settings.MEDIA_ROOT + str(
             crud.get(self.table, "*", 'where id=' +
                      pk)[0].get('owner_id')) + '/' +
                      base64.b16encode(file_res.modified_file_name)),
         os.path.join(settings.MEDIA_ROOT + file_res.modified_file_name),
         '123')
     self.org_data = convert(settings.MEDIA_ROOT +
                             FileSerializer(File.objects.get(
                                 id=pk)).data.get('modified_file_name'))
     os.remove(
         os.path.join(settings.MEDIA_ROOT + file_res.modified_file_name))
     if self.org_data != request.data:
         # Activity log
         request_data = {}
         param = {'field': 'file_id', 'file_id': pk, 'label': 'version'}
         track_fields = {
             c.can_read: c.read,
             c.can_write: c.write,
             c.can_delete: c.delete
         }
         request_data.update({'user_id': request.user.id})
         log_view = LogView()
         log_view.generate_log(request_data, param, "", track_fields)
         f = open(
             settings.MEDIA_ROOT + str(
                 crud.get(self.table, "*",
                          'where id=' + pk)[0].get('owner_id')) + '/' +
             file_res.modified_file_name.split('.')[0] + '.html', 'w')
         f.write(request.data['data'].encode())
         f.close()
         os.chdir(settings.MEDIA_ROOT + str(
             crud.get(self.table, "*", 'where id=' +
                      pk)[0].get('owner_id')))
         os.system('unoconv --format=' + file_res.name.split('.')[-1] +
                   ' ' + settings.MEDIA_ROOT + str(
                       crud.get(self.table, "*", 'where id=' +
                                pk)[0].get('owner_id')) + '/' +
                   file_res.modified_file_name.split('.')[0] + '.html')
         time.sleep(3)
         os.remove(settings.MEDIA_ROOT + str(
             crud.get(self.table, "*", 'where id=' +
                      pk)[0].get('owner_id')) + '/' +
                   file_res.modified_file_name.split('.')[0] + '.html')
         encrypt_file(
             os.getcwd() + '/' + file_res.modified_file_name,
             os.getcwd() + '/' +
             base64.b16encode(file_res.modified_file_name), '123')
         os.remove(os.getcwd() + '/' + file_res.modified_file_name)
     return Response({"hai": 'hai'})
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
    def upload_file(self, request, *args, **kwargs):

        file = request.data.get('file')
        if request.data.get('directory_id') != 'null':
            modified_file_name = map(
                lambda x: x.replace("\'", "").strip()
                if x.find("'") != -1 else x.replace(' ', '_').strip(),
                file.name.split('.'))
            modified_file_name[-1] = '.' + modified_file_name[-1]
            modified_file_name.insert(-2, request.data.get('directory_id'))
        else:
            modified_file_name = map(
                lambda x: x.replace("\'", "").strip()
                if x.find("'") != -1 else x.replace(' ', '_').strip(),
                file.name.split('.'))
            modified_file_name[-1] = '.' + modified_file_name[-1]
        try:
            if not os.path.isdir(settings.MEDIA_ROOT + str(request.user.id)):
                os.mkdir(settings.MEDIA_ROOT + str(request.user.id))
            upload_dir = default_storage.save(''.join(modified_file_name),
                                              ContentFile(file.read()))
            user_name = request.user.first_name + request.user.last_name
            user_id = request.user.id
            tmp_file = os.path.join(settings.MEDIA_ROOT, upload_dir)
            encrypt_file(
                os.path.join(settings.MEDIA_ROOT, upload_dir),
                os.path.join(settings.MEDIA_ROOT + str(request.user.id),
                             base64.b16encode(upload_dir)), '123')
            os.remove(tmp_file)
            file_data = {
                c.name: file.name,
                "modified_file_name": ''.join(modified_file_name),
                "file_type": file.name.split('.')[-1] or 'n/a',
                "size": file.size,
                "file_content_type": file.content_type,
                "created_by_id": request.user.id,
                "owner_id": request.user.id,
                "created_date": datetime.now(),
                "modified_date": datetime.now()
            }
            connection_es.index(index='dms_test',
                                doc_type='post',
                                body={
                                    'name': file.name,
                                    'type': 'File',
                                    'content_type': file.content_type,
                                    'owner_id': request.user.id
                                })
            if request.data.get('directory_id') != 'null':
                file_data.update(
                    {c.directory_id: request.data.get('directory_id')})
            param = {
                'field': 'file_id',
                'label': 'File',
                'user_name': user_name
            }
            act_log_data = file_data
            response = crud.add(self.table, file_data)
            file_data.update({"id": response})
            act_log_data.update({'user_id': user_id})
            log_view = LogView()
            log_view.generate_log(act_log_data, param)
            return Response(response)
        except Exception as e:
            return Response({"error": e})
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    def create(self, request):
        """
        <b>Author: </b> Manickam \n
        Service to create a new Directory. Send payload based on Example value.
        \n
        """
        try:
            uam_func_sync(request, c.strUAMLink, c.strAppName, c.strAppClient,
                          "File Manager ", "Create",
                          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
        user_id = request.user.id
        # directory_data = request.data
        # tags = directory_data.pop(c.tags)
        # response = crud.add(self.table, directory_data)

        # return Response(response)
        serializer = DirectorySerializer(data=request.data)
        if serializer.is_valid():
            directory_data = request.data
            tag = [
                istags for istags in directory_data.keys() if istags == c.tags
            ]
            if istags:
                tags = directory_data.pop("tags")
            directory_data.update({
                'created_date':
                datetime.now(),
                'modified_date':
                datetime.now(),
                'created_by_id':
                directory_data.get('owner_id'),
                'modified_by_id':
                directory_data.get('owner_id')
            })
            #tags = directory_data.pop(c.tags)
            # directory_data.update({c.created_date:datetime.now(),c.modified_date:datetime.now(),c.created_by_id:directory_data.get(c.owner_id),c.modified_by_id:directory_data.get(c.owner_id)})
            directory_id = crud.add(self.table, directory_data)
            if tags:
                for tag in tags:
                    directory_tag_data = {
                        c.directory_id: directory_id,
                        c.tag_id: tag
                    }
                    crud.add(c.tag_dir_rel, directory_tag_data)
            param = {
                'field': 'directory_id',
                'label': 'Directory',
                'user_name': user_name
            }
            # self.add_log(act_log_data=serializer.data)
            act_log_data = request.data
            act_log_data.update({'id': directory_id, 'user_id': user_id})
            log_view = LogView()
            log_view.generate_log(act_log_data, param)
            # return Response(directory_id)
            # act_log_data=request.data
            # act_log_data.update({'id':directory_id})
            # log_view = LogView()
            # log_view.generate_log(act_log_data,param)e

            response = crud.get(self.table, c.all,
                                c.where_id_param.format(directory_id))
            return Response(response[0])
        else:
            return Response(serializer.errors)