Beispiel #1
0
def save_article(request, page_name):
    continent_list = dropdown()

    page_content = request.POST['Content']
    continent_content = request.POST['continent_rad']

    try:
        article = Article.objects.get(article_title=page_name)

    except Article.DoesNotExist:
        author = request.user
        authorname = author.username
        article = Article(article_title=page_name,
                          article_continent=continent_content,
                          article_author=authorname)
        article.save()
    content_author = request.user
    content_author_name = content_author.username
    content = Content(content_content=page_content,
                      content_article=article,
                      content_change_date=datetime.now(),
                      content_author=content_author_name)
    content.save()

    article.article_last_revision = content
    article.save()

    return HttpResponseRedirect("/wikijourney/" + page_name + "/")
Beispiel #2
0
 def test_content_cannot_save_without_subclass_app_model(self):
     c = Content()
     try:
         c.save()
         self.fail("Base content saved without app_model provided by sub-class.")
     except:
         pass
Beispiel #3
0
def download_all_images(content: Content) -> Content:
    content.reset_downloaded_images_list()
    state.delete_images_directory()

    print('> [Image Robot] Starting downloading images...')

    IMAGES_DIRECTORY = os.path.join('content', 'images')
    create_new_directory(IMAGES_DIRECTORY, 'Image')

    ORIGINAL_IMAGES_DIRECTORY = os.path.join('content', 'images', 'originals')
    create_new_directory(ORIGINAL_IMAGES_DIRECTORY, 'Image')

    for sentence_index, sentence in enumerate(content.sentences):
        images = sentence.images

        for image_index, image in enumerate(images):
            try:
                if image in content.downloaded_images:
                    raise Exception('Image already downloaded.')

                if download_and_save(image, f'{sentence_index}-original.png'):
                    content.add_downloaded_image(image)
                    print(
                        f'> [Image Robot] [S{sentence_index}][I{image_index}] Image successfully downloaded: {image}'
                    )
                    break

            except Exception as e:
                print(
                    f'> [Image Robot] [S{sentence_index}][I{image_index}] Error ({image}): {e}'
                )

    print('> [Image Robot] Images successfully downloaded.')

    return content
Beispiel #4
0
def save_article(request, page_name):
	continent_list = dropdown()

	page_content = request.POST['Content']
	continent_content = request.POST['continent_rad']

	try:
		article = Article.objects.get(article_title=page_name)

	except Article.DoesNotExist:
		author = request.user
		authorname = author.username
		article = Article(article_title = page_name, 
						  article_continent = continent_content, 
						  article_author= authorname)
		article.save()
	content_author = request.user
	content_author_name=content_author.username
	content = Content(content_content= page_content, 
					  content_article= article,
					  content_change_date= datetime.now(), 
					  content_author=content_author_name)
	content.save()

	article.article_last_revision = content
	article.save()

	return HttpResponseRedirect("/wikijourney/" + page_name + "/")
Beispiel #5
0
def create_do(request):
    user, email, usernum = request.user.username.split("|")[:]
    if redirect_validation(request, request.POST['type'], "type"):
        return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['project'], "project"):
        return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['version'], "version"):
        return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['comment'], "comment"):
        return redirect_post_review(request.POST)

    request.session['err'] = ''
    s = Content(
        type=request.POST['type'],
        project=request.POST['project'],
        env="",
        run_id="",
        comment=request.POST['comment'],
        version=request.POST['version'],
        status=1,
        create_time=int(time.time()),
        deploy_time=0,
        finish_time=0,
        create_user=user,
        deploy_user="",
    )
    s.save()

    initialization_release_status(request.POST['project'])

    return HttpResponseRedirect("/task/list/1/")
Beispiel #6
0
def create_do(request):
    user, email, usernum = request.user.username.split("|")[:]
    if redirect_validation(request, request.POST['type'], "type"): return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['project'], "project"): return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['version'], "version"): return redirect_post_review(request.POST)
    if redirect_validation(request, request.POST['comment'], "comment"): return redirect_post_review(request.POST)

    request.session['err'] = ''
    s = Content(
            type=request.POST['type'],
            project=request.POST['project'],
            env="",
            run_id="",
            comment=request.POST['comment'],
            version=request.POST['version'],
            status = 1,
            create_time = int(time.time()),
            deploy_time = 0,
            finish_time = 0,
            create_user = user,
            deploy_user = "",
    )
    s.save()

    initialization_release_status(request.POST['project'])

    return HttpResponseRedirect("/task/list/1/")
Beispiel #7
0
def upload(request):
    """
    Renders the upload form page.

    Args:
        request: the request object

    Returns:
        response: a http response object
    """
    if request.method == 'POST':  # If the form has been submitted...
        form = UploadForm(request.POST,
                          request.FILES)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            for filefield, tmpname in handle_uploaded_file(request):
                c = Content()
                originalname = str(request.FILES["file"])
                c.user = request.user  # Only authenticated users can use this view
                c.set_file(originalname,
                           tmpname)  # Save uploaded file to filesystem
                c.get_type_instance()  # Create thumbnail if it is supported
                c.save()
                Uploadinfo.create(c, request)
                # uli.set_request_data(request)
                # uli.save()
            return HttpResponseRedirect(reverse('content:edit', args=[c.uid]))
    else:
        form = UploadForm(initial={})  # An unbound form
    return _render_to_response(request, 'content_upload.html', {
        'uploadform': form,
    })
