コード例 #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
 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")
     """
コード例 #3
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")
        """
コード例 #4
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)
コード例 #5
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)
コード例 #6
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
コード例 #7
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))
コード例 #8
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))
コード例 #9
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)
コード例 #10
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))
コード例 #11
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
コード例 #12
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
コード例 #13
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
コード例 #14
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
コード例 #15
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)
コード例 #16
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
コード例 #17
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
コード例 #18
0
ファイル: tests.py プロジェクト: ratuljain/Eventify
 def setUp(self):
     self.attachment = Attachment(file="eventify_model.pdf")
     self.attachment.save()