示例#1
0
    def is_valid(self, bundle, request=None):
        errors = {}

        if 'description' in bundle.data:
            if not WALL_VAL.match(bundle.data['description']):
                errors['description'] = 'The description is not correct'

        return errors
示例#2
0
    def post(self, request, type_upload):
        response_data = {'status': 'ko', 'message': 'Error in parameters'}
        file_uploaded = request.FILES['file']

        if not WALL_VAL.match(file_uploaded.name):
            response_data = {
                'status': 'ko',
                'message': 'The name of the file is not correct'
            }
        elif file_uploaded.content_type not in settings.SUPPORTED_FILES:
            response_data = {
                'status': 'ko',
                'message': 'You cannot upload this kind of file'
            }
        elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE:
            response_data = {
                'status':
                'ko',
                'message':
                'You cannot upload a file bigger than %d MB' %
                settings.FILE_SIZE
            }
        elif type_upload == 'profile':
            entity = request.POST['entity']
            project_id = None
            if 'project' in request.POST:
                project_id = request.POST['project']

            if entity in ('project', 'user'):
                new_filename = generate_filename(file_uploaded.name)

                if entity == 'project':
                    obj = get_object_or_404(Project, id=project_id)
                    obj.image_name = new_filename
                    obj.save()
                elif entity == 'user':
                    request.user.profile.picture_name = new_filename
                    request.user.profile.save()

                upload_file(file_uploaded, entity, new_filename)
                response_data = {
                    'status': 'ok',
                    'message': 'File uploaded correctly'
                }
        elif type_upload == 'document':
            relative_path = upload_file(file_uploaded,
                                        'documents',
                                        with_subpath=True)
            response_data = {
                'status': 'ok',
                'message': 'File uploaded correctly',
                'url': relative_path
            }

        return HttpResponse(json.dumps(response_data),
                            mimetype='application/json')
示例#3
0
    def is_valid(self, bundle, request=None):
        errors = {}

        if 'title' in bundle.data and request.method == 'POST':
            try:
                project = Project.objects.get(title=bundle.data['title'])
            except Project.DoesNotExist:
                project = None
            if project:
                errors['title'] = 'There is another project with this title'

        if 'title' in bundle.data and request.method in ('POST', 'PUT'):
            if not WALL_VAL.match(bundle.data['description']):
                errors['description'] = 'The description is not correct'
            if 'tags' in bundle.data:
                if not WALL_VAL.match(bundle.data['tags']):
                    errors['tags'] = 'The tags are not correct'

        return errors
示例#4
0
    def is_valid(self, bundle, request=None):
        errors = {}

        if "title" in bundle.data and request.method == "POST":
            try:
                project = Project.objects.get(title=bundle.data["title"])
            except Project.DoesNotExist:
                project = None
            if project:
                errors["title"] = "There is another project with this title"

        if "title" in bundle.data and request.method in ("POST", "PUT"):
            if not WALL_VAL.match(bundle.data["description"]):
                errors["description"] = "The description is not correct"
            if "tags" in bundle.data:
                if not WALL_VAL.match(bundle.data["tags"]):
                    errors["tags"] = "The tags are not correct"

        return errors
示例#5
0
    def is_valid(self, bundle, request=None):
        errors = {}

        if 'state' in bundle.data:
            if bundle.data['state'] and not WORD_VAL.match(bundle.data['state']):
                errors['state'] = 'The state is not correct'
        if 'city' in bundle.data:
            if bundle.data['city'] and not WORD_VAL.match(bundle.data['city']):
                errors['city'] = 'The city is not correct'
        if 'twitter' in bundle.data:
            if bundle.data['twitter'] and not WORD_VAL.match(bundle.data['twitter']):
                errors['twitter'] = 'twitter account is not correct'
        if 'facebook' in bundle.data:
            if bundle.data['facebook'] and not WALL_VAL.match(bundle.data['facebook']):
                errors['facebook'] = 'The facebook is not correct'
        if 'linkedin' in bundle.data:
            if bundle.data['linkedin'] and not WALL_VAL.match(bundle.data['linkedin']):
                errors['linkedin'] = 'The linkedin is not correct'
        return errors
示例#6
0
    def is_valid(self, bundle, request=None):
        if not bundle.data:
            return {'__all__': 'There is no data'}

        errors = {}

        if 'text' in bundle.data:
            if not WALL_VAL.match(bundle.data['text']):
                errors['text'] = ['The text is not correct']

        return errors
示例#7
0
    def post(self, request):
        file_uploaded = request.FILES['file']
        project_part_id = request.POST['project_part']

        if not WALL_VAL.match(file_uploaded.name):
            response_data = {
                'status': 'ko',
                'message': 'The name of the file is not correct'
            }
        elif file_uploaded.content_type not in settings.SUPPORTED_FILES:
            response_data = {
                'status': 'ko',
                'message': 'You cannot upload this kind of file'
            }
        elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE:
            response_data = {
                'status':
                'ko',
                'message':
                'You cannot upload a file bigger than %d MB' %
                settings.FILE_SIZE
            }
        else:
            project_part = ProjectPart.objects.get(pk=project_part_id)
            file_obj = File(created_user=request.user,
                            original_name=file_uploaded.name,
                            system_name=file_uploaded.name,
                            project_part=project_part)
            if os.path.exists(file_obj.get_file_path(True)):
                file_obj.set_original_name()
            with open(file_obj.get_file_path(True), 'wb+') as destination:
                for chunk in file_uploaded.chunks():
                    destination.write(chunk)

            if (file_obj.is_image()):
                file_obj.generate_thumbnail()
            file_obj.save()
            Activity.objects.set_activity('add', file_obj)

            if request.user.profile.drive_token and project_part.project.drive_id:
                drive_util = DriveUtil(request.user.profile.drive_token)
                if not project_part.drive_id:
                    project_part_folder = drive_util.get_project_part_folder(
                        request.user.profile.drive_folder, project_part)
                else:
                    project_part_folder = project_part.drive_id
                drive_util.insert_file(file_uploaded.name, 'description',
                                       project_part_folder,
                                       file_uploaded.content_type,
                                       file_obj.get_file_path(True))

            response_data = {'status': 'ok'}
        return HttpResponse(json.dumps(response_data),
                            mimetype='application/json')