Beispiel #8
0
def parse_post_req_content(insert=False):
    try:
        json_content = request.get_json()
        content = Content(json_content)
        content.insert = insert
        return content
    except:
        err = traceback.format_exc()
        logger.error(err)
        raise InvalidUsage(
            'Could not parse request! Please check ids and request format.')
Beispiel #9
0
def edit_content(type_, **kwargs):
    data = kwargs.get("data")

    content = Content.get_content_by_type(type_)

    if content is None:
        return None

    if data is not None:
        content.data = data

    Content.make_commit()
    return content
Beispiel #10
0
 def post(self, request, format=None):
     # todo
     data = request.data
     serializer = ContentPostSerializer(data=data)
     if not serializer.is_valid():
         err = {}
         err['msg'] = serializer.errors
         return Response(err)
     obj = Content()
     obj.entry = data['entry']
     obj.save()
     con = ContentSerializer(obj)
     return Response(con.data)
Beispiel #11
0
def break_content_into_sentences(content: Content) -> Content:
    print('> [Text Robot] Breaking content into sentences...')

    text = content.sanitized_source_content
    tokenizer = nltk.tokenize.PunktSentenceTokenizer()
    sentences = tokenizer.tokenize(text)[:content.number_of_sentences]

    for sentence in sentences:
        content.add_sentence(Sentence(sentence))

    print(
        f'> [Text Robot] Content broke into sentences. Number of sentences: {content.number_of_sentences}.'
    )
    return content
Beispiel #12
0
def create(request):
    author_email = request.POST["author_email"]
    text = request.POST["text"]
    subject = request.POST.get("subject")
    status = request.POST.get('status')

    author = Author.query(Author.email == author_email).get()
    if (author is None):
        return HttpResponseServerError("Author %s not found" % author_id)

    content = Content(author=author.key, text=text, subject=subject,
            status="draft")
    content.put()
    return HttpResponse(json_fixed.dumps(content))
Beispiel #13
0
def html5upload(request):
    """
    Renders the upload form page.
    """
    if request.method == 'POST': # If the form has been submitted...
        result = []
        for filefield, tmpname in handle_uploaded_file(request):
            c = Content()
            originalname = str(request.FILES["file"])
            c.user = request.user # Only authenticated users can use this view
            c.set_file(originalname, tmpname) # Save uploaded file to filesystem
            c.get_type_instance() # Create thumbnail if it is supported
            c.save()
            #print originalname
            #generating json response array
            result.append({"name": originalname,
                           "size": c.filesize,
                           "url": c.uid,
                           "thumbnail_url": '/content/instance/%s-200x200.jpg' % c.uid,
                           "delete_url": '/content/delete/%s' % c.uid,
                           "delete_type":"POST",})
        #print result
        response_data = json.dumps(result)
        #print response_data
        #checking for json data type
        #big thanks to Guy Shapiro
        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'
        return HttpResponse(response_data, mimetype=mimetype)
    return _render_to_response(request, 'content_html5upload.html', {
                               })
Beispiel #14
0
 def testCreate (self):
     content1 = Content (1, 'Title1')
     content1.append_piece(Piece('piece text'))
     
     content2 = Content(2, 'Title2')
     content2.append_piece(Piece('piece text'))
     content2.append_piece(Piece(content1))
Beispiel #15
0
def api_upload(request):
    """
    Renders the upload form page.

    Args:
        request: the request object

    Returns:
        response: a http response object
    """
    if request.method == 'POST':  # If the form has been submitted...
        # for header in request.META.keys():
        #    if header.startswith('HTTP'):
        #        print header, request.META[header]
        # print request.raw_post_data[:1000]
        if request.user.is_authenticated() is False:
            return HttpResponse(status=401)
        form = UploadForm(request.POST,
                          request.FILES)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            for filefield, tmpname in handle_uploaded_file(request):
                SUPPORTED_FIELDS = ['title', 'caption', 'author']
                kwargs = {}
                for field in SUPPORTED_FIELDS:
                    kwargs[field] = request.POST.get(field)
                try:
                    kwargs['point'] = Point(float(request.POST.get('lon')),
                                            float(request.POST.get('lat')))
                except:
                    # raise
                    pass
                print kwargs
                c = Content(**kwargs)
                originalname = str(request.FILES["file"])
                # Only authenticated users can use this view
                c.user = request.user
                # Save uploaded file to filesystem
                c.set_file(originalname, tmpname)
                # Create thumbnail if it is supported
                c.get_type_instance()
                c.save()
                Uploadinfo.create(c, request)
                break  # We save only the first file
            response = HttpResponse(status=201)
            # response.status_code = 201
            # FIXME: use reverse()
            response['Location'] = '/content/api/v1/content/%s/' % c.uid
            return response
            # return HttpResponseRedirect(reverse('content:edit', args=[c.uid]))
        else:
            response = HttpResponse(status=204)
            return response
    else:
        raise Http404
