Ejemplo n.º 1
0
        def wrapper_func(request, *args, **kwargs):
            method_args = request.GET if request.method == 'GET' else request.POST

            default_enc = 'json'

            parsed_required_args = []
            for required_arg in required_args:
                (required_arg, type, default_value) = parse_required_arg(required_arg)
                parsed_required_args.append([required_arg, type, default_value])

                if required_arg == 'enc':
                    default_enc = default_value

            enc = method_args.get('enc', default_enc)

            # Checking method.
            if method and request.method != method:
                return error_response.create(enc, '%s method not allowed.' % request.method)

            transformed_values = {}
            
            try:
                # Checking required arguments.
                for (required_arg, type, default_value) in parsed_required_args:
                    if default_value == None and \
                            not required_arg in method_args and \
                            not required_arg in kwargs:
                        return error_response.create(enc, 'Missing required field %s.' % required_arg)

                    try:
                        value = method_args.get(required_arg, kwargs.get(required_arg, default_value))
                        new_name = required_arg
                        if type in transformers.transform_funcs:
                            (new_name, value) = transformers.transform_funcs[type](request, value)
                        if required_arg in transformers.transform_funcs:
                            (new_name, value) = \
                                transformers.transform_funcs[required_arg](request, value)
                        args += (value,)
                        transformed_values[new_name or required_arg] = value
                    except transformers.TransformError, (errMessage):
                        return error_response.create(enc, str(errMessage))

                # Checking permissions
                for permission in permissions:
                    if not permissionscheckers.permission_funcs[permission](request,
                                                                            method_args,
                                                                            transformed_values):
                        return error_response.create(enc, 'Does not have %s permission.' % permission \
                                                              if localsettings.DEBUG \
                                                              else 'Permission violation')
Ejemplo n.º 2
0
def get_post_token_value(request):
    from django.contrib.csrf.middleware import _make_token
    from flowgram.core import log
    
    try:
        session_id = request.COOKIES[settings.SESSION_COOKIE_NAME]
    except KeyError:
        log.critical("get_post_token found no sessionid for authenticated user %s" % request.user)
        return error_response.create(get(request, 'enc', 'json'), 'Session cookie required')

    return _make_token(session_id)
Ejemplo n.º 3
0
def file_upload(request):
    # Uses GET parameters instead of POST for passing data, handled by Flex.
    file = request.FILES['file']
    content = file['content']
    content_type = file['content-type']
    filename = file['filename']
    filetype = request.GET.get('filetype', '')
    enc = request.GET.get('enc', 'json')
    
    flowgram_id = request.GET.get('flowgram_id')
    flowgram = get_object_or_404(models.Flowgram, id=flowgram_id) \
                   if flowgram_id \
                   else controller.get_working_flowgram(request.user)
    if not permissions.can_edit(request.user, flowgram):
        return error_response.create(enc, 'Does not have edit_flowgram permission.' \
                                              if localsettings.DEBUG \
                                              else 'Permission violation')

    if filetype == 'image' or content_type.startswith('image'):
        return handle_file_upload_image(enc, content, content_type, filetype, filename, flowgram)
    elif filetype in DOC_TYPE_EXTENSIONS:
        return handle_file_upload_doc(enc, content, content_type, filetype, filename, flowgram)
    elif filetype == 'media':
        page = None
        try:
            page_id = request.GET.get('page_id')
            page = models.Page.objects.get(id=page_id)
        except:
            pass

        if page and not permissions.can_edit(request.user, page):
            return error_response.create(enc, 'Does not have edit_page permission.' \
                                                  if localsettings.DEBUG \
                                                  else 'Permission violation')

        return handle_file_upload_media(enc, content, content_type, filetype, filename, \
                                            page or flowgram)
    else:
        return error_response.create(enc, 'Unsupported file type')
Ejemplo n.º 4
0
def save_custom_page(request, enc, flowgram, content):
    page_id = request.POST.get('page_id', '')
    page = models.Page.objects.get(id=page_id) if page_id else None

    user = flowgram.owner

    if page:
        # If editing an existing page.

        # Checking to make sure the page belongs to the specified Flowgram.
        if not page.flowgram == flowgram:
            return error_response.create(
                enc,
                'The specified page does not belong to the specified Flowgram.')

        html = render_to_string('blankpages/view.html', {'html': content,
                                                         'user': user})
        controller.set_page_contents(page, html, sethl=True)

        # This try..except block is used for legacy support (previous versions did not save a
        # separate CustomPage object).
        try:
            custom_page = models.CustomPage.objects.get(page=page)
            custom_page.content = content
            custom_page.save()
        except models.CustomPage.DoesNotExist:
            models.CustomPage.objects.create(page=page, content=content)

        controller.make_thumbnail(page)

        return data_response.create(enc, 'ok', encode.page.to_dict(page))
    else:
        # If creating a new page.

        html = render_to_string('blankpages/view.html', {'html': content,
                                                         'user': user})
        
        title = "%s's custom Flowgram page" % user
        page = models.Page.objects.create(title=title, source_url='')
        page = controller.create_page_to_flowgram(flowgram, page, html)

        page.source_url = '%sapi/getpage/%s/' % (localsettings.my_URL_BASE, page.id)
        page.is_custom = True
        page.save()

        models.CustomPage.objects.create(page=page, content=content)

        return data_response.create(enc, 'ok', encode.page.to_dict(page))
Ejemplo n.º 5
0
def add_audio_fms(request, page, time, duration):
    file_path = '%s/%s/%s/%d.flv' % (localsettings.FMS_STREAM_DIR, page.flowgram.id, page.id, time)
    
    if os.path.isfile(file_path):
        audio = models.Audio.objects.create(page=page, time=time, duration=duration)
        audio.path = file_path
        
        helpers.set_modified(page.flowgram)
        
        s3.save_filename_to_bucket(localsettings.S3_BUCKET_AUDIO, '%s.flv' % audio.id, file_path)
        
        return data_response.create(request.POST.get('enc', 'json'), 'ok', audio.id)
    else:
        log.debug('add_audio_fms called with page.id=%s, time=%d, and duration=%d but file_path=%s DNE' % \
                      (page.id, time, duration, file_path))
        
        return error_response.create(request.POST.get('enc', 'json'), 'flv does not exist.')
Ejemplo n.º 6
0
def upload(request, enc):
    if request.FILES.has_key('file'):
        # do later in queue once per slide
        # adds a page to the fg
        # html will be like before but embed a swf that is centered
        # page = Page.objects.create(title=request.FILES['file']['filename'], position=255)
        # page = controller.create_page_to_flowgram(fg, page, html)

        (fg, new) = controller.get_working_flowgram(request.user)
        if new:
            log.debug("Action log: new working flowgram %s created during add_page" % fg.id)

        # create queue item
        ppt_import_request = models.PptImportRequest.objects.create(flowgram=fg)

        #     2. Send to S3
        ppt_import_request.save_ppt_to_s3(request.FILES['file']['content'])
        
        return data_response.create(enc, 'ok', ppt_import_request.id)
    else:
        return error_response.create(enc, 'No file was uploaded.')
Ejemplo n.º 7
0
def add_comment(request, enc, flowgram, text):
    if not text:
        # TODO(westphal): Drop support for XML.
        if enc == 'xml':
            return HttpResponse('No comment text')
        else:
            return error_response.create(enc, 'No comment text')
    comment = models.Comment.objects.create(
        owner=request.user if request.user.is_authenticated() else None,
        flowgram=flowgram,
        text=text)
    controller.record_stat(request, 'add_comment_website', '0', comment.id)
    if localsettings.FEATURE['notify_fw']:
        controller.store_fgcomented_event({'commentor': request.user,
                                           'fg_id': flowgram.id,
                                           'eventCode': 'FG_COMMENTED'})
            
    # TODO(westphal): Drop support for XML.
    if enc == 'xml':
        return HttpResponse(helpers.render_comment_to_xml(comment), mimetype="text/xml")
    else:
        return data_response.create(enc, 'ok', encode.comment.to_dict(comment))
Ejemplo n.º 8
0
def handle_file_upload_image(enc, content, content_type, filetype, filename, flowgram):
    temp_image = tempfile.mkstemp(".image")
    os.close(temp_image[0])
    temp_image_path = temp_image[1]

    try:
        file = open(temp_image_path, "wb")
        file.write(content)
        file.close()
        
        try:
            # Opens the file to make sure it works as an image.
            image = Image.open(temp_image_path)
            image.load()
        except:
            return error_response.create(
                enc,
                'Invalid image: Currently supported image types include: JPEG, PNG, and GIF')

        (mime, extension) = IMAGE_FORMAT_TO_MIME_AND_EXTENSION[image.format]

        # Resizing the image if too large.
        if image.size[0] > settings.DEFAULT_PHOTO_WIDTH or \
               image.size[1] > settings.DEFAULT_PHOTO_HEIGHT:
            image.thumbnail((settings.DEFAULT_PHOTO_WIDTH, settings.DEFAULT_PHOTO_HEIGHT),
                            Image.BICUBIC)

        page = models.Page.objects.create(title=filename)

        uploaded_file = models.UploadedFile.objects.create(page=page, mimetype=mime, media_type=0)
        page.source_url = uploaded_file.get_absolute_url()

        html = ''.join(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title></title>',
                        '<link rel="stylesheet" type="text/css" href="',
                        localsettings.MEDIA_URL,
                        'css/photo_importers.css">',
                        '<link rel="stylesheet" type="text/css" href="',
                        localsettings.MEDIA_URL,
                        'css/page.css"></head><body><div><img src="',
                        uploaded_file.get_absolute_url(),
                        '" /></div></body></html>'])

        # Saving the thumbnailed image and reading the data.
        try:
            temp_image_final = tempfile.mkstemp(extension)
            os.close(temp_image_final[0])
            temp_image_final_path = temp_image_final[1]
            image.save(temp_image_final_path)

            file = open(temp_image_final_path, "rb")
            data = file.read()
            file.close()
        finally:
            os.remove(temp_image_final_path)
        
        uploaded_file.set_file(data)

        page = controller.create_page_to_flowgram(flowgram, page, html)
        return data_response.create(enc, 'ok', encode.page.to_dict(page))
    finally:
        os.remove(temp_image_path)