示例#8
0
    def is_valid(self, bundle, request=None):
        if not bundle.data:
            return {'__all__': 'There is no data'}

        errors = {}

        if 'text' in bundle.data:
            if not WALL_VAL.match(bundle.data['text']):
                errors['text'] = ['The text is not correct']

        return errors
示例#9
0
    def is_valid(self, bundle, request=None):
        errors = {}

        if 'state' in bundle.data:
            if bundle.data['state'] and not WORD_VAL.match(
                    bundle.data['state']):
                errors['state'] = 'The state is not correct'
        if 'city' in bundle.data:
            if bundle.data['city'] and not WORD_VAL.match(bundle.data['city']):
                errors['city'] = 'The city is not correct'
        if 'twitter' in bundle.data:
            if bundle.data['twitter'] and not WORD_VAL.match(
                    bundle.data['twitter']):
                errors['twitter'] = 'twitter account is not correct'
        if 'facebook' in bundle.data:
            if bundle.data['facebook'] and not WALL_VAL.match(
                    bundle.data['facebook']):
                errors['facebook'] = 'The facebook is not correct'
        if 'linkedin' in bundle.data:
            if bundle.data['linkedin'] and not WALL_VAL.match(
                    bundle.data['linkedin']):
                errors['linkedin'] = 'The linkedin is not correct'
        return errors
示例#10
0
    def post(self, request):
        file_uploaded = request.FILES["file"]
        project_part_id = request.POST["project_part"]

        if not WALL_VAL.match(file_uploaded.name):
            response_data = {"status": "ko", "message": "The name of the file is not correct"}
        elif file_uploaded.content_type not in settings.SUPPORTED_FILES:
            response_data = {"status": "ko", "message": "You cannot upload this kind of file"}
        elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE:
            response_data = {
                "status": "ko",
                "message": "You cannot upload a file bigger than %d MB" % settings.FILE_SIZE,
            }
        else:
            project_part = ProjectPart.objects.get(pk=project_part_id)
            file_obj = File(
                created_user=request.user,
                original_name=file_uploaded.name,
                system_name=file_uploaded.name,
                project_part=project_part,
            )
            if os.path.exists(file_obj.get_file_path(True)):
                file_obj.set_original_name()
            with open(file_obj.get_file_path(True), "wb+") as destination:
                for chunk in file_uploaded.chunks():
                    destination.write(chunk)

            if file_obj.is_image():
                file_obj.generate_thumbnail()
            file_obj.save()
            Activity.objects.set_activity("add", file_obj)

            if request.user.profile.drive_token and project_part.project.drive_id:
                drive_util = DriveUtil(request.user.profile.drive_token)
                if not project_part.drive_id:
                    project_part_folder = drive_util.get_project_part_folder(
                        request.user.profile.drive_folder, project_part
                    )
                else:
                    project_part_folder = project_part.drive_id
                drive_util.insert_file(
                    file_uploaded.name,
                    "description",
                    project_part_folder,
                    file_uploaded.content_type,
                    file_obj.get_file_path(True),
                )

            response_data = {"status": "ok"}
        return HttpResponse(json.dumps(response_data), mimetype="application/json")
示例#11
0
    def post(self, request, type_upload):
        response_data = {"status": "ko", "message": "Error in parameters"}
        file_uploaded = request.FILES["file"]

        if not WALL_VAL.match(file_uploaded.name):
            response_data = {"status": "ko", "message": "The name of the file is not correct"}
        elif file_uploaded.content_type not in settings.SUPPORTED_FILES:
            response_data = {"status": "ko", "message": "You cannot upload this kind of file"}
        elif file_uploaded.size * 1.0 / 1048576 > settings.FILE_SIZE:
            response_data = {
                "status": "ko",
                "message": "You cannot upload a file bigger than %d MB" % settings.FILE_SIZE,
            }
        elif type_upload == "profile":
            entity = request.POST["entity"]
            project_id = None
            if "project" in request.POST:
                project_id = request.POST["project"]

            if entity in ("project", "user"):
                new_filename = generate_filename(file_uploaded.name)

                if entity == "project":
                    obj = get_object_or_404(Project, id=project_id)
                    obj.image_name = new_filename
                    obj.save()
                elif entity == "user":
                    request.user.profile.picture_name = new_filename
                    request.user.profile.save()

                upload_file(file_uploaded, entity, new_filename)
                response_data = {"status": "ok", "message": "File uploaded correctly"}
        elif type_upload == "document":
            relative_path = upload_file(file_uploaded, "documents", with_subpath=True)
            response_data = {"status": "ok", "message": "File uploaded correctly", "url": relative_path}

        return HttpResponse(json.dumps(response_data), mimetype="application/json")