Beispiel #16
0
 def put(self, request, id, format=None):
     # todo
     data = request.data
     serializer = ContentPostSerializer(data=data)
     if not serializer.is_valid():
         err = {}
         err['msg'] = serializer.errors
         return Response(err)
     obj = Content.objects.get(id=id)
     if obj:
         obj1 = Content()
         obj1.entry = data['entry']
         obj1.save()
         con = ContentSerializer(obj1)
         return Response(con.data)
Beispiel #17
0
 def test_post_req_content_res(self):
     with open('tests/services/test_req1.json') as json_data:
         req = json.load(json_data)
         json_data.close()
         res = services.classify_content(Content(req))
         self.assertEqual(len(res.result_list), 4,
                          'Missing response result')
Beispiel #18
0
 def post(self):
     data = self.get_arguments()
     try:
         type = int(self.get_argument('type'))
         flag = self.get_argument('flag')
         slug = self.get_argument('slug')
         spec = {'type': type, 'slug': slug}
         cid = Content.one(spec)['_id']
         uid = self.current_user['_id']
         if int(flag) in [x for x,_y in MY_FLAGS]:
             has_vote = Vote.one({'uid': uid ,'cid': cid})
             if not has_vote:
                 Content.collection.update(
                     spec, {'$inc': { "votes." + flag: 1}}
                 )
                 vote = Vote()
                 vote['uid'],  vote['cid'], vote['vote'] = uid, cid, int(flag)
                 vote.save()
                 return self.json_response("You have voted has been noted", "OK")
             else:
                 Content.collection.update(
                     spec, {'$inc': { "votes." + flag: -1}}
                 )
                 Vote.remove({'uid': uid, 'cid': cid})
                 return self.json_response("Your vote has been removed", "OK")
         raise Exception("Invalid vote")
     except Exception, e:
         raise
         return self.json_response(e.__str__(), "ERROR", data)
Beispiel #19
0
 def post(self):
     data = self.get_arguments()
     try:
         type = int(self.get_argument('type'))
         flag = self.get_argument('flag')
         slug = self.get_argument('slug')
         spec = {'type': type, 'slug': slug}
         cid = Content.one(spec)['_id']
         uid = self.current_user['_id']
         if int(flag) in [x for x, _y in MY_FLAGS]:
             has_vote = Vote.one({'uid': uid, 'cid': cid})
             if not has_vote:
                 Content.collection.update(spec,
                                           {'$inc': {
                                               "votes." + flag: 1
                                           }})
                 vote = Vote()
                 vote['uid'], vote['cid'], vote['vote'] = uid, cid, int(
                     flag)
                 vote.save()
                 return self.json_response("You have voted has been noted",
                                           "OK")
             else:
                 Content.collection.update(spec,
                                           {'$inc': {
                                               "votes." + flag: -1
                                           }})
                 Vote.remove({'uid': uid, 'cid': cid})
                 return self.json_response("Your vote has been removed",
                                           "OK")
         raise Exception("Invalid vote")
     except Exception, e:
         raise
         return self.json_response(e.__str__(), "ERROR", data)
Beispiel #20
0
 def search(self, topic, site):
     bs = self.getPage(site.searchUrl + topic)
     searchResults = bs.select(site.resultListing)
     for result in searchResults:
         url = result.select(site.resultUrl)[0].attrs["href"]
         if (site.absoluteUrl):
             bs = self.getPage(url)
         else:
             bs = self.getPage(site.url + url)
         if bs is None:
             print("page or url wrong!")
             return
         title = self.safeGet(bs, site.titleTag)
         body = self.safeGet(bs, site.bodyTag)
         if title != "" and body != "":
             content = Content(topic, title, body, url)
             content.print()
Beispiel #21
0
async def pull_content(node_id: str = Body(..., embed=True), content_id: str = Body(..., embed=True),
                    admin: User = Depends(admin_only)):
    assert admin is not None
    node = await Node.find_one_and_pull(
        find={"id": node_id},
        data={"contents": Content.ref(content_id)}
    )
    return node.export()
Beispiel #22
0
def file_upload():
    if 'file' not in request.files:
        raise FileNotFoundException("File not Found")
    file = request.files['file']
    data = request.form['description']
    # if user does not select file, browser also
    # submit an empty part without filename
    if file.filename == '':
        raise FileNotSelectedException("No selected file")

    if file and allowed_file(file.filename):
        filename = secure_filename(random_string() + ".jpg")
        path = UPLOAD_FOLDER
        content = Content(filename, path, data)
        Content.add_content(content)
        image_file = Image.open(file)
        image_file.save(os.path.join(path, filename), quality=30)
Beispiel #23
0
    def get(self):

        self.templateVars["institutions"] = Institution.query().fetch()
        self.templateVars["authors"] = Author.query().fetch()
        self.templateVars["conferences"] = Conference.query().fetch()
        self.templateVars["publications"] = Publication.query().fetch()
        self.templateVars["contents"] = Content.query().fetch()
        return self.render("admin.html")
Beispiel #24
0
 def render(self, type='article', **kwargs):
     spec = {'status':'published'}
     type = getattr(CONTENT_TYPE, type.upper(), CONTENT_TYPE.ARTICLE)
     spec.update({'type': type})
     limit = kwargs.pop('limit', 3)
     items = Content.all(spec).sort([('created_at', -1)]).limit(limit)
     if items:
         return self.render_string('modules/content-latest', items=items)
     else: return ''
 def test_content_add_model(self):
     """
     Test that the content of the page is successfully added to the database
     """
     content = Content(header="Test_Header", content="Test_Content")
     db.session.add(content)
     db.session.commit()
     self.assertEqual(
         Content.query.filter_by(header="Test_Header",
                                 content="Test_Content").count(), 1)
Beispiel #26
0
def html5upload(request):
    """
    Renders the upload form page.

    Args:
        request: the request object

    Returns:
        response: a http response object
    """
    if request.method == 'POST':  # If the form has been submitted...
        result = []
        for filefield, tmpname in handle_uploaded_file(request):
            c = Content()
            originalname = str(request.FILES["file"])
            c.user = request.user  # Only authenticated users can use this view
            c.set_file(originalname,
                       tmpname)  # Save uploaded file to filesystem
            c.get_type_instance()  # Create thumbnail if it is supported
            c.save()
            Uploadinfo.create(c, request).save()
            # print originalname
            # generating json response array
            result.append({
                "name":
                originalname,
                "size":
                c.filesize,
                "url":
                reverse('content:edit', args=[c.uid]),
                "thumbnail_url":
                '/content/instance/%s-200x200.jpg' % c.uid,
                "delete_url":
                reverse('content:edit', args=[c.uid]),
                "delete_type":
                "POST",
            })
        # print result
        response_data = json.dumps(result)
        # print response_data
        # checking for json data type
        # big thanks to Guy Shapiro
        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'
        return HttpResponse(response_data, mimetype=mimetype)
    return _render_to_response(request, 'content_html5upload.html', {})
Beispiel #27
0
async def admin_upload_content(request: Request,
                               content_data: UploadFile = File(...),
                               content_short: str = Form(...),
                               content_filetype: str = Form(...),
                               admin: User = Depends(get_current_admin)):
    from extensions.mongo import mongo_engine
    file_id = ObjectId()
    new_content = Content(id=file_id,
                          short=content_short,
                          filetype=content_filetype)
    await Content.insert_one(new_content.dict(by_alias=True))
    grid_in = mongo_engine.fs.open_upload_stream_with_id(
        file_id,
        content_data.filename,
        metadata=new_content.dict(by_alias=True))
    with content_data.file as f:
        await grid_in.write(f.read())
        await grid_in.close()
    return RedirectResponse(url="/", status_code=303)
Beispiel #28
0
def sanitize_content(content: Content) -> Content:
    print('> [Text Robot] Sanitizing content...')

    text_without_blank_lines_and_markdown = remove_blank_lines_and_markdown(
        content.original_source_content)
    text_without_dates_in_parentheses = remove_dates_in_parentheses(
        text_without_blank_lines_and_markdown)
    content.sanitized_source_content = text_without_dates_in_parentheses

    print('> [Text Robot] Content sanitized!')
    return content
Beispiel #29
0
def save_to_database(content_item):
	'''
	save a title, link, and timestamp to a database
	'''
	from config import db
	from models import Content

	content_insert = Content(
		title = content_item['title'],
		repeat_count = content_item['repeat_count'],
		image = content_item['image'],
		link = content_item['link'],
		time_scraped = content_item['time_scraped']
	)

	_db_session = db.session

	last_insert = _db_session.execute("select max(id), title, repeat_count from Content").first()
	last_insert_id = last_insert.values()[0]
	last_insert_title = last_insert.values()[1]
	last_insert_repeat_count = last_insert.values()[2]

	#_db_session.query(Content).filter_by(id=last_insert_id).update({"title": last_insert_title + "[repeat]"})
	#_db_session.commit()

	if last_insert_title == content_insert.title:
		if last_insert_repeat_count is None: last_insert_repeat_count = 0
		content_insert.repeat_count  = int(last_insert_repeat_count + 1)
		_db_session.query(Content).filter_by(id=last_insert_id).update({"repeat_count": content_insert.repeat_count})
		if DEBUG: print "Content was the same from last run, not storing content again."
	else:
		_db_session.add(content_insert)

	try:
		_db_session.commit()
	except:
		_db_session.rollback()
		raise
	finally:
		_db_session.close()
Beispiel #30
0
    def __init__(self, log, course, answers, courses):
        self.course_name = ''
        self.users = Users()
        self.tasks = Tasks()
        self.modules = Modules()
        self.content = Content()

        self._parse(log)

        for item in (self.users, self.tasks, self.modules, self.content):
            item.update_data(course, answers)

        self.course_long_name = courses[self.course_name]
Beispiel #31
0
def fetch_content_from_wikipedia(content: Content) -> Content:
    print('> [Text Robot] Fetching content from Wikipedia...')

    algorithmia_client = Algorithmia.client(
        access_credentials('algorithmia.json', 'api_key'))
    wiki_algorithm = algorithmia_client.algo('web/WikipediaParser/0.1.2')
    wiki_response = wiki_algorithm.pipe(content.search_term)
    wiki_content = wiki_response.result.get('content')

    content.original_source_content = wiki_content
    print('> [Text Robot] Fetching done!')

    return content
Beispiel #32
0
async def read_content(content_id: ObjectIdStr,
                       request: Request,
                       flashes: list = Depends(get_message_flashes)):
    with tempfile.TemporaryFile() as file:
        content = Content.parse_obj(
            (await
             mongo_engine.db["fs.files"].find_one({"_id":
                                                   content_id}))["metadata"])
        await mongo_engine.fs.download_to_stream(content_id, file)
        pprint(content)
        file.seek(0)
        data = file.read()
    return Response(content=data, media_type=f"application/{content.filetype}")
Beispiel #33
0
def pieces_by_date():
    start = request.form.get('start')
    if start:
        start_date = datetime.datetime.strptime(start, '%Y-%m-%d').date()
    else:
        start_date = date.today() - timedelta(days=3)
    days = request.form.get('days', 2, type=int)
    html = ''
    for i in xrange(days):
        target_day = start_date - timedelta(days=i)
        pieces_data = Content.get_content_by_date(target_day)
        test = get_template_attribute('other.html', 'test')
        html += test(pieces_data)
    return html
Beispiel #34
0
def pieces_by_date():
	start = request.form.get('start')
	if start:
		start_date = datetime.datetime.strptime(start, '%Y-%m-%d').date()
	else:
		start_date = date.today() - timedelta(days=3)
	days = request.form.get('days', 2, type=int)
	html = ''
	for i in xrange(days):
		target_day = start_date - timedelta(days=i)
		pieces_data = Content.get_content_by_date(target_day)
		test = get_template_attribute('other.html', 'test')
		html += test(pieces_data)
	return html
Beispiel #35
0
def write(request):
    if request.method =='POST':
        content = Content(date = timezone.now())
        form = PostForm(request.POST, instance=content)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('')
    else:
        form = PostForm()
    content_list = Content.objects.all().order_by('id').reverse()
    return render_to_response('blog/index.html',
                              RequestContext(request,
                                             { 'content_list':content_list,
                                               'form':PostForm,
                                             }))
Beispiel #36
0
def extract_feeds() -> List:
    all_feeds = Content.get_all_feeds()
    if all_feeds is not None:
        data = []
        for feed in all_feeds:
            final_note = Dictnote(id=feed.id,
                                  image_name=feed.image_name,
                                  image_path=feed.image_path,
                                  description=feed.description,
                                  like_count=feed.like_count,
                                  created_on=str(feed.created_on))
            data.append(final_note)
        return data
    else:
        raise FileNotFoundException("File not Found")
Beispiel #37
0
def line_type(line):
    """    
    seperate line's msg into many type
    content: normal text
    multiline: is part of last content, because we split log by \n
    datestamp: date for following message
    invite: someone invite others into group
    join: the one was invited join group
    title: Group's name
    savedate: the date user saved log
    """
    content_r = re.match('(\d{2}:\d{2})\t(.*?)\t(.*?)', line)
    if content_r:
        time = content_r.group(1)
        name = content_r.group(2)
        text = content_r.group(3)
        content = Content(time, name, text)
        return content

    datestamp_r = re.match('^(\d{4}\/\d{2}\/\d{2})\((週.)\)', line)
    if datestamp_r:
        return ('datestamp', datestamp_r.group(1), datestamp_r.group(2))

    invite_r = re.match('(\d{2}:\d{2})\t(.*)邀請(.*)加入群組。', line)
    if invite:
        time = invite_r.group(1)
        inviter = invite_r.group(2)
        invitee = invite_r.group(3)
        invite = Invite(time, inviter, invitee)
        return invite

    join = re.match('(\d{2}:\d{2})\t(.*)加入聊天', line)
    if join:
        dic = {'time': join.group(1), 'name': join.group(2)}
        return ('join', dic)

    title = re.match('\ufeff\[LINE\] (.*)的聊天記錄', line)
    if title:
        return ('title', title.group(1))

    save_date = re.match('儲存日期:(\d{4}\/\d{2}\/\d{2} \d{2}:\d{2})', line)
    if save_date:
        return ('save_date', save_date.group(1))

    if line.strip() == "":
        return ('empty', line)

    return ('multiline', line)
Beispiel #38
0
 def _extra_context(self, context):        
     extras = {
         "today": date.today(),
         "yesterday": date.today() - timedelta(1),
         "this_month": date.today(),
         "last_month": date.today() - timedelta(monthrange(date.today().year, date.today().month)[1]),
     }
     path = self.request.path        
     content = Content.all().filter('path =', path)
     content_extras = {}
     for item in content:
         content_extras[item.ident] = item.value
             
     context.update(extras)
     context.update(content_extras)
     return context
Beispiel #39
0
def create_content(content_name,
                   browser=False,
                   script=False,
                   url=None,
                   script_body=None):
    if db_session.query(Content).filter(
            Content.content_name == content_name).first():
        return False  # Content already exists with this name
    # TODO : Fix scripts being saved without newlines
    new_content = Content(content_name=content_name,
                          browser=browser,
                          url=url,
                          script=script,
                          script_body=script_body)
    db_session.add(new_content)
    db_session.commit()
    return True
Beispiel #40
0
def index():
    index_show = Content.query.order_by(Content.pub_date.desc()).all()
    vote_stat = ''
    if current_user.is_authenticated():
        vote_stat = VoteStat.query.filter_by(user_id=current_user.id).all()
    pieces_data = []
    start_date = None
    for delta in xrange(0, 5):
        target_day = date.today() - timedelta(days=delta)
        pieces_data.append(Content.get_content_by_date(target_day))
        start_date = target_day.strftime('%Y-%m-%d')

    return render_template('index.html',
                           index_show=index_show,
                           pieces_data=pieces_data,
                           start_date=start_date,
                           timedelta=timedelta,
                           vote_stat=vote_stat)
Beispiel #41
0
def api_upload(request):
    """
    Renders the upload form page.
    """
    if request.method == 'POST': # If the form has been submitted...
        #for header in request.META.keys():
        #    if header.startswith('HTTP'):
        #        print header, request.META[header]
        #print request.raw_post_data[:1000]
        if request.user.is_authenticated() is False:
            return HttpResponse(status=401)
        form = UploadForm(request.POST, request.FILES) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            for filefield, tmpname in handle_uploaded_file(request):
                SUPPORTED_FIELDS = ['title', 'caption', 'author']
                kwargs = {}
                for field in SUPPORTED_FIELDS:
                    kwargs[field] = request.POST.get(field)
                try:
                    kwargs['point'] = Point(float(request.POST.get('lon')), float(request.POST.get('lat')))
                except:
                    #raise
                    pass
                print kwargs
                c = Content(**kwargs)
                originalname = str(request.FILES["file"])
                c.user = request.user # Only authenticated users can use this view
                c.set_file(originalname, tmpname) # Save uploaded file to filesystem
                c.get_type_instance() # Create thumbnail if it is supported
                c.save()
                break # We save only the first file
            response = HttpResponse(status=201)
            #response.status_code = 201
            # FIXME: use reverse()
            response['Location'] = '/content/api/v1/content/%s/' % c.uid
            return response
            #return HttpResponseRedirect(reverse('edit', args=[c.uid]))
        else:
            response = HttpResponse(status=204)
            return response
    else:
        raise Http404
Beispiel #42
0
def upload(request):
    """
    Renders the upload form page.
    """
    if request.method == 'POST': # If the form has been submitted...
        form = UploadForm(request.POST, request.FILES) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            for filefield, tmpname in handle_uploaded_file(request):
                c = Content()
                originalname = str(request.FILES["file"])
                c.user = request.user # Only authenticated users can use this view
                c.set_file(originalname, tmpname) # Save uploaded file to filesystem
                c.get_type_instance() # Create thumbnail if it is supported
                c.save()
            return HttpResponseRedirect(reverse('edit', args=[c.uid]))
    else:
        form = UploadForm(initial={}) # An unbound form
    return _render_to_response(request, 'content_upload.html', {
                                  'uploadform' : form,
                               })
Beispiel #43
0
def index():
	index_show = Content.query.order_by(Content.pub_date.desc()).all()
	vote_stat = ''
	if current_user.is_authenticated():
		vote_stat = VoteStat.query.filter_by(user_id=current_user.id).all()
	pieces_data = []
	start_date = None
	for delta in xrange(0, 5):
		target_day = date.today() - timedelta(days=delta)
		pieces_data.append(Content.get_content_by_date(target_day))
		start_date = target_day.strftime('%Y-%m-%d')
		
	return render_template('index.html',
		index_show=index_show,
		pieces_data = pieces_data,
		start_date = start_date,
		timedelta = timedelta,
		vote_stat = vote_stat
		)
Beispiel #44
0
 def post(self, partner_id):
     db_session = maker()
     body = self.get_argument('body')
     my_user_query = self.get_a_user_query_from_id(self.current_user)
     time_stamp = time.strftime('%Y-%m-%d %H:%M:%S')
     # データ追加
     new_content = Content(from_id=my_user_query.id,
                           to_id=partner_id,
                           content=body,
                           datetime=time_stamp)
     try:
         db_session.add(new_content)
         db_session.commit()
     except Exception as e:
         db_session.rollback()
         logging.warning(e)
     finally:
         db_session.close()
     self.redirect('/chat/{}'.format(partner_id))
Beispiel #45
0
def feed(request,path):
    if not path:
        FeedClass = Content.getFeedClass()
        feedGenerator = FeedClass(path,request).get_feed()
    else:
        try:
            cpath = CategoryPath(path)
        except PathNotFound:
            raise Http404

        contentItem = cpath[0]
        if isinstance( contentItem, ContentCategory ):
            FeedClass = contentItem.getFeedClass()
            feedGenerator = FeedClass(path,request).get_feed()
        else:
            raise Http404

    response = HttpResponse( mimetype=feedGenerator.mime_type )
    feedGenerator.write( response, 'utf-8' )
    return response
 def test_questions_answers_add_model(self):
     """
     Test that the question and then answer is successfully added to the database
     """
     content = Content(header="Test_Header", content="Test_Content")
     question = Questions(question_text="Test_Question?", content=content)
     answer = Answers(answer_text="Answer_Test",
                      correct=0,
                      question=question)
     db.session.add(content)
     db.session.add(question)
     db.session.add(answer)
     db.session.commit()
     self.assertEqual(
         Questions.query.filter_by(question_text="Test_Question?").count(),
         1)
     self.assertEqual(
         Answers.query.filter_by(answer_text="Answer_Test",
                                 correct=0,
                                 question=question).count(), 1)
Beispiel #47
0
    def __call__(self, user=None):
        query = Content.query(ancestor=ndb.Key('Content', self.ancestor))
        count = query.count()
        invalid_choices = memcache.get("invalid_content:" + user.email()) or []
        choices = filter(lambda choice: choice not in invalid_choices, range(count))
        while count > 0 and len(choices):
            choice = random.choice(choices)
            content = query.fetch(offset=choice, limit=1)[0]

            if self.unrated or self.sample_subcontent:
                subquery = SubContent.query(ancestor=content.key)
                subcontent_keys = subquery.fetch(keys_only=True)

            if self.unrated:
                if len(subcontent_keys) == 0:
                    filters = Rating.content == content.key
                else:
                    filters = Rating.content.IN(subcontent_keys)
                if user:
                    filters = ndb.AND(Rating.user == user, filters)
                ratings = Rating.query(filters, \
                              ancestor=ndb.Key('Rating', self.ancestor)).count()

                invalid  = self.sample_subcontent and ratings > 0
                invalid |= len(subcontent_keys) == 0 and ratings == 1
                invalid |= len(subcontent_keys) > 0 and ratings == len(subcontent_keys)
                if invalid:
                    choices.remove(choice)
                    invalid_choices.append(choice)
                    memcache.set("invalid_content:" + user.email(), invalid_choices)
                    continue
            
            if len(subcontent_keys) and self.sample_subcontent:
                content = random.choice(subcontent_keys).get()

            return content
        else:
            return None
Beispiel #48
0
 def run(self, anon=False, forward=False):
     try:
         data = get_content(self.message)
     except ContentError as e:
         self.send.message(e)
     with config.DB.atomic() as tnx:
         try:
             content = Content.create(type=data[0],
                                      text=data[1],
                                      file_id=data[2])
             post = Post(content=content,
                         token=gen_token(),
                         address=Address.new())
             if not anon:
                 post.user = self.user_id
             if forward:
                 if self.message.forward_from:
                     self.send_message(
                         'you cannot forward messages for forward posting, write your message'
                     )
                     return (type(self), {'forward': forward})
                 post.forward_message_id = self.message.message_id
                 post.created_at = self.message.date
             post.save()
             if str(
                     self.user_id
             ) == config.ADMIN_ID and config.ADMIN_DEFAULT_BALANCE > 0 and not config.DEBUG:
                 post.send(config.ADMIN_DEFAULT_BALANCE, bot=self.bot)
                 post.address.is_accepted = True
                 post.address.save()
                 self.send_message('message posted')
             else:
                 self.send_message(TEXT_PAY.format(post.address.address))
                 self.send_message(post.address.address)
             tnx.commit()
         except Exception as e:
             logger.error(e)
             tnx.rollback()
Beispiel #49
0
def save_record():
    data = request.get_json()
    header = data['header']
    title = data['title']
    content = data['text']
    content = add_class(content, "img", "img-fluid")
    content = add_img_id(content)

    if 'id' in data:
        id = data['id']
        article = Content.query.get(id)
        if not article:
            return jsonify("An error occured")

        article.header = header
        article.title = title
        article.content = content
    else:
        article = Content(header=header, title=title, content=content)
        db.session.add(article)

    db.session.commit()
    return jsonify("Action executed with success")
def handle_tweet(tweet):
    global num_tweets
    num_tweets += 1
    print 'got tweet: %s' % bs(tweet.text)
    if num_tweets % 100 == 0:
        print "X" * 10, num_tweets
        # break
    content = Content()
    tweet_text = tweet.text
    tweetwhen = bs(tweet.created_at)
    s = tweet_text.split()
    #tags = set()
    for word in s:
        if word.startswith("#"):
            word = bs(word)
            if content.hashtags == None:
                content.hashtags = ""
            else:
                content.hashtags = content.hashtags + " " +word 
    content.message = tweet_text
    content.timestamp = tweetwhen

    if tweet.coordinates:
        print "tweet had a location"
        lat = tweet.coordinates["coordinates"][0]
        lon = tweet.coordinates["coordinates"][1]
        point = Point(lat, lon)

        neighborhood = find_in_boxes(boxes, point)
        if neighborhood is None:
            print "tweet didn't have a neighborhood"
            return
        print type(tweet_text)
        print bs(tweet_text)
        print neighborhood.name
        content.neighborhood_id = neighborhood.id
#        content.hashtags = " ".join(tags) 
    try:
        db.session.add(content)
        print ">>>>>> adding"
        db.session.commit()
    except Exception, e:
        print "error, not committing"
        traceback.print_exc()
Beispiel #51
0
    def get_contents(self, get_subcontents=True, get_ratings=False, get_ratings_count=True):
        contents = Content.query(ancestor=ndb.Key('Content', ANNOTATION_NAME)) \
                          .order(Content.date).fetch()

        subcontents = defaultdict(list)
        if get_subcontents:
            query = SubContent.query(ancestor=ndb.Key('Content', ANNOTATION_NAME))
            for subcontent in query.fetch():
                subcontents[subcontent.key.parent().urlsafe()] += subcontent,
                
        ratings = defaultdict(list)
        ratings_counts = defaultdict(int)
        if get_ratings or get_ratings_count:
            query = Rating.query(ancestor=ndb.Key('Rating', ANNOTATION_NAME))
        
            if get_ratings:
                for rating in query.fetch():
                    ratings[rating.content.urlsafe()].append(rating)            

            if get_ratings_count:
                for rating in query.fetch(projection=['content']):
                    ratings_counts[rating.content.urlsafe()] += 1

        return contents, subcontents, ratings, ratings_counts
Beispiel #52
0
def delete_content(type_):
    return Content.delete_content(type_)
Beispiel #53
0
def add_content(data, type_):
    content = Content(data, type_)

    return Content.save_content(content)
Beispiel #54
0
def delete_content(_type):
    return Content.delete_content(_type)
site.login("Yaslient1", password)

f1 = open("/home/bigent/Projects/yaslient-bot-wiki/data/dateinfobox_list.txt", "r")
data = []

for i in f1.readlines():
    i = str(i).strip()
    data.append(i)

for i in data:
    if not u"anon" in page.Page(site, title=i).getHistory()[0].keys():
        print "{} adlı maddenın wikitext'i alınıyor...".format(i)
        old_content = page.Page(site, title=i).getWikiText()
        print "Alındı."

        con = Content(content=old_content)
        old_infoboxes = con.findInfoboxes()

        print "Doğum ve ölüm tarihi yenileniyor..."
        con.fixBirthAndDeathDates()
        print "Yenilendi."

        if old_infoboxes == con.infoboxes:
            print "Herhangi bir değişiklik olmadığından madde düzenlenmiyor."
        else:
            print "Maddenin eski hali yeni haliyle değiştiriliyor..."
            page.Page(site, i).edit(con.render(), bot="yes", skipmd5=True)
            print "Değiştirildi."
        #print "====!===="
        #print con.render()
        #print "====!===="
Beispiel #56
0
def query_content(ANNOTATION_NAME):
    query = Content.query(ancestor=ndb.Key('Content', ANNOTATION_NAME))
    return (query, query.count())
Beispiel #57
0
def like_count():
    like = request.get_json()
    id = like['id']
    count = Content.get_like(id)
    return count
Beispiel #58
0
def get_contents():
    return Content.get_contents()
Beispiel #59
0
def register(request):
    if request.POST:
        content = Content()
        content.user = request.user
        content.image1 = request.FILES['image1_upload']
        content.image2 = request.FILES['image2_upload']
        content.image3 = request.FILES['image3_upload']
        content.image4 = request.FILES['image4_upload']
        content.image5 = request.FILES['image5_upload']
        content.image6 = request.FILES['image6_upload']
        content.image7 = request.FILES['image7_upload']
        content.image8 = request.FILES['image8_upload']
        content.terms = request.POST.get('terms')
        content.save()
        return redirect('/portal/register')

    try:
        gallery = Content.objects.get(user=request.user)
        return render(request, 'portal/register.html', {'gallery': gallery})
    except ObjectDoesNotExist:
        print 'Does Not Exist!'
        return render(request, 'portal/register.html')
Beispiel #60
0
def add_content(data, _type):
    content = Content(data, _type)

    return Content.save_content(content)