Пример #1
0
def serve_attachment(request, file_type, file_id):
    ''' Serve attachment for viewing or download '''
    attch = {
        'drawing': DrawingAttachment,
        'revision': RevisionAttachment,
        'comment': CommentAttachment,
        'reply': ReplyAttachment
    }
    try:
        attachment = attch[file_type].objects.get(pk=file_id)
        filepath = attachment.upload.name
        filename = attachment.filename(filepath=filepath)
        full_path = os.path.join(settings.MEDIA_ROOT, filepath)
        with open(full_path, 'rb') as attch:
            response = httpresp(
                attch.read(), content_type=mimetypes.guess_type(full_path)[0])
            response['Content-Disposition'] = 'filename={}'.format(filename)
            return response

    except Exception as ex:
        return httpresp('''Error: {} <br/>
                        Unable to serve file:  file_id: {}
                        </br>Please notify James Kominick
                        </br>Close this tab'''\
                        .format(ex, file_id))
Пример #2
0
def get_definition(request, word_text):
    """ get word defintion """
    word = Word.objects.filter(text=word_text)
    word_def = ''
    if word.exists():
        word_def = word[0].definition

    data = {'definition': word_def}
    return httpresp(json.dumps(data), content_type='application/json')
Пример #3
0
def dump_data(request, dump_type):
    ''' Dump database json to
        /backup/user/ or /backup/auto '''
    if not request.user.is_superuser:
        return httprespred(reverse('tracking:index'))

    if dump_type == 'json':
        zip_name = 'data_user_json_dump.zip'
        dump_name = 'data_user_dump.json'
        dump_names = [dump_name]
        dump_path = os.path.join(settings.BASE_DIR, 'backup', 'user',
                                 dump_name)
        zip_path = os.path.join(settings.BASE_DIR, 'backup', 'user', zip_name)

        subprocess.call([
            sys.executable, 'manage.py', 'dumpdata', '--indent', '2',
            '--verbosity', '0', '-o', dump_path
        ],
                        env=os.environ.copy())

    else:  #dump_type=='csv':
        zip_name = 'data_user_csv_dump.zip'
        zip_path = os.path.join(settings.BASE_DIR, 'backup', 'user', zip_name)
        dump_names = _extract_to_csv(zip_name, zip_path)

    os.chdir(os.path.dirname(zip_path))
    with ZipFile(zip_name, 'w') as zip_dump:
        for item in dump_names:
            zip_dump.write(item)
    os.chdir(settings.BASE_DIR)

    try:
        with open(zip_path, 'rb') as dump:
            response = httpresp(dump.read(),
                                content_type=mimetypes.guess_type(zip_path)[0])
            response['Content-Disposition'] = 'filename={}'.format(zip_name)
            response['Set-Cookie'] = 'fileDownload=true; path=/'
            return response
    except Exception as ex:
        return httpresp('''Error: {} <br/>
                        Unable to serve file: {}
                        </br>Please notify James Kominick
                        </br>Close this tab'''\
                        .format(ex, zip_name))
Пример #4
0
def serve_attachment(request, file_type, file_id):
    ''' Serve attachment for viewing or download '''
    attch = {'drawing':DrawingAttachment, 'revision':RevisionAttachment,
             'comment':CommentAttachment, 'reply':ReplyAttachment}
    try:
        attachment = attch[file_type].objects.get(pk=file_id)
        filepath = attachment.upload.name
        filename = attachment.filename(filepath=filepath)
        full_path = os.path.join(settings.MEDIA_ROOT, filepath)
        with open(full_path, 'rb') as attch:
            response = httpresp(attch.read(), content_type=mimetypes.guess_type(full_path)[0])
            response['Content-Disposition'] = 'filename={}'.format(filename)
            return response

    except Exception as ex:
        return httpresp('''Error: {} <br/>
                        Unable to serve file:  file_id: {}
                        </br>Please notify James Kominick
                        </br>Close this tab'''\
                        .format(ex, file_id))
Пример #5
0
def get_word(request):
    """ get a random word for ajax requests """
    word = Word.random.all()
    data = {'word': word.text}
    return httpresp(json.dumps(data), content_type='application/json')