コード例 #1
0
    def handle_feedback(self, question, session_id):
        # a feedback has to follow this reg ex, examples:
        # valid: ':feedback 0 list', ':feedback 10 youtube', ':feedback 100 list'
        # not valid: ':feedback a', ':feedback 1000 list trailer'
        m = re.match(':feedback (\d{1,3}) (\w+)$', question)

        if m == None:
            return None, None

        percentage = m.group(1)
        expected_type = m.group(2)

        self.history.update_feedback(session_id, percentage, expected_type)

        controller_message = Message(
            session_id  = session_id,
            actor       = 'controller',
            attachments = [Attachment(content_type = 'text',
                            content = 'Thank you for the feedback')])

        user_message = Message(
            session_id = session_id,
            actor      = 'user',
            attachments = [Attachment(content_type = 'feedback',
                            content = {'percentage':   percentage,
                                       'expectedType': expected_type})])


        return user_message, controller_message
コード例 #2
0
ファイル: views.py プロジェクト: arattinger/tripassist
def form_builder(request, form, model, holiday_id, item_id, html, field):
    data = {'holiday_id': holiday_id}
    inst = None
    init_attachments = None
    if item_id:
        inst = model.objects.get(pk=item_id)
        data['title'] = inst.name
        data['item_id'] = item_id
        if inst.files.count() > 0:
            init_attachments = {"attachments": inst.files.all()}
    else:
        data['title'] = 'Add %s' % (field)
    if request.method == 'POST':
        form = form(request.POST, instance=inst)
        if form.is_valid():
            inst = form.save(inst)
            inst.save()
            if 'file' in request.FILES:
                fileitem = request.FILES['file']
                newFile = Attachment(attachment=fileitem, user=request.user)
                newFile.save()
                inst.files.add(newFile)
                inst.save()
            holiday = Holiday.objects.get(pk=holiday_id)
            getattr(holiday, field).add(inst)
            holiday.save()
            messages.success(request, 'Saved successfully!')
            return HttpResponseRedirect('/holiday/' + str(holiday_id))
        data['form'] = form
    else:
        data['form'] = form(instance=inst, initial=init_attachments)
    return render_to_response(html, data, RequestContext(request))
コード例 #3
0
ファイル: importing.py プロジェクト: thechild/mbox2sql
def handle_attachment(message, content, related=False):
#    r = ''
#    if related:
#        r = '(r)'

    filename, encoding = decode_header(content.get_filename())[0]
    if encoding:
        filename = filename.decode(encoding, errors='replace')

    #if not related:
    #    print "saving attachment [%s] of type %s from message %d %s" % (filename, content.get_content_type(), message.id, r)

    a = Attachment()
    a.filename = filename  # TODO need to parse weird strings from this
    if not a.filename:
        a.filename = str(uuid.uuid4())
    a.content_type = content.get_content_type()
    a.stored_location = os.path.join(files_dir, str(message.id), get_valid_filename(a.filename))
        # probably want to fix this too
    a.mime_related = related
        # load the file
    file_content = content.get_payload(decode=1)
    a.file_md5 = hashlib.md5(file_content).hexdigest()  # again, probably a better way to do this than all in memory
    # actually write it do disk - should wrap this in a try except too
    if not os.path.exists(os.path.join(files_dir, str(message.id))):
        os.makedirs(os.path.join(files_dir, str(message.id)))
    with open(a.stored_location, 'wb') as fp:
        fp.write(file_content)
    a.message = message
    a.save()
コード例 #4
0
ファイル: urls.py プロジェクト: yddgit/python-webapp-sample
def api_get_attachments():
    u'附件列表API'
    check_admin()
    total = Attachment.count_all()
    page = Page(total, _get_page_index())
    attachments = Attachment.find_by('order by created_at desc limit ?,?',
                                     page.offset, page.limit)
    return dict(attachments=attachments, page=page)
コード例 #5
0
 def setUp(self):
     self.file1 = Attachment(
         file_name="file1",
         description="A file",
         object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
     self.file2 = Attachment(
         file_name="file2",
         description="Another File",
         object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
     """
コード例 #6
0
    def setUp(self):

        ##self.file1 = Attachment(file_name="file1", description="A file")
        ##self.file2 = Attachment(file_name="file2", description="Another File")

        #self.revision1 = Revision(version="1", )
        ##self.revision2 =
        self.revision1 = Attachment(file_name="rev123")
        self.revision2 = Attachment(file_name="rev1234")
        """
コード例 #7
0
    def query(self, question, session_id, history_object):
        target, query, metadata = self.dbbase.get_query(question)
        results = mysqlhelper.execute_sql(self.cursor, query)
        print('target: %s' % target)
        print(metadata)


        attachment = Attachment('text', results[0]) if len(results) == 1 else\
                     Attachment('list', results)

        return attachment, (target, query, metadata)
コード例 #8
0
def add(request):
    if request.method == 'POST':
        form = AddViolation(request.POST)
        if form.is_valid():
            msg=_("Thank you for submitting a new report. To finalize your submission please confirm using your validation key.\nYour verification key is %s/%s%s\nPlease note that reports are moderated, it might take some time before your report appears online. Thank you for your patience.")
            actid=sendverifymail('activate?key=',form.cleaned_data['email'], msg)
            operator, created = Operator.objects.get_or_create(name=form.cleaned_data['operator'])
            v=Violation(
                country = form.cleaned_data['country'],
                operator_ref = operator,
                contract = form.cleaned_data['contract'],
                resource = form.cleaned_data['resource'],
                resource_name = form.cleaned_data['resource_name'],
                type = form.cleaned_data['type'],
                media = form.cleaned_data['media'],
                temporary = form.cleaned_data['temporary'],
                contractual = form.cleaned_data['contractual'],
                contract_excerpt = sanitizeHtml(form.cleaned_data['contract_excerpt']),
                loophole = form.cleaned_data['loophole'],
                activationid = actid
                )
            v.save()
            #c=Confirmation(key='', email=form.cleaned_data['email'], violation=v)
            #c.save()
            c = Comment(
                comment=form.cleaned_data['comment'],
                submitter_email=form.cleaned_data['email'],
                submitter_name=form.cleaned_data['nick'],
                consent=form.cleaned_data['consent'],
                timestamp=datetime.now(),
                violation=v,
                )
            c.save()
            for f in request.FILES.getlist('attachments[]'):
                a=Attachment(comment=c, name=f.name, type=f.content_type)
                m = hashlib.sha256()
                for chunk in f.chunks():
                    m.update(chunk)
                sname=m.hexdigest()
                a.storage.save(sname,f)
                a.save()

            messages.add_message(request, messages.INFO, _('Thank you for submitting this report, you will receive a verification email immediately, if not check your spam folder.'))
            return HttpResponseRedirect('/') # Redirect after POST
    else:
        form = AddViolation()

    v_list = Violation.objects.filter(activationid='',featuredcase__isnull=False).order_by('id').reverse()[:3]
    return render_to_response(
        'index.html',
        { 'form': form,
          'violations': v_list },
        context_instance=RequestContext(request))
コード例 #9
0
ファイル: views.py プロジェクト: rails-to-cosmos/force
def fetch_menu():
    # get provider, provider->get_fresh_menus
    menu_files = download_menu_files()

    for menu_file in menu_files:
        with open(menu_file, 'r') as mf:
            fs = Attachment(menufile=File(mf))
            fs.save()
            load_menu_from_file(menu_file, fs)

    # TODO verbose output
    return True
コード例 #10
0
ファイル: views.py プロジェクト: pombredanne/nnmon
def add(request):
    if request.method == 'POST':
        form = AddViolation(request.POST)
        if form.is_valid():
            msg=_("Thank you for submitting a new report. To finalize your submission please confirm using your validation key.\nYour verification key is %s/%s%s\nPlease note that reports are moderated, it might take some time before your report appears online. Thank you for your patience.")
            actid=sendverifymail('activate?key=',form.cleaned_data['email'], msg)
            operator, created = Operator.objects.get_or_create(name=form.cleaned_data['operator'])
            v=Violation(
                country = form.cleaned_data['country'],
                operator_ref = operator,
                contract = form.cleaned_data['contract'],
                resource = form.cleaned_data['resource'],
                resource_name = form.cleaned_data['resource_name'],
                type = form.cleaned_data['type'],
                media = form.cleaned_data['media'],
                temporary = form.cleaned_data['temporary'],
                contractual = form.cleaned_data['contractual'],
                contract_excerpt = sanitizeHtml(form.cleaned_data['contract_excerpt']),
                loophole = form.cleaned_data['loophole'],
                activationid = actid
                )
            v.save()
            #c=Confirmation(key='', email=form.cleaned_data['email'], violation=v)
            #c.save()
            c = Comment(
                comment=form.cleaned_data['comment'],
                submitter_email=form.cleaned_data['email'],
                submitter_name=form.cleaned_data['nick'],
                consent=form.cleaned_data['consent'],
                timestamp=datetime.now(),
                violation=v,
                )
            c.save()
            for f in request.FILES.getlist('attachments[]'):
                a=Attachment(comment=c, name=f.name, type=f.content_type)
                m = hashlib.sha256()
                for chunk in f.chunks():
                    m.update(chunk)
                sname=m.hexdigest()
                a.storage.save(sname,f)
                a.save()

            messages.add_message(request, messages.INFO, _('Thank you for submitting this report, you will receive a verification email immediately, if not check your spam folder.'))
            return HttpResponseRedirect('/') # Redirect after POST
    else:
        form = AddViolation()

    v_list = Violation.objects.filter(activationid='',featuredcase__isnull=False).order_by('id').reverse()[:3]
    return render_to_response(
        'index.html',
        { 'form': form,
          'violations': v_list },
        context_instance=RequestContext(request))
コード例 #11
0
ファイル: tests.py プロジェクト: ratuljain/Eventify
class EventifyAttachmentCreationTest(TestCase):
    attachment = None

    def setUp(self):
        self.attachment = Attachment(file="eventify_model.pdf")
        self.attachment.save()

    def test_attachment_url_creation(self):
        """
        Ensure we can retrieve list of venues.
        """
        # check if URL actually exists
        attach_url = self.attachment.attachment_url
        request = requests.get(attach_url)
        self.assertEqual(request.status_code, status.HTTP_200_OK)
コード例 #12
0
ファイル: tests.py プロジェクト: JosephRedfern/CM2301-9
class AttachmentTestCase(TestCase):
    """
    A test class for Attachment
    """

    def setUp(self):
            self.file1 = Attachment(file_name="file1", description="A file", object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
            self.file2 = Attachment(file_name="file2", description="Another File", object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
            """
            """
    def test_get_total_size(self):
        """
        The File Size is determined
        """
        self.assertEqual(self.file1.get_total_size(), 0)
        self.assertEqual(self.file2.get_total_size(), 0)
コード例 #13
0
 def do_delete(self):
  attachment = Attachment.gql('WHERE attachment_id = :1', self.request.get("attachment_id")).get()
  if attachment:
    attachment.delete()
    return True
  else:
    return False
コード例 #14
0
    def query(self, question, session_id, history_object):
        # get the most probable query
        target, query, metadata = self.dbbase.get_query(question)

        print('query')
        print(query)

        print('metadata')
        print(metadata)

        if isinstance(metadata, tuple):
            query_type = metadata[0]
            metadata = metadata[1]
        else:
            query_type = metadata
            metadata = None

        if query is None:
            print ("Query not generated :(\n")
            return None, (target, query, metadata)

        if target.startswith("?"):
            target = target[1:]

        # add limit to query
        query = "%s limit %d" % (query, 5)

        self.sparql.setQuery(query)
        self.sparql.setReturnFormat(JSON)
        results = self.sparql.query().convert()

        if not results["results"]["bindings"]:
            print ("No answer found :(")
            return None, (target, query, metadata)


        print('Target result')
        # Results filtered for the target of the query (works with just one target)
        target_results = []
        for result in results["results"]["bindings"]:
            tmp = result[target]["value"]
            target_results.append(tmp)

        attachment = Attachment('text', target_results[0]) if len(results) == 1 else\
                     Attachment('list', target_results)

        return attachment, (target, query, metadata)
コード例 #15
0
ファイル: store.py プロジェクト: rcarmo/yaki-gae
    def get_attachment(self, page, filename):
        """Return an attachment from the cloud store, storing it locally"""

        if not self.token:
            log.debug("No token")
            return None

        # get the folder contents
        metadata = self.get_metadata(page)
        if not metadata:
            return None

        target = None
        for i in metadata['contents']:
            if not i['is_dir']:
                if os.path.basename(i['path']) == filename:
                    target = i['path']
                    break

        if not target:
            return None

        get_url = _urls.files % (target, urllib.urlencode({"access_token": self.token}))
        log.debug(get_url)
        r = fetch(get_url)

        if r['status'] == 200:
            metadata = json.loads(r['x-dropbox-metadata'])

            # mtime is taken from cloud store metadata
            mtime = parse_dropbox_date(metadata['modified'])

            id   = os.path.join(page.lower(),filename.lower())
            params = {
                "id"       : id,
                "path"     : page,
                "filename" : filename,
                "mtime"    : mtime,
                "data"     : r['data'],
                "mime_type": metadata['mime_type'],
            }
            a = Attachment(**params)
            a.put()
            return a
        return None
コード例 #16
0
  def post(self):         
    """create attachment or update attachment for given attachment_id"""     
    
    logging.info("POST: _method:%s attachment_id: %s" % (self.request.get("_method"), self.request.get("attachment_id")))
     
    _method = self.request.headers.get(METHOD_OVERRIDE_HEADER, None)
    if _method == 'DELETE':
      self.handle_delete()        
    else:    
      self.do_delete()
      attachment = Attachment(attachment_id = self.request.get("attachment_id"),
                              content_type = self.request.get("content_type"),
                              data = self.request.get("data"))   
      attachment.put()

      # return xml
      path = os.path.join(os.path.dirname(__file__), 'templates','attachment.xml')
      self.response.out.write(template.render(path, {'attachment': attachment}))      
コード例 #17
0
ファイル: wiki.py プロジェクト: rcarmo/yaki-gae
    def get_attachment(path, filename):
        """Returns a single attachment"""
        log.debug("%s:%s" % (path, filename))

        result = Attachment.get_by_id(os.path.join(path,filename).lower())
        if not result:
            if store.is_attachment(path, filename):
                return store.get_attachment(path, filename)
            return None
コード例 #18
0
 def head(self, attachment_id, filename = None):    
   """allows to check if attachment exitsts?"""  
   
   attachment = Attachment.gql("WHERE attachment_id = :1", 
                               attachment_id).get()    
   if attachment:
     self.response.headers['Content-Type'] = attachment.content_type
     self.response.set_status(200)
   else:
     self.error(404)       
コード例 #19
0
ファイル: __init__.py プロジェクト: hfercc/wboard
def send_private_message(sender, receiver, message, attachments = ''):
	if isinstance(sender, int):
		sender = User.objects.get(id = sender)
	if isinstance(receiver, int):
		receiver = User.objects.get(id = receiver)
	pm = PrivateMessage(sender = sender, receiver = receiver, body_text = message)
	pm.save()
	if attachments:
		for s in attachments.split("|"):
			url, file_name = s.split("*")
			attachment = Attachment(url = url, file_name = file_name)
			attachment.save()
			pm.attachments.add(attachment)
	return pm
	
# def send_private_message_and_notify(sender, receiver, message, attachments = []):
	# pm = send_private_message(sender, receiver, message)
	# if pm.sender != pm.receiver:
		# notification.send_notification(pm.receiver, 'pm', private_message = pm)
	# return pm
コード例 #20
0
ファイル: attachments.py プロジェクト: larrykubin/showlists
async def sign(fileUpload: FileUpload,
               current_user: User = Depends(get_current_user)):
    # create attachment record with status incomplete
    # if upload success, callback from uppy
    try:
        attachment = Attachment()
        attachment.filename = fileUpload.filename
        attachment.creator_id = current_user.id
        db.add(attachment)
        db.commit()

        bucket_name = 'showlists'
        object_name = 'shows/{}'.format(attachment.id)
        response = s3_client.generate_presigned_post(bucket_name, object_name)
    except ClientError as e:
        print(e)
        return None

    # The response contains the presigned URL and required fields
    return response
コード例 #21
0
 def get(self):
   """get attachment by key"""
   if self.request.get("key", None):
     attachment = Attachment.get(self.request.get("key"))
     if attachment:
       self.response.headers['Content-Type'] = attachment.content_type
       self.response.set_status(200)
     else:
       self.error(404)
   else:
     self.response.out.write("Paperclip storage engine")   
コード例 #22
0
    def query(self, question, session_id):

        # try to handle a feedback, it may return the user message with
        # content_type 'feedback' and a thankful message from the controller
        user_message, controller_message = self.handle_feedback(question, session_id)
        if user_message != None and controller_message != None:
            self.history.keep(user_message)
            self.history.keep(controller_message)
            return controller_message, None


        message_attachment = Attachment(content_type = 'text',
                                        content      = question)
        message_question = Message(session_id  = session_id,
                                   actor       = 'user',
                                   attachments = [message_attachment])

        self.history.keep(message_question)

        # TODO: routing
        message_answer = self.bot.query(message_question, session_id)

        self.history.keep(message_answer)


        optional = None
        # if the bot cannot answer anymore, the flag terminated is set True,
        # the service will ask for a feedback
        if message_answer.terminated:
            optional = Message(
                session_id  = session_id,
                actor       = 'controller',
                attachments = [Attachment(content_type = 'feedback',
                    content = 'Did you get what you expected? ' + \
                              'Please write something like<br> ' + \
                              '<i>:feedback &lt;percentage&gt; ' + \
                              '&lt;list|trailer|other expected type&gt;</i><br>' +\
                              'Example: <i>:feedback 100 list</i>')])
            self.history.keep(optional)

        return message_answer, optional
コード例 #23
0
 def get(self, attachment_id, filename = None): 
   '''
   get attachment by nice url
   /attachment_id/[filename.ext]
   '''                                  
   attachment = Attachment.gql("WHERE attachment_id = :1", 
                               attachment_id).get()    
   if attachment:
     self.response.headers['Content-Type'] = attachment.content_type
     self.response.out.write( attachment.data ) 
   else:
     self.error(404)   
コード例 #24
0
ファイル: forms.py プロジェクト: rangermeier/subkoord
def attachment_formset_handler(request, object):
    if request.method == "POST" and request.POST.has_key("attachments-TOTAL_FORMS"):
        attachment_formset = AttachmentFormSet(request.POST, prefix = "attachments" )
        if attachment_formset.is_valid():
            for form in attachment_formset:
                if form.cleaned_data.has_key("id") and form.cleaned_data["id"]:
                    if not form.cleaned_data["file"] or form.cleaned_data["DELETE"] == True:
                        form.cleaned_data["id"].delete()
                    else:
                        form.save()
                elif form.cleaned_data.has_key("file") and form.cleaned_data["file"]:
                    if not object.id:
                        object.save()
                    attachment = Attachment(file = form.cleaned_data["file"],
                        content_type = ContentType.objects.get_for_model(object),
                        object_id = object.id, )
                    attachment.save()
    else:
        attachment_formset = AttachmentFormSet(queryset = object.attachments.all(),
            prefix = "attachments" )
    return attachment_formset
コード例 #25
0
ファイル: diary.py プロジェクト: Mononofu/infinite-diary
 def get(self):
   attachments = ""
   for a in Attachment.all():
     attachments += attachmentTemplate.render({
           'name': a.name,
           'thumbnail': a.thumbnail,
           'key': a.key()
     })
   self.response.out.write(indexTemplate.render({
       'title': 'Attachments',
       'body': attachments,
       'active_page': 'attachments'
   }))
コード例 #26
0
ファイル: urls.py プロジェクト: yddgit/python-webapp-sample
def api_delete_attachment(attachment_id):
    u'删除附件API'
    check_admin()
    attachment = Attachment.get(attachment_id)
    if attachment is None:
        raise APIResourceNotFoundError('Attachment')
    # 删除本地文件
    local_path = get_local_file_path(attachment.local_name)
    if os.path.exists(local_path):
        os.remove(local_path)
    # 删除数据库记录
    attachment.delete()
    return dict(id=attachment_id)
コード例 #27
0
class AttachmentTestCase(TestCase):
    """
    A test class for Attachment
    """
    def setUp(self):
        self.file1 = Attachment(
            file_name="file1",
            description="A file",
            object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
        self.file2 = Attachment(
            file_name="file2",
            description="Another File",
            object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
        """
            """

    def test_get_total_size(self):
        """
        The File Size is determined
        """
        self.assertEqual(self.file1.get_total_size(), 0)
        self.assertEqual(self.file2.get_total_size(), 0)
コード例 #28
0
ファイル: views.py プロジェクト: aisk/Cuneiform
def create_post():

    required_fields = ['title', 'content']
    post_data = {x:request.form[x] for x in required_fields}
    post = Post()
    post.set(post_data)

    upload_image = request.files['featured_image']
    if upload_image.filename != '' and allowed_file(upload_image.filename):
        f = Attachment(upload_image.filename, data=upload_image.stream)
        post.set('featured_image', f)

    post.save()

    return redirect(url_for('show_post', post_id=post.id))
コード例 #29
0
    def parse_response(bindings, select_columns):
        attachment_type = 'text' if len(bindings) == 1 else 'list'
        results = []
        # for every row
        for binding in bindings:
            line = []
            print('binding')
            print(binding)
            # for every column
            for k,v in binding.iteritems():
                if k in select_columns:
                    line.append(v['value'])
                    
            results.append(' '.join(line))

        return Attachment(attachment_type, results)
コード例 #30
0
ファイル: urls.py プロジェクト: yddgit/python-webapp-sample
def attachment(attachment_id):
    u'下载附件'
    attachment = Attachment.get(attachment_id)
    if attachment is None:
        raise notfound()
    local_path = get_local_file_path(attachment.local_name)
    if not os.path.exists(local_path):
        raise notfound()
    if not os.path.isfile(local_path):
        raise notfound()
    file_name_encode = quote(attachment.file_name.encode('utf-8'))
    ctx.response.set_header('Content-Length', os.path.getsize(local_path))
    ctx.response.set_header('Content-Type', attachment.file_type)
    ctx.response.set_header(
        'Content-Disposition',
        'attachment;filename="%s";filename*=UTF-8\'\'%s' %
        (file_name_encode, file_name_encode))
    return static_file_generator(local_path)
コード例 #31
0
ファイル: app.py プロジェクト: yanjingwei/Cuneiform
def new_post():
    author = User.get_current()
    title, content = request.form['title'], request.form['content']
    tag_names = parse_tag_names(request.form['tags'])

    f = request.files['featuredImage']
    if f.filename == '':
        featuredImage = None
    else:
        featuredImage = Attachment(f.filename, data=f.stream)
    if featuredImage and not allowed_file(featuredImage.extension):
        flash('warning', 'Upload a proper image.')
        return redirect(url_for('post_form'))

    post = Post()
    post.title = title
    post.content = content
    post.markedContent = markdown(content)
    post.author = author
    if featuredImage:
        post.featuredImage = featuredImage

    acl = ACL()
    acl.set_public_read_access(True)
    acl.set_write_access(author.id, True)
    post.set_acl(acl)

    post.save()

    tags = []
    for name in tag_names:
        tag = Tag.get_by_name(name)
        if not tag:
            tag = Tag()
            tag.name = name
        tags.append(tag)
    for tag in tags:
        m = TagPostMap()
        m.set({'tag': tag, 'post': post})
        m.save()
        tag.increment('post_count')
    Tag.save_all(tags)

    return redirect(url_for('show_post', post_id=post.id))
コード例 #32
0
ファイル: api.py プロジェクト: Red54/Sophia
def upload_file():
    file = request.files['file']
    if file and allowed_file(file.filename):
        ext_name = file.filename.rsplit('.', 1)[1]
        filename = '{0:s}.{1:s}'.format(uuid.uuid1(), ext_name)
        file.save(os.path.join(UPLOAD_FOLDER, filename))
        size = os.path.getsize(os.path.join(UPLOAD_FOLDER, filename))
        attachment = Attachment()
        attachment.path = filename
        attachment.filename = file.filename
        attachment.size = size
        attachment.created_at = int(time.time())
        attachment.user_id = current_user.id
        attachment.ext_name = ext_name
        attachment.topic_id = 0
        db.session.add(attachment)
        db.session.commit()
        obj = {
            'id': attachment.id,
            'url': url_for('uploaded_file', filename=filename)
        }
        return json.dumps(obj)
    return ''
コード例 #33
0
def add_attachment(session, attachment, note, user):
    """Save and track attachment in the database.

    Parameters

    attachment -- A file retrieved from a flask request
        - See https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
    """

    check_permission(session, PermissionType.EDIT, user, note)

    # set file name
    name, ext = os.path.splitext(attachment.filename)

    # determine filename
    num = 1
    file_name = secure_filename(f"{user.name}_{name}_{num}{ext}")
    file_name = os.path.join(UPLOAD_FOLDER, file_name)
    while os.path.isfile(file_name):
        num += 1
        file_name = secure_filename(f"{user.name}_{name}_{num}{ext}")
        file_name = os.path.join(UPLOAD_FOLDER, file_name)

    # create model to track attachment
    attachment_model = Attachment(
        display_name=attachment.filename,
        file_name=file_name,
        note_id=note.id,
        owner_id=user.id,
    )
    session.add(attachment_model)

    # Flush to db to make sure everything is good
    session.flush()

    # Save file
    attachment.save(file_name)

    return attachment_model
コード例 #34
0
ファイル: mail.py プロジェクト: Mononofu/infinite-diary
  def handle_entry(self, message):

    entry = Entry(author='Julian')
    raw, entry.content = self.get_content(message)

    if entry.content is None:
      logging.error("Failed to find message body")
      logging.error(message)
      return

    matches = re.search("diaryentry(\d+)", raw)
    if matches is None:
      logging.error("received mail that wasn't a diary entry")
      logging.error(raw)
      return

    entry.date = datetime.date.fromtimestamp(int(matches.group(1)))
    entry.put()

    num_attachments = 0

    # fall back to raw mail message for attachment parsing
    for part in message.original.walk():
      content_type = part.get_content_type()
      if content_type not in ["text/plain", "text/html", "multipart/mixed",
                              "multipart/alternative"]:
        attachment = Attachment(name=part.get_param("name"),
                                content_type=content_type)

        # store attachment in blobstore
        bucket = '/infinite-diary.appspot.com'
        filename = os.path.join(bucket, 'attachments',
                                time.strftime('%Y-%m-%d_%H-%M'),
                                str(num_attachments))

        with gcs.open(filename, 'w') as f:
          f.write(base64.b64decode(part.get_payload()))

        attachment.content = blobstore.create_gs_key('/gs' + filename)
        attachment.entry = entry.key()
        attachment.thumbnail = images.get_serving_url(attachment.content,
                                                      size=400)

        attachment.put()
        num_attachments += 1
コード例 #35
0
def add_attachment():

    api_key = request.headers.get('Authorization')
    response = UserClient.get_user(api_key)
    if not response:
        return make_response(jsonify({'message': 'Not logged in'}), 401)

    user = response['result']
    if int(user['id']) != int(request.form['contractor_user_id']):
        return make_response(
            jsonify({'message': 'user can not add an attachment'}), 401)
    attachment_instance = Attachment()
    attachment_instance.name = request.form['title']
    attachment_instance.filename = request.form['filename']
    attachment_instance.contract_id = request.form['contract_id']
    attachment_instance.description = request.form['description']

    db.session.add(attachment_instance)
    db.session.commit()

    response = jsonify({'message': 'success'})

    return response
コード例 #36
0
ファイル: views.py プロジェクト: nyddle/Discourse
def attachments(request, path):
    """
    On delete: delete the file at the path.
    On post: post a new file at the path, unless the 
    On delete with path, delete the file.
    On post with path, replace the file.
    On post without path, create a new file.
    On get with path, download the file.
    On get without path, return a list of files in the thread.
    TODO: On head with path, return info about the file.
    """
    if request.method == 'DELETE' or request.POST.get('method') == 'delete':
        for path in get_files(request, path):
            attachment = Attachment.objects.get(path=path)
            for reciever, response in attachment_manipulate.send(sender=attachment, request=request, action='delete'):
                if isinstance(response, HttpResponse):
                    return response
            attachment.delete()
        return HttpResponse(json.dumps(True), content_type="application/json")
    elif request.POST.get('method') == 'hide':
        for path in get_files(request, path):
            attachment = Attachment.objects.get(path=path)
            for reciever, response in attachment_manipulate.send(sender=attachment, request=request, action='hide'):
                if isinstance(response, HttpResponse):
                    return response
            attachment.hidden = True
            attachment.save()
        return HttpResponse(json.dumps(True), content_type="application/json")
    elif request.POST.get('method') == 'show':
        for path in get_files(request, path):
            attachment = Attachment.objects.get(path=path)
            for reciever, response in attachment_manipulate.send(sender=attachment, request=request, action='show'):
                if isinstance(response, HttpResponse):
                    return response
            attachment.hidden = False
            attachment.save()
        return HttpResponse(json.dumps(True), content_type="application/json")
    elif request.POST.get('method') == 'zip':
        attachments = []
        for path in get_files(request, path):
            attachment = Attachment.objects.get(path=path)
            for reciever, response in attachment_view.send(sender=attachment, request=request):
                if isinstance(response, HttpResponse):
                    return response
            attachments.append(attachment)
        zip = AttachmentZip.create(attachments)
        response = HttpResponse(json.dumps(zip.info()), content_type="application/json", status=202)
        response['Location'] = zip.url
        return response
    elif request.method == 'POST':
        file = request.FILES['file']
        path = posixpath.join(path, file._name)
        try:
            attachment = Attachment.objects.get(path=path)
        except:
            attachment = Attachment(path=path)
        attachment.file = file
        attachment.mimetype = file.content_type
        attachment.author = request.user
        for reciever, response in attachment_manipulate.send(sender=attachment, request=request, action='create'):
            if isinstance(response, HttpResponse):
                return response
        attachment.save()
        return HttpResponse(json.dumps(attachment.info()), content_type="application/json")
    elif path:
        attachment = get_object_or_404(Attachment, path=path)
        for reciever, response in attachment_view.send(sender=attachment, request=request):
            if isinstance(response, HttpResponse):
                return response
        if not attachment.file.storage.exists(attachment.file.path):
            raise Http404
        response = HttpResponse(FileWrapper(attachment.file), content_type=attachment.mimetype)
        response['Content-Length'] = attachment.file.storage.size(attachment.file.path)
        response['Content-Disposition'] = "attachment; filename=%s" % attachment.file.name
        return response
コード例 #37
0
ファイル: tests.py プロジェクト: ratuljain/Eventify
 def setUp(self):
     self.attachment = Attachment(file="eventify_model.pdf")
     self.attachment.save()
コード例 #38
0
ファイル: tests.py プロジェクト: JosephRedfern/CM2301-9
 def setUp(self):
         self.file1 = Attachment(file_name="file1", description="A file", object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
         self.file2 = Attachment(file_name="file2", description="Another File", object_id="d636abe0-b293-11e2-9e96-0800200c9a66")
         """
コード例 #39
0
# coding: utf-8
from models import Attachment
from SPARQLWrapper import SPARQLWrapper, JSON
from sparql_generator import generate_sparql_from_context
from rasa_extractor import RasaExtractor
from context import Context, Entity, LiteralObject, WildcardPredicate
from models import Attachment, Message, Extra
import graph_metadata as gm
import cPickle as pickle
import nltk

idk = Attachment('text', 'I do not know how to answer')
no_answer = Attachment('text', '')


class ContextHelper(object):

    def __init__(self, sparql_endpoint, rasa_endpoint, history):
        self.sparql = SPARQLWrapper(sparql_endpoint)
        self.rasa = RasaExtractor(rasa_endpoint, history)
        self.history = history

    def query(self, question, session_id):

        # get the context for the question
        context, modifier = self.context_for_question(question, session_id)


        message_answer = Message(session_id  = session_id,
                                 actor       = 'bot',
                                 terminated  = False)
コード例 #40
0
ファイル: views.py プロジェクト: rogeriofalcone/anaf
def ajax_upload(request, object_id=None, record=None):
    try:
        object = None
        if request.method == "POST":
            if request.is_ajax():
                # the file is stored raw in the request
                upload = request
                is_raw = True
                # AJAX Upload will pass the filename in the querystring if it
                # is the "advanced" ajax upload
                try:
                    filename = request.GET['qqfile']
                    content_type = "application/octet-stream"
                except KeyError:
                    return HttpResponseBadRequest("AJAX request not valid")
            # not an ajax upload, so it was the "basic" iframe version with
            # submission via form
            else:
                is_raw = False
                if len(request.FILES) == 1:
                    # FILES is a dictionary in Django but Ajax Upload gives the uploaded file an
                    # ID based on a random number, so it cannot be guessed here in the code.
                    # Rather than editing Ajax Upload to pass the ID in the querystring,
                    # observer that each upload is a separate request,
                    # so FILES should only have one entry.
                    # Thus, we can just grab the first (and only) value in the
                    # dict.
                    upload = request.FILES.values()[0]
                    content_type = upload.content_type
                else:
                    raise Http404("Bad Upload")
                filename = upload.name

            random.seed()
            filehash = str(random.getrandbits(128))

            savefile = join(
                getattr(settings, 'MEDIA_ROOT'), 'attachments', filehash)

            # save the file
            success = save_upload(upload, savefile, is_raw)

            attachment = Attachment(filename=filename,
                                    content_type=content_type,
                                    uploaded_by=request.user.profile,
                                    attached_file=filehash)

            if record:
                attachment.attached_record = record
                about = record.about.all()
                if about.count():
                    attachment.attached_object = about[0]
                    object = attachment.attached_object
            else:
                object = Object.objects.get(id=object_id)
                attachment.attached_object = object

            attachment.save()

            if object:
                object.set_last_updated()

            # TODO: smart markup and return as string, and object id, different
            # classnames,id or attribute for update records and objects

            if success:
                ret_json = {'success': success,
                            'object_id': object.id if object else None,
                            'update_id': record.id if record else None}

            else:
                ret_json = {'success': False,
                            'object_id': None,
                            'update_id': None}

            return HttpResponse(json.dumps(ret_json))
    except:
        pass
コード例 #41
0
ファイル: diary.py プロジェクト: Mononofu/infinite-diary
 def get(self, key):
   a = Attachment.get(key)
   self.send_blob(a.content, content_type=a.content_type)
コード例 #42
0
    def query(self, message_question, session_id):

        message_answer = Message(session_id=session_id,
                                 actor='bot',
                                 terminated=False)

        extra = Extra(creator='command')  # command: starts with ':'

        # TODO: analyse the content_type first!
        question = message_question.attachments[0].content

        attachments = None
        if question == ':hello':
            attachments = [
                Attachment('question', 'Hi, how can I help you?'),
                Attachment('text', 'Type <b>:help</b> for a list of commands')
            ]
            extra.query = ':hello'

        elif question == ':help':
            attachments = [Attachment('text', 'You asked for help. I think I can ' \
                                    + 'help you: https://google.com'),
                           Attachment('list', [':hello',
                                               ':help',
                                               ':youtube',
                                               ':list',
                                               ':histogram',
                                               ':pie'])]
            extra.query = ':help'

        elif question == ':youtube':
            attachments = [
                Attachment('text', 'Here is the trailer of Inception (2010)'),
                Attachment('youtube',
                           'https://www.youtube.com/embed/YoHD9XEInc0')
            ]
            extra.query = ':youtube'

        elif question == ':list':
            attachments = [
                Attachment('text', 'Here is your list'),
                Attachment('list', [1, 2, 3]),
                Attachment('question', 'Do you need something else?')
            ]
            extra.query = ':list'

        elif question == ':histogram':
            attachments = [
                Attachment('text', 'Here are some statistics'),
                Attachment(
                    'histogram', {
                        'title':
                        '# votes',
                        'labels': [
                            "Fast and Furios", "Casablanca", "Gran Torino",
                            "Game of Thrones"
                        ],
                        'values': [12, 19, 3, 5]
                    }),
                Attachment('question', 'Did you find this useful?')
            ]
            extra.query = ':histogram'

        elif question == ':pie':
            attachments = [
                Attachment(
                    'text',
                    'Here is the appreciationrate in stars for Casablanca'),
                Attachment(
                    'pie', {
                        'labels':
                        ["1 Star", "2 Stars", "3 Stars", "4 Stars", "5 Stars"],
                        'values': [205, 1313, 5333, 12023, 33333]
                    }),
                Attachment('question', 'Did you find this useful?')
            ]
            extra.query = ':pie'

        else:
            return None

        message_answer.attachments = attachments
        message_answer.extra = extra

        return message_answer
コード例 #43
0
ファイル: urls.py プロジェクト: yddgit/python-webapp-sample
def api_create_attachment():
    u'创建附件API'
    check_admin()

    # 获取上传的文件
    i = ctx.request.input(attachment_file={})
    f = i.attachment_file
    if not isinstance(f, MultipartFile):
        raise APIValueError('attachment_file',
                            'attachment_file must be a file.')

    # 文件名
    file_name = f.filename
    if not file_name:
        raise APIValueError('file_name', 'file_name cannot be empty.')
    file_name = re.split(r'[/\\]', file_name)[-1]  # 获取不包含路径的文件名

    # 扩展名
    fext = os.path.splitext(file_name)[1]
    if not fext[1:] in _UPLOAD_ALLOW_FILE_TYPE:
        raise APIValueError('file_type',
                            '*%s file is not allowed to upload.' % fext)

    # Content-Type
    file_type = mimetypes.types_map.get(fext.lower(),
                                        'application/octet-stream')
    if not file_type:
        raise APIValueError('file_type', 'file_type cannot be empty.')

    # 文件内容
    file_data = f.file
    if not file_data:
        raise APIValueError('file_data', 'file_data cannot be empty.')

    # 生成本地保存的文件名
    local_name = next_id()

    # 保存上传的文件(若目录不存在则自动创建)
    local_path = get_local_file_path(local_name)
    if not os.path.exists(os.path.dirname(local_path)):
        # 支持递归创建多级目录
        os.makedirs(os.path.dirname(local_path))
    BLOCK_SIZE = 8192  # Buffer Size
    with open(local_path, 'wb') as upload_file:
        file_size = 0
        while True:
            data = file_data.read(BLOCK_SIZE)
            if not data:
                break
            file_size = file_size + len(data)
            if file_size > _UPLOAD_MAX_SIZE:
                break
            upload_file.write(data)
    # 文件过大不能上传
    if file_size > _UPLOAD_MAX_SIZE:
        os.remove(local_path)
        raise APIError('file too big to upload.')

    # 保存数据库记录
    local_file_size = os.path.getsize(local_path)
    user = ctx.request.user
    attachment = Attachment(user_id=user.id,
                            local_name=local_name,
                            file_name=file_name,
                            file_type=file_type,
                            file_size=local_file_size)
    attachment.insert()
    return attachment