예제 #1
0
def add_resource():
    if request.method == 'GET':
        return render_template("form.html",
                               action="Add",
                               resource={},
                               tag="",
                               message="")
    data = request.form.to_dict(flat=True)
    data['owner_id'] = current_user.id
    #print data
    message = valid_resource_time(data['available_start'],
                                  data['available_end'])
    if len(data['name']) == 0:
        message = "name can't be empty"
    if message != "":
        return render_template("form.html",
                               action="Add",
                               resource={},
                               tag="",
                               message=message)
    resource = Resource()
    resource.deserialize(data)
    tag_list = data['tag'].split()
    for tag_name in tag_list:
        tag = db.session.query(Tag).filter_by(value=tag_name.lower()).first()
        if not tag:
            tag = Tag(tag_name.lower())
            db.session.add(tag)
        resource.tags.append(tag)
    db.session.add(resource)
    try:
        db.session.commit()
    except:
        db.session.rollback()
    return redirect(url_for('.get_resources', id=resource.id))
예제 #2
0
def retrieve(request, uri):
    resource = None
    try:
        resource = Resource.objects.get(uri=uri)
        if not resource.retrieved:
            result = resource.source.getManager().get(uri)

    except ObjectDoesNotExist:
        
        result = None
        for repository in Repository.objects.all():
            result = repository.getManager().get(uri)
            if result is not None:
                if result is not None:
                    resource = Resource(
                        uri = uri,
                        source = repository,
                    )
                    resource.save()
                    break
    if resource is None:
        return JSONResponse([])

    if not resource.retrieved and result is not None:
        resource = attach_content(resource, result)

    return JSONResponse(resource)
예제 #3
0
    def test_model_eq(self):
        resource1 = Resource(ID=1, name="Resource1", resource_type="ROOM")
        resource2 = Resource(ID=1, name="Resource1", resource_type="ROOM")
        resource3 = Resource(ID=1, name="Resource3", resource_type="ROOM")
        self.assertEqual(resource1, resource2)
        self.assertNotEqual(resource1, resource3)

        date = QtCore.QDate.currentDate()
        start_time = QtCore.QTime(8, 0)
        end_time = QtCore.QTime(9, 0)
        start = QtCore.QDateTime(date, start_time)
        end = QtCore.QDateTime(date, end_time)
        reservation1 = Reservation(ID=1,
                                   resource=resource1,
                                   start=start,
                                   end=end)
        reservation2 = Reservation(ID=1,
                                   resource=resource2,
                                   start=start,
                                   end=end)
        reservation3 = Reservation(ID=1,
                                   resource=resource3,
                                   start=start,
                                   end=end)
        self.assertEqual(reservation1, reservation2)
        self.assertNotEqual(reservation1, reservation3)
예제 #4
0
    def get(self):
        user = users.get_current_user()
        if user:
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
            # retrieve reservations by current user
            now_time = datetime.now() - timedelta(minutes=300)
            reservation_by_curr_user = Reservation.query(ndb.AND(Reservation.user == user.email(),
                                                                 Reservation.end_time > now_time)) \
                                                  .fetch()
            if reservation_by_curr_user:
                reservation_by_curr_user = sorted(reservation_by_curr_user,
                                                  key=lambda r: r.start_time)

            # retrieve all resources in system
            sorted_resources = Resource.query().order(
                -Resource.last_reservation_time)
            # retrieve resources owned by current user
            resources_owned = Resource.query(Resource.owner == user.email())

            template_values = {
                'user': user,
                'reservation_by_curr_user': reservation_by_curr_user,
                'sorted_resources': sorted_resources,
                'resources_owned': resources_owned,
                'url': url,
                'url_linktext': url_linktext,
            }

            template = JINJA_ENVIRONMENT.get_template('index.html')
            self.response.write(template.render(template_values))

        else:
            self.redirect(users.create_login_url(self.request.uri))
예제 #5
0
파일: tests.py 프로젝트: trawick/edurepo
    def test_strings(self):
        lo2 = self.lo0
        url = 'http://www.google.com/'
        r = Resource(objective=lo2, url=url)
        r.full_clean()
        r.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        rs.full_clean()
        rs.save()

        self.assertTrue(' created ' in str(rs))
        self.assertEquals(rs.type_str(), 'Created')

        rs = ResourceSubmission(user=self.u1, resource=r, type='f')
        rs.full_clean()
        rs.save()

        self.assertTrue(' flagged ' in str(rs))
        self.assertEquals(rs.type_str(), 'Inappropriate')

        rs = ResourceSubmission(user=self.u2, resource=r, type='v')
        rs.full_clean()
        rs.save()

        self.assertTrue(' voted on ' in str(rs))
        self.assertEquals(rs.type_str(), 'Up-voted')
예제 #6
0
    def post(self, request):
        self.query_dict = {'classgroup': request.DATA.get('classgroup')}

        self.verify_membership()

        resource_type = request.DATA.get('resource_type')

        if resource_type != "vertical":
            raise Http404

        resource = Resource(resource_type=resource_type,
                            user=request.user,
                            classgroup=self.cg)
        resource.save()

        renderer = ResourceRenderer(resource,
                                    user=request.user,
                                    static_data={
                                        'request':
                                        request,
                                        'author_post_link':
                                        '/api/resources/author/'
                                    })

        html = renderer.author_view().get_html()

        return Response({'html': html, 'display_name': resource.display_name})
예제 #7
0
	def get(self):
		if users.get_current_user():
			url = users.create_logout_url(self.request.uri)
			url_linktext = 'Logout'
		else:
			url = users.create_login_url(self.request.uri)
			url_linktext = 'Login'
			self.redirect(users.create_login_url(self.request.uri))
		user = users.get_current_user()

		# query resources created by the current user
		resource_query = Resource.query(user == Resource.author).order(-Resource.lastReserveDate)
		resources = resource_query.filter().fetch()

		resources = None
		if Resource:
			if Resource.lastReserveDate:
				resource_query = Resource.query(user == Resource.author).order(-Resource.lastReserveDate)
				resources = resource_query.fetch()
			elif Resource.modDate:
				resource_query = Resource.query(user == Resource.author).order(-Resource.modDate)
				resources = resource_query.fetch()

		nowStr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
		now = datetime.strptime(nowStr, '%Y-%m-%d %H:%M:%S')

		template_values = {
			'resources': resources,
			'user': user,
			'url': url,
			'url_linktext': url_linktext,
			'now': now
		}
		template = JINJA_ENVIRONMENT.get_template('myResource.html')
		self.response.write(template.render(template_values))
예제 #8
0
def workspace_test():
	print_data('workspaces objects', br=False)

	for index in range(3):
		w = Workspace()
		w.name = 'New workspace name'
		w.description = 'Some new description'
		w.save()

	workspaces = Workspace.all()
	print_data('new objects -> model.all()', workspaces)

	w.name = 'Updated name'
	w.save()

	workspaces = Workspace.all()
	print_data('UPDATED -> model.all()', workspaces)

	workspaces = Workspace.get(id=w.id, name=w.name)
	print_data('GET -> model.get()', [workspaces])

	workspaces = Workspace.filter(name='New workspace name')
	print_data('FILTER -> model.filter()', workspaces)

	for index in range(2):
		o = Application()
		o.workspace_id = w.guid
		o.save()

	a = View()
	a.application_id = o.guid
	a.save()

	a = Resource()
	a.application_id = o.guid
	a.save()

	for index in range(3):
		o = Widget()
		o.workspace_id = w.guid
		o.save()

	for index in range(3):
		o = DataSource()
		o.workspace_id = w.guid
		o.save()

	objects = Workspace.all() + Resource.all() + Application.all() + Widget.all() + DataSource.all() + View.all()
	print_data('All objects in db', objects)

#	[w.delete() for w in Workspace.all()]
	workspaces = Workspace.all()
	print_data('cleaned', workspaces)

	workspaces = Workspace.filter(include_deleted=True)
	print_data('cleaned with deleted if exists', workspaces)

	objects = Workspace.all() + Resource.all() + Application.all() + Widget.all() + DataSource.all() + View.all()
	print_data('no objects left', objects)
예제 #9
0
    def save(self, data: Dict):
        self.validate(data)

        try:
            resource = Resource(data)
            resource.save()
        except ValueError as err:
            raise ValidationError(message=str(err))
예제 #10
0
파일: tests.py 프로젝트: trawick/edurepo
 def test_bad_url(self):
     bad_urls = ('ftp://*****:*****@example.com/',
                 'http://*****:*****@example.com/',
                 'http://foo',
                 )
     for (bad_url, i) in zip(bad_urls, range(len(bad_urls))):
         r = Resource(objective=self.lo0, url=bad_url)
         self.assertRaises(ValidationError, lambda: r.full_clean())
예제 #11
0
def api_resources(*, page='1'):
    page_index = get_page_index(page)
    num = yield from Resource.findNumber('count(id)')
    p = Page(num, page_index)
    if num == 0:
        return dict(page=p, resources=())
    resources = yield from Resource.findAll(orderBy='created_at desc',
                                            limit=(p.offset, p.limit))
    return dict(page=p, resources=resources)
예제 #12
0
파일: views.py 프로젝트: wylove/djangoweb
def add_new_resource(request):
    title = request.POST.get('title', '').strip()
    type = request.POST.get('type')
    # to image: filename, to video: id
    attach = request.POST.get('attach', '').strip()

    # check params
    if not _check_login(request):
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "请登录后操作!"}))
    if len(title) <= 0:
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "资源标题不能为空!"}))
    if len(title) >= 1000:
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "资源标题过长,只能1000个字符!"}))
    if len(attach) <= 0:
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "请上传一个视频或者图片!"}))
    if type not in ('image', 'video'):
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "资源类型不支持!"}))
    if type == 'image' and not default_storage.exists(os.path.join(config.get_config('SHAREHP_UPLOAD_DIR'), attach)):
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "服务器图片丢失,请重新上传!"}))
    if type == 'video':
        video_info = cache.get_video_info(attach)
        if not video_info:
            return HttpResponse(json.dumps({'success': -1, 'error_msg': "服务器视频丢失,请重新上传!"}))

    # build filed data
    try:
        if type == 'image':
            thumbnail, content = _deal_image_resource(attach)
        else:
            thumbnail, content = _deal_video_resource(video_info)

    except QiniuUploadFileError:
        logger.error('Fail to add new resource, uploading image error!\n')
        return HttpResponse(json.dumps({'success': -1, 'error_msg': "服务器异常,请稍后再试!"}))

    # 保存资源
    current_date = datetime.now()
    resource = Resource(
        gmt_create=current_date,
        gmt_modify=current_date,
        user_id=_get_current_userid(request),
        title=title,
        type=type,
        thumbnail=thumbnail,
        content=content,
        up=0,
        down=0,
        comments=0,
        status='enabled'
    )
    resource.save()
    cache.set_last_resource_id(resource.id)  # important
    return HttpResponse(json.dumps({'success': 0, 'data': {}}))
예제 #13
0
 def getCollage():
     collage = "%s/scripts/img/mincportfolio.png" % (access.server_url)
     try:
         result = None
         if Resource.all().count() == 1:
             for resource in Resource.all():
                 result = resource.resource_key.key()
             collage = "%s/serve/%s" % (access.server_url, result)
             return collage
         else:
             return collage
     except:
         return collage
예제 #14
0
def serve(request, resource):
    verb = request.method

    if verb == "GET":
        try:
            entry = Resource.objects.get(key=resource)
            return HttpResponse("<p> " + str(entry.value) + "</p>" + out)
        except Resource.DoesNotExist:
            return HttpResponseNotFound("ERROR: Entrada no disponible")
    elif verb == "PUT":
        entry = Resource(key=resource, value=request.body)
        entry.save()
        return HttpResponse("Entrada insertada correctamente")
def serve(request, resource):
    verb = request.method
    
    if verb == "GET":
        try:
            entry = Resource.objects.get(key=resource)
            return HttpResponse("<p> " + str(entry.value) + "</p>" + out)
        except Resource.DoesNotExist:
            return HttpResponseNotFound("ERROR: Entrada no disponible")
    elif verb == "PUT":
        entry = Resource(key=resource, value=request.body)
        entry.save()
        return HttpResponse("Entrada insertada correctamente")
예제 #16
0
def handle_create(request, resource_type):
    '''
    handle FHIR create operation
    '''
    correctible = (request.format == 'xml')
    valid, search_elements = fhir_parser.parse_resource(
        resource_type, request.data, correctible)
    if not valid:
        return fhir_error.inform_bad_request()

    resource = Resource(resource_type, request.data, owner_id=request.authorizer.email)
    index_resource(resource, search_elements)

    return resource.as_response(request, created=True)
예제 #17
0
파일: views.py 프로젝트: sgml/simpledav
    def put(self):
        """Uploads a file."""
        path = self.request_path
        parent_path = os.path.dirname(path)

        # anything at this path already?
        existing_resource = Resource.get_by_path(path)

        if existing_resource:
            existing_resource.delete_recursive()

        # fetch parent
        if parent_path:
            parent = Resource.get_by_path(parent_path)
            if not parent or not parent.is_collection:
                return self.response.set_status(
                    409, "Conflict")  # must create parent folder first
        else:
            parent = Resource.root()

        logging.info("Creating resource at %s" % path)
        data = ResourceData(blob=self.request.body)
        data.put()

        resource = Resource(path=path, parent_resource=parent, data=data)
        resource.content_length = len(self.request.body)
        resource.put()

        self.response.set_status(201, 'Created')
예제 #18
0
def saveResource(request):
    #print("Test")
    resources = Resource.objects.all()
    #print(resources.length)
    resoID = resources.__len__() + 1
    #print(ResoID)
    name = request.GET["Name"]
    lat = request.GET["Lat"]
    lng = request.GET["Lng"]
    skills = request.GET["Skills"]

    newResource = Resource(ResoID=resoID,Name=name,Lat=lat,Lng=lng,Skills=skills)
    newResource.save()
    return HttpResponse("OK")
예제 #19
0
def handle_create(request, resource_type):
    '''
    handle FHIR create operation
    '''
    correctible = (request.format == 'xml')
    valid, search_elements = fhir_parser.parse_resource(
        resource_type, request.data, correctible)
    if not valid:
        return fhir_error.inform_bad_request()

    resource = Resource(resource_type, request.data, owner_id=request.authorizer.email)
    index_resource(resource, search_elements)

    return resource.as_response(request, created=True)
예제 #20
0
def create_resource():
    """Creates a new todo and returns JSON of that created todo"""
    new_resource = Resource(
        name=request.json["name"],
        url=request.json["url"],
        description=request.json["description"],
        login_required=request.json["login_required"],
        api_available=request.json["api_available"],
        country_of_origin=request.json["country_of_origin"],
        documentation_url=request.json["documentation_url"],
        keywords=request.json["keywords"])
    db.session.add(new_resource)
    db.session.commit()
    response_json = jsonify(resource=new_resource.serialize())
    return (response_json, 201)
예제 #21
0
파일: tests.py 프로젝트: trawick/edurepo
    def test_1(self):
        """Basic creation of Resource, disallowing same URL+objective combination"""
        lo1 = self.lo0
        url1 = 'http://127.0.0.1/foo.html'
        r1 = Resource(objective=lo1, url=url1)
        self.assertEquals(r1.votes, 0)
        self.assertEquals(r1.inappropriate_flags, 0)
        r1.save()
        when_added = r1.when_added
        now = datetime.datetime.now()
        when_added = when_added.replace(tzinfo=None)
        self.assertTrue(now - when_added < datetime.timedelta(seconds=5))

        r2 = Resource(objective=lo1, url=url1)
        self.assertRaises(IntegrityError, lambda: r2.save())
예제 #22
0
 def update_resources(self):
     for course in self.current_courses:
         self.open_with_session(course.url)
         for a in self.page.select('#region-main a[href]'):
             url = a['href']
             mod = {
                 'nombre': a.get_text(),
                 'url': url,
                 'enviado': False,
                 'idMateria': course.idMateria,
                 'idTipoRecurso': self._identifiy_type_of_resource(url)
             }
             if Resource.not_loaded(mod):
                 Resource.insert(mod)
         print(f'OK - {course.idMateria} -> {course.nombre}')
예제 #23
0
    def get(self):
        resource_id = self.request.get('id')
        resource = Resource.query(Resource.id == resource_id).get()
        reservations = Reservation.query(
            Reservation.resource_id == resource_id).fetch()

        header = '<?xml version="1.0" encoding="UTF-8" ?>'
        tag_owner = '<owner>{}</owner>'.format(resource.owner)
        tag_name = '<name>{}</name>'.format(resource.name)
        tag_start = '<start_time>{}</start_time>'.format(
            resource.available_start_time)
        tag_end = '<end_time>{}</end_time>'.format(resource.available_end_time)
        tags_reservation = []
        for r in reservations:
            t = {}
            t['user'] = '******'.format(r.user)
            t['start'] = '<reservedAt>{}</reservedAt>'.format(r.start_time)
            tags_reservation.append(t)

        template_values = {
            'header': header,
            'owner': tag_owner,
            'name': tag_name,
            'start_time': tag_start,
            'end_time': tag_end,
            'reservations': tags_reservation,
        }
        template = JINJA_ENVIRONMENT.get_template('rss.html')
        self.response.write(template.render(template_values))
예제 #24
0
 def get_all(self):
     resources = []
     self.cursor.execute('SELECT * FROM resources ORDER BY name COLLATE NOCASE')
     rows = self.cursor.fetchall()
     for row in rows:
         resources.append(Resource(row=row))
     return resources
예제 #25
0
    def fetch_request_token(self, oauth_consumer, oauth_callback):
        logger.warning("!!! In MockOAuthDataStore.fetch_request_token  args: %s"%locals())
        
        if oauth_consumer.key != self.consumer.key:
            raise OAuthError('Consumer key does not match.')
            
        # OAuth 1.0a: if there is a callback, check its validity
        callback = None
        callback_confirmed = False
        if oauth_callback:
            if oauth_callback != OUT_OF_BAND:
                if check_valid_callback(oauth_callback):
                    callback = oauth_callback
                    callback_confirmed = True
                else:
                    raise oauth.OAuthError('Invalid callback URL.')

        #not going to implement scope just yet-so just hard code this for now
        resource = Resource.all().filter("name =","default")[0]
        
        #try:
        #    resource = Resource.objects.get(name=self.scope)
        #except:
        #    raise OAuthError('Resource %s does not exist.' % escape(self.scope))
        
        self.request_token = Token.create_token(consumer=self.consumer,
                                                        token_type=Token.REQUEST,
                                                        timestamp=self.timestamp,
                                                        resource=resource,
                                                        callback=callback,
                                                        callback_confirmed=callback_confirmed)
        
        return self.request_token
예제 #26
0
def add():
    if not g.user.is_authenticated():
        return render_template('add.html')
    if request.method == 'GET':
        return render_template('add.html')

    resource_type = request.form['resource_type']
    url = request.form['url'].strip()
    resource = Resource.query.filter_by(resource_type=resource_type,
                                        url=url).first()
    if resource is not None:
        flash('service already registered (%s, %s)' % (resource_type, url),
              'danger')
        if 'resource_type' in request.args:
            rtype = request.args.get('resource_type')
            return redirect(url_for('add', resource_type=rtype))
        return redirect(url_for('add'))

    [title, success, response_time, message,
     start_time] = run_test_resource(resource_type, url)

    resource_to_add = Resource(current_user, resource_type, title, url)
    run_to_add = Run(resource_to_add, success, response_time, message,
                     start_time)

    DB.session.add(resource_to_add)
    DB.session.add(run_to_add)
    try:
        DB.session.commit()
        flash('service registered (%s, %s)' % (resource_type, url), 'success')
    except Exception, err:
        DB.session.rollback()
        flash(str(err), 'danger')
예제 #27
0
	def get(self):
		if users.get_current_user():
			url = users.create_logout_url(self.request.uri)
			url_linktext = 'Logout'
		else:
			self.redirect(users.create_login_url(self.request.uri))
		user = users.get_current_user()

		nowStr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
		now = datetime.strptime(nowStr, '%Y-%m-%d %H:%M:%S')

		tagIDStr = self.request.get('tag')
		resources = None
		if tagIDStr != "" and Resource and Resource.tags:
			if Resource.tags:
				resource_query_byTags = Resource.query(tagIDStr == Resource.tags)
				if Resource.lastReserveDate:
					resource_inOrder = resource_query_byTags.order(-Resource.lastReserveDate)
					if resource_inOrder:
						resources = resource_inOrder.fetch()

		template_values = {
			'user': user,
			'url': url,
			'url_linktext': url_linktext,
			'resources': resources,
			'now': now
		}

		template = JINJA_ENVIRONMENT.get_template('tag.html')
		self.response.write(template.render(template_values))
예제 #28
0
def store_scene_json():
    from models import Resource
    from docs import Blob
    from application import db

    json_data = json.loads(request.data)
    data = json_data['scene']
    name = json_data['name']
    type = 'scene'
    tags = 'scene json plain'
    blob_key = Blob(json.dumps(data)).save()
    resource = Resource(name=name, type=type, blob_key=blob_key, tags=tags)
    db.session.add(resource)
    db.session.commit()
    resource_url = url_for('resource.get_by_name', resource_name=name)
    resource_url_base64 = base64.encodestring(resource_url)
    view_url = url_for('player', path=resource_url_base64)
    result = json.dumps({
        "success": True,
        "data": {
            "resource_url": resource_url,
            "base64_resource_url": resource_url_base64,
            "resource_id": resource.id,
            "view_url": view_url
        }
    })
    return result
예제 #29
0
    def restore_object(self, attrs, instance=None):
        user = self.context['request'].user
        classgroup = attrs.get('classgroup')
        name = attrs.get('name')

        attributes = ['data', 'approved']

        if instance is None:
            instance = Resource(user=user, classgroup=classgroup, name=alphanumeric_name(name), display_name=name)
            instance.save()
        else:
            if instance.user != user:
                raise serializers.ValidationError("Class name is already taken.")

        instance = set_attributes(attributes, attrs, instance)
        return instance
예제 #30
0
파일: views.py 프로젝트: sgml/simpledav
    def get(self):
        """Downloads a file."""
        path = self.request_path

        resource = Resource.get_by_path(path)

        if not resource:
            return self.response.set_status(404, "Not Found")

        if resource.is_collection:
            template_values = {
                'path':
                path,
                'prefix':
                self._prefix,
                'resources': [
                    child for child in resource.children
                    if not child.display_name.startswith('.')
                ]
            }

            template_path = os.path.join(os.path.dirname(__file__),
                                         'templates/collection.html')
            self.response.out.write(
                template.render(template_path, template_values))
        else:
            # deliver the file data
            self.response.headers[
                'Content-Type'] = resource.content_type_or_default
            self.response.out.write(resource.data.blob)
예제 #31
0
    def payoutResources(camps):
        """ Grabs all of the camp type structures and gives resources to owners."""
        # TODO: If this starts to become costly, look into optimizing this via
        # Keys-only queries.

        logging.info('called it')

        # map the players that need to updated into a cache.
        players_to_update = {}
        # Map the resources that need to be updated into a cache.
        resources_to_update = {}

        for camp in camps:
            logging.info('camp: ' + str(camp))
            # TODO: Add some contested area checking.
            # TODO: Add error checking on all of this.
            # Get the player who owns camp, first from the cache, then the store.
            owner_key = camp.owner_key
            owner = None
            if owner_key in players_to_update:
                owner = players_to_update[owner_key]
            else:
                owner = camp.owner_key.get()
            tile_resource = camp.harvesting_camp_data.tile_resource_key.get()
            template_key = tile_resource.resource_template

            # If the player already has the resource, up the quantity.
            for resource_key in owner.resources:
                # Get the resource from the cache first, otherwise get it from store.
                resource = None
                if resource_key in resources_to_update:
                    logging.info('getting resource from dict')
                    resource = resources_to_update[resource_key]
                else:
                    logging.info('getting resource from resource_key: ' +
                                 str(resource_key))
                    resource = resource_key.get()
                if not resource:
                    logging.error('Resource is None. Key is: ' +
                                  str(resource_key))
                    continue
                if resource.resource_template == template_key:
                    resource.quantity += determineHarvestRate(tile_resource)
                    resources_to_update[resource_key] = resource
                    # In python, if you break a for loop, the else does not trigger.
                    # This is the for/else logic...
                    # http://book.pythontips.com/en/latest/for_-_else.html
                    break
            # If the player doesn't already have the resource, create one.
            else:
                # TODO: Find out if there is a way to put_multi this resource.
                owner.resources.append(
                    Resource(
                        resource_template=template_key,
                        quantity=determineHarvestRate(tile_resource)).put())
                players_to_update[owner_key] = owner
        # Now all of the resources and players to update should be ready to be put.
        entities_to_put = players_to_update.values(
        ) + resources_to_update.values()
        ndb.put_multi(entities_to_put)
예제 #32
0
def add_from_csv(txt_file):
    f = open(txt_file, 'r')
    content = f.readlines()
    f.close()

    print len(content)

    # read each line in the tab-separated txt file and add the a resource for that line
    for line in content:
        line = line.strip().split('\t')

        if line[10]:
            description = line[10]
        else:
            description = "No description available"

        resource = Resource(subject=line[0],
                            topic=line[1],
                            name=line[2],
                            link=line[3],
                            instructor=line[4],
                            college=line[5],
                            lecture_notes=line[6] == "TRUE",
                            videos=line[7] == "TRUE",
                            assignments=line[8] == "TRUE",
                            book=line[9] == "TRUE",
                            description=description)

        db.session.add(resource)

    db.session.commit()
예제 #33
0
    def fetch(self, msg):
        qres = msg.get('qres')
        keep_file = msg.get('keep_file')
        keep_tempfile = msg.get('keep_tempfile')

        request = Request(self,
                          qres.url,
                          keep_file=keep_file,
                          keep_tempfile=keep_tempfile)
        request.fetch()

        res = Resource(
            url=qres.url,
            level=qres.level,
            context=qres.context,
            status_code=request.status_code,
            content_type=request.content_type,
            content_length=request.content_length,
        )
        msg = {
            'qres': qres,
            'res': res,
            'filepath': request.tempfile,
        }
        self.fetch_results.put(msg)
예제 #34
0
 def test_get_reservations(self):
     customer = Customer(name='Teemu Teekkari',
                         email='teemu.teekkari@aalto fi')
     service1 = Service(name='service1',
                        price=10.0,
                        duration=60.0,
                        description='service1 description')
     service2 = Service(name='service1',
                        price=10.0,
                        duration=60.0,
                        description='service1 description')
     self.db.save(service1)
     self.db.save(service2)
     added_reservations = []
     date = QtCore.QDate.currentDate()
     resource = Resource(name="Resource1", resource_type="ROOM")
     for x in range(8, 19):
         start_time = QtCore.QTime(x, 0)
         end_time = QtCore.QTime(x, 59)
         start = QtCore.QDateTime(date, start_time)
         end = QtCore.QDateTime(date, end_time)
         new_reservation = Reservation(customer=customer,
                                       resource=resource,
                                       start=start,
                                       end=end,
                                       services=[service1, service2])
         self.db.save(new_reservation)
         added_reservations.append(new_reservation)
     reservations = self.db.get_reservations()
     for i in range(len(reservations)):
         self.assertEqual(reservations[i], added_reservations[i])
예제 #35
0
 def accept(self):
     name = self.name.text()
     resource_type = self.resource_type.text()
     if name:
         if self.resource:
             resource = Resource(ID=self.resource.ID, name=name, resource_type=resource_type)
         else:
             resource = Resource(name=name, resource_type=resource_type)
         self.database.save(resource)
         self.information = MessageDialog("Resource added", icon=QtWidgets.QMessageBox.Information)
         self.information.show()
         self.close()
         self.parent.update()
     else:
         self.information = MessageDialog("Please fill in name.")
         self.information.show()
예제 #36
0
def api_create_resource(request, *, name, employee_id, email, introduce):
    check_admin(request)
    if not name or not name.strip():
        raise APIValueError('name', 'name cannot be empty.')
    if not employee_id or not employee_id.strip():
        raise APIValueError('employee_id', 'employee_id cannot be empty.')
    if not email or not email.strip():
        raise APIValueError('email', 'email cannot be empty.')
    if not introduce or not introduce.strip():
        raise APIValueError('introduce', 'introduce cannot be empty.')
    resource = Resource(name=name.strip(),
                        employee_id=employee_id.strip(),
                        image=request.__user__.image,
                        email=email.strip(),
                        introduce=introduce.strip())
    yield from resource.save()
    return resource
예제 #37
0
파일: views.py 프로젝트: sgml/simpledav
    def mkcol(self):
        """Creates a subdirectory, given an absolute path."""
        path = self.request_path
        parent_path = os.path.dirname(path)

        # check for duplicate
        if Resource.exists_with_path(path):
            return self.response.set_status(405, "Method Not Allowed")

        # fetch parent
        if parent_path:
            parent = Resource.get_by_path(parent_path)
            if not parent:
                return self.response.set_status(
                    409, "Conflict")  # must create parent folder first
        else:
            parent = Resource.root()

        logging.info("Creating dir at %s" % path)
        collection = Resource(path=path,
                              parent_resource=parent,
                              is_collection=True)
        collection.put()

        self.response.set_status(201, 'Created')
예제 #38
0
    def put(self):
        """Uploads a file."""
        path = self.request_path
        parent_path = os.path.dirname(path)

        # anything at this path already?
        existing_resource = Resource.get_by_path(path)

        if existing_resource:
            existing_resource.delete_recursive()

        # fetch parent
        if parent_path:
            parent = Resource.get_by_path(parent_path)
            if not parent or not parent.is_collection:
                return self.response.set_status(409, "Conflict")  # must create parent folder first
        else:
            parent = Resource.root()

        logging.info("Creating resource at %s" % path)
        data = ResourceData(blob=self.request.body)
        data.put()

        resource = Resource(path=path, parent_resource=parent, data=data)
        resource.content_length = len(self.request.body)
        resource.put()

        self.response.set_status(201, "Created")
예제 #39
0
    def post(self):
        resource_id = self.request.get('id')
        resource_name = self.request.get('name')
        start_time = time(
            *map(int,
                 self.request.get('available_start_time').split(':')))
        start_time = datetime.combine(datetime.today(), start_time)
        duration = int(self.request.get('duration'))
        resource = Resource.query(Resource.id == resource_id).get()
        end_time = start_time + timedelta(minutes=duration)
        # check time format and availability
        has_error = False
        msg = ''

        # error check
        if end_time < start_time:
            has_error = True
            msg = 'Error, wrong format of start time or duration. Please return to former page to enter correctly.'

        elif resource.available_start_time > start_time or \
            resource.available_end_time < end_time:
            has_error = True
            msg = 'Error, resource not available during the selected period. Please return to former page to enter another time period.'

        else:
            reservations = Reservation.query(
                Reservation.resource_id == resource_id).fetch()
            for r in reservations:
                if not (end_time <= r.start_time or start_time >= r.end_time):
                    has_error = True
                    msg = 'Error, reservation conflict. Please return to former page to enter another time period.'

        if has_error:
            template = JINJA_ENVIRONMENT.get_template('newReservation.html')
            template_values = {'msg': msg}
            self.response.write(template.render(template_values))

        else:
            # add reservation if no error
            reservation = Reservation()
            reservation.id = str(uuid.uuid4())
            reservation.user = str(users.get_current_user().email())
            reservation.start_time = start_time
            reservation.duration = duration
            reservation.end_time = end_time
            reservation.resource_id = resource_id
            reservation.resource_name = resource_name
            reservation.put()
            resource.last_reservation_time = datetime.now() - timedelta(
                minutes=300)
            resource.num_reserved += 1
            resource.put()
            t.sleep(1)

            send_mail(resource, reservation)

            self.redirect('/')
예제 #40
0
 def get_resources(self):
     # TODO(Ling): Implement multiple pages.
     # TODO(Ling): Add KeyProperty to Tag model.
     all_resources = Resource.query().fetch()
     data = []
     for resource in all_resources:
         if self.tag.key in resource.tag_keys:
             data.append(resource)
     return data
예제 #41
0
def saveResource(request):
    #print("Test")
    resources = Resource.objects.all()
    #print(resources.length)
    resoID = resources.__len__() + 1
    #print(ResoID)
    name = request.GET["Name"]
    lat = request.GET["Lat"]
    lng = request.GET["Lng"]
    skills = request.GET["Skills"]

    newResource = Resource(ResoID=resoID,
                           Name=name,
                           Lat=lat,
                           Lng=lng,
                           Skills=skills)
    newResource.save()
    return HttpResponse("OK")
예제 #42
0
    def delete(self):
        """Deletes a resource at a url. If it's a collection, it must be empty."""
        path = self.request_path
        resource = Resource.get_by_path(path)

        if not resource:
            return self.response.set_status(404, "Not Found")

        resource.delete_recursive()
예제 #43
0
파일: tests.py 프로젝트: trawick/edurepo
    def test_duplicate_submission(self):
        lo1 = self.lo0
        url = 'http://www.google.com/'
        r = Resource(objective=lo1, url=url)
        r.full_clean()
        r.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        rs.full_clean()
        rs.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        self.assertRaises(ValidationError, lambda: rs.full_clean())

        # can't vote on a resource you submitted
        rs = ResourceSubmission(user=self.u1, resource=r, type='v')

        with self.assertRaisesRegexp(ValidationError, 'cannot vote.*submitted'):
            rs.full_clean()
예제 #44
0
파일: views.py 프로젝트: jefree/followeb
def addSubscriptionView(request):
	
	if request.method == 'POST':
		
		url =  request.POST['url']
		title = request.POST['title']
		description = request.POST['description']
		
		html_request = requests.get(url)
		url=html_request.url
		code_html = html_request.text

		resource = Resource(url=url, title=title, description=description)
		resource.save()

		tasks.create_new_version(resource, code_html ,1)
		
		return redirect('/followeb/')

	return HttpResponseBadRequest('Bad Request')
예제 #45
0
파일: views.py 프로젝트: mkramb/errorify
def bundles_resources_save(request, uuid):
    response = []

    for uploaded_file in request.FILES.values():
        if is_text_file(uploaded_file):
            resource = Resource(bundle=Bundle.api.get(uuid, request.user))
            resource.source = ''.join([ chunk for chunk in uploaded_file.chunks() ])
            resource.save()
            response.append(resource.uuid)

    if len(response) and not len(messages.get_messages(request)):
        messages.success(request, _(u'New resources uploaded.'))

    if not request.is_ajax():
        return redirect('app_bundles_detail', uuid)

    return HttpResponse(
        simplejson.dumps(response),
        mimetype='application/json'
    )
예제 #46
0
    def move(self):
        """Moves a resource from one path to another."""
        path = self.request_path
        resource = Resource.get_by_path(path)

        if not resource:
            return self.response.set_status(404, "Not Found")

        overwrite = self.request.headers.get("Overwrite", "T")
        destination = self.request.headers["Destination"]  # exception if not present

        destination_path = self.url_to_path(urlparse(destination).path)
        parent_path = os.path.dirname(destination_path)

        if path == destination_path:
            return self.response.set_status(403, "Forbidden")

        # anything at this path already?
        existing_resource = Resource.get_by_path(destination_path)

        if existing_resource:
            if overwrite == "T":
                existing_resource.delete_recursive()
            else:
                return self.response.set_status(412, "Precondition Failed")

        # fetch parent
        if parent_path:
            parent = Resource.get_by_path(parent_path)
            if not parent or not parent.is_collection:
                return self.response.set_status(409, "Conflict")  # must create parent folder first
        else:
            parent = Resource.root()

        resource.parent_resource = parent  # reparent this node
        resource.move_to_path(destination_path)

        self.response.set_status(204 if existing_resource else 201)
예제 #47
0
def request_json(**kwargs):
    if request.json:
        global r
        r = Resource()
        for k, v in kwargs.iteritems():
            r.rid = v  # Now automatically updated
        r.title = request.json['title']
        r.link = str(request.json['link'])
        # Insert Tags to Tags Object
        r.tags = []
        for item in request.json['tags']:
            t = Tags()
            t.tag_name = item['tag_name']
            r.tags.append(t)
        r.description = request.json['description']
        r.save(upsert=True)
예제 #48
0
def get_content(request, uri):
    try:
        resource = Resource.objects.get(uri=uri)
        if not resource.retrieved:
            result = resource.source.getManager().get(uri)

    except ObjectDoesNotExist:
        result = None
        for repository in Repository.objects.all():
            result = repository.getManager().get(uri)
            if result is not None:
                resource = Resource(
                    uri = uri,
                    source = repository,
                )
                resource.save()
                break

    if not resource.retrieved and result is not None:
        resource = attach_content(resource, result)

    response = HttpResponse(resource.content, content_type=resource.contentType)
    response['Content-Disposition'] = 'attachment; filename="'+resource.title+'"'
    return response
예제 #49
0
    def get(self):
        """Downloads a file."""
        path = self.request_path

        resource = Resource.get_by_path(path)

        if not resource:
            return self.response.set_status(404, "Not Found")

        if resource.is_collection:
            template_values = {
                "path": path,
                "prefix": self._prefix,
                "resources": [child for child in resource.children if not child.display_name.startswith(".")],
            }

            template_path = os.path.join(os.path.dirname(__file__), "templates/collection.html")
            self.response.out.write(template.render(template_path, template_values))
        else:
            # deliver the file data
            self.response.headers["Content-Type"] = resource.content_type_or_default
            self.response.out.write(resource.data.blob)
예제 #50
0
파일: tests.py 프로젝트: trawick/edurepo
 def setUp(self):
     self.u1_password = '******'
     self.u1 = User.objects.create_user(username='******', email='*****@*****.**',
                                        password=self.u1_password)
     user = authenticate(username=self.u1.username, password=self.u1_password)
     assert user
     self.u2 = User.objects.create_user(username='******', email='*****@*****.**')
     self.cc0 = CourseCategory(id='TESTNA', description='Test Non-Academic')
     self.cc0.full_clean()
     self.cc0.save()
     self.c0 = Course(id='Class00', cat=self.cc0, description='Class00Desc')
     self.c0.full_clean()
     self.c0.save()
     self.lo0 = LearningObjective(id=dummy_objective_id, course=self.c0, description=dummy_objective_description)
     self.lo0.full_clean()
     self.lo0.save()
     self.res1 = Resource(objective=self.lo0, url=dummy_resource_url)
     self.res1.full_clean()
     self.res1.save()
     self.ressub1 = ResourceSubmission(user=self.u1, resource=self.res1,
                                       type='v', comment=dummy_comment)
     self.ressub1.full_clean()
     self.ressub1.save()
예제 #51
0
파일: views.py 프로젝트: fhfengzhiyong/fan
def upload_file():
    if request.method == "POST":
        id = request.args["id"]
        if id:
            f = request.files["file"]
            resource = Resource()
            resource.id = uuid.uuid4().__str__()
            resource.bs_id = id
            name = f.filename
            extension = getFileExt(name)
            resource.extension = extension
            resource.file_name = (resource.id) + "." + extension
            resource.real_name = name
            session = DBSession()
            session.add(resource)
            session.commit()
            session.close()
            f.save(current_app.config.get("UPLOADED_FILE") + resource.file_name)
    return jsonify(code=0)
예제 #52
0
    def mkcol(self):
        """Creates a subdirectory, given an absolute path."""
        path = self.request_path
        parent_path = os.path.dirname(path)

        # check for duplicate
        if Resource.exists_with_path(path):
            return self.response.set_status(405, "Method Not Allowed")

        # fetch parent
        if parent_path:
            parent = Resource.get_by_path(parent_path)
            if not parent:
                return self.response.set_status(409, "Conflict")  # must create parent folder first
        else:
            parent = Resource.root()

        logging.info("Creating dir at %s" % path)
        collection = Resource(path=path, parent_resource=parent, is_collection=True)
        collection.put()

        self.response.set_status(201, "Created")
예제 #53
0
def get_or_create_resource(session, rad_record, lazy=True, create_categories=True):
    """
    Checks to see if a resource already exists in the database
    and adds it if it does not exist (or is forced to by use of
    the lazy argument).

    Args: 
        session: The current database session. 
        rad_record: The RadRecord to be added.
        lazy: If false, forces the record to be added even if it is a duplicate. 
            Defaults to true.
        create_categories: If true, will create categories if they don't already exist.
            If false, will skip over listed categories that don't already exist. 
            Defaults to true.

    Returns:
        Two values. The first value is a boolean
        indicating if a new record was created. The second
        value will be the created/updated model.
    """
    # Just create a new record always if we're lazy-loading. This avoids
    # weirdness in which we're partially updating an item.
    if lazy:
        new_record = True
        record = Resource(name=rad_record.name.strip())
        session.add(record)
    else:
        new_record, record = get_or_create(session, Resource, name=rad_record.name.strip())

    record.last_updated = datetime.utcnow()

    if new_record:
        record.date_created = datetime.utcnow()

    if new_record or not lazy:

        # See if we have just a normal address field - if not,
        # manually construct one by joining all available
        # fields with commas
        new_address = ''
        if hasattr(rad_record, 'address') and \
            rad_record.address is not None and \
            rad_record.address != '' and \
            not rad_record.address.isspace():

            new_address = rad_record.address.strip()
        else:
            new_address = ", ".join(a.strip() for a in [rad_record.street,
                                    rad_record.city, rad_record.state,
                                    rad_record.zipcode, rad_record.country]
                                    if a is not None and a != '' and not a.isspace())

        # Address issue 131 - if we're updating an existing
        # record, and are changing the address (using a lowercase comparison),
        # invalidate the existing geocoding information.
        if not new_record and \
            record.address is not None and \
            record.address.lower() != new_address.lower():
            record.latitude = None
            record.longitude = None
            record.location = None

        # Now set the new address
        if new_address != '' and not new_address.isspace():
            record.address = new_address
        else:
            record.address = None

        # Try to parse out the date_verified field if it's provided
        if rad_record.date_verified is not None and \
            len(rad_record.date_verified) > 0 and \
            not rad_record.date_verified.isspace():
            # Try to parse it out using 'YYYY-MM-DD'
            try:
                record.date_verified = datetime.strptime(rad_record.date_verified, 
                    '%Y-%m-%d').date()
            except ValueError:
                # Parsing error, clear it out
                record.date_verified = None
        else:
            # Not provided - clear it out
            record.date_verified = None

        # Copy over all the other fields verbatim
        record.organization = rad_record.organization
        record.description = rad_record.description

        record.email = rad_record.email
        record.phone = rad_record.phone
        record.fax = rad_record.fax
        record.url = rad_record.url
        record.hours = rad_record.hours

        record.source = rad_record.source
        record.npi = rad_record.npi
        record.notes = rad_record.notes
        
        record.visible = rad_record.visible

        # Do we have a list of category names?
        # Failing that, do we have a single category name?
        if hasattr(rad_record, 'category_names') and \
            rad_record.category_names is not None and \
            len(rad_record.category_names) > 0:

            # Use the list of category names
            try_add_categories(session, record, rad_record.category_names, create_categories)

        elif hasattr(rad_record, 'category_name') and \
            rad_record.category_name is not None and \
            not rad_record.category_name.isspace():

            # Use the single category name
            try_add_categories(session, record, [rad_record.category_name], create_categories)

        # Do we have a list of population tags?
        if hasattr(rad_record, 'population_tags') and \
            rad_record.population_tags is not None and \
            len(rad_record.population_tags) > 0:

            try_add_populations(session, record, rad_record.population_tags)

        session.add(record)

        # Flush the session because otherwise we won't pick up
        # duplicates with UNIQUE constraints (such as in category names) 
        # until we get an error trying to commit such duplicates
        # (which is bad)
        session.flush()

    return new_record, record
예제 #54
0
  def create(self, request):
    term = ''
    guid = None
    email = ''
    leaseid = ''
    try:
        if request.POST.get('term') and request.POST['term']:
           term = request.POST['term'] 
        if request.POST.get('email') and request.POST['email']:
           email = request.POST['email']
        if request.POST.get('guid') and request.POST['guid']:
           guid = request.POST['guid']
        if request.POST.get('leaseid') and request.POST['leaseid']:
           leaseid = request.POST['leaseid']
        if term==None or email==None or int(term) < 0 or int(term) > 365:
            return HttpResponse(json.dumps({'status':'error', 
                   'msg':'Term and email required.Term has to be less than 365 days'}), status=400)
        if guid == None:
              resource = Resource()
              #guid = uuid.uuid4
              #resource.guid = guid
              resource.callbackurl = ''
              resource.callbackauth = ''
              resource.save()
              print('resource saved')
              guid = resource.guid
        try:
              print('all_resources='+repr(Resource.objects.all()))
              resource = Resource.objects.get(guid=guid)
        except Resource.DoesNotExist:
              resource = None
              return HttpResponse(json.dumps({
               'status': 'error',
               'msg': 'Resource could not be created',
               'lease':'',
               }), status=500)
        try:
            subscriber = Subscriber.objects.get(email=email)
        except Subscriber.DoesNotExist:
            subscriber = None
            pass
        if subscriber == None:
           subscriber = Subscriber()
           subscriber.email = email
           subscriber.save()
        subscriber = Subscriber.objects.get(email=email)

        if not leaseid:
            lease = ResourceLease()
            print('lease.resource='+repr(resource))
            lease.resource = resource
            lease.subscriber = subscriber
            lease.term = int(term)
            from datetime import timedelta
            lease.expires = datetime.datetime.now() + timedelta(days=int(term))
            lease.status = 'A'
            lease.save()
            print('lease.id'+str(lease.id))
            leaseid = str(lease.id)
        try:
            lease = ResourceLease.objects.get(id=leaseid)
        except ResourceLease.DoesNotExist:
            lease = None
            return HttpResponse(json.dumps({
               'status': 'error',
               'msg': 'Resource could not be created',
               'lease':'',
               }), status=500)
        queryset = ResourceSubscribers.objects.filter(resource=resource)
        recipients = []
        for item in queryset:
            recipients.append(item.subscriber.email)
        if email not in recipients:
            recipients.append(email)
            ressub = ResourceSubscribers()
            ressub.resource = resource
            ressub.subscriber = subscriber
            ressub.save()
        return HttpResponse(json.dumps({'status':'success', 'guid':str(guid), 'leaseid':str(leaseid), 'subscribers':recipients}))
    except:
        import sys
        exc_type, exc_value, exc_traceback = sys.exc_info()
        import traceback
        msg = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
        logger.warn(msg)
        return HttpResponse(json.dumps({
               'status': 'error',
               'msg': msg,
               'lease':'',
               }), status=500)
    finally:
        pass
예제 #55
0
 def test_duplicate_resource(self):
     r = Resource()
     r.name = 'resource1'
     with self.assertRaises(IntegrityError) as e:
         print "Testing for exception..."
         r.save()
예제 #56
0
def main(show_progress, *args, **kwargs):

    # Create a new fetch index for the records fetched.
    last_fetch_index = Dataset.select(fn.Max(Dataset.fetch_index)).scalar() or 0
    fetch_index = last_fetch_index + 1

    # Set up progress bar
    if show_progress:
        progress_bar = ProgressBar(widgets=[
            'Progress: ', Percentage(),
            ' ', Bar(marker=RotatingMarker()),
            ' ', ETA(),
            ' Fetched metadata for ', Counter(), ' datasets.'
        ])
        progress_bar.start()

    # Fetch all pages of datasets
    datasets_fetched = 0
    last_page = False
    while not last_page:

        params = DEFAULT_PARAMS.copy()
        params['start'] = datasets_fetched
        resp = make_request(default_requests_session.get, URL, params=params).json()

        if not resp['success']:
            logging.error("Request to URL %s was unsuccessful", URL)

        result = resp['result']
        num_datasets = len(result['results'])
        datasets_fetched += num_datasets

        if show_progress:
            # We can finally initialize the total number of datasets expected
            # only after we get the first round of results.
            progress_bar.maxval = result['count']
            progress_bar.update(datasets_fetched)

        for dataset in result['results']:

            dataset_record = Dataset.create(
                dataset_id=dataset['id'],
                title=trim_char_data(dataset['title']),
                license_title=trim_char_data(['license_title']),
                fetch_index=fetch_index,
            )

            for resource in dataset['resources']:
                if resource['format'] == DATA_FORMAT:
                    Resource.create(
                        resource_id=resource['id'],
                        dataset=dataset_record,
                        format=resource['format'],
                        url=resource['url'],
                    )

        time.sleep(REQUEST_DELAY)  # enforce a pause between each fetch to be respectful to API
        last_page = datasets_fetched >= result['count']

    if show_progress:
        progress_bar.finish()
예제 #57
0
 def propfind(self):
     path = self.request_path
     self.propfind_resource(Resource.get_by_path(path))
예제 #58
0
파일: tests.py 프로젝트: trawick/edurepo
class BasicTests(TestCase):

    def setUp(self):
        self.u1_password = '******'
        self.u1 = User.objects.create_user(username='******', email='*****@*****.**',
                                           password=self.u1_password)
        user = authenticate(username=self.u1.username, password=self.u1_password)
        assert user
        self.u2 = User.objects.create_user(username='******', email='*****@*****.**')
        self.cc0 = CourseCategory(id='TESTNA', description='Test Non-Academic')
        self.cc0.full_clean()
        self.cc0.save()
        self.c0 = Course(id='Class00', cat=self.cc0, description='Class00Desc')
        self.c0.full_clean()
        self.c0.save()
        self.lo0 = LearningObjective(id=dummy_objective_id, course=self.c0, description=dummy_objective_description)
        self.lo0.full_clean()
        self.lo0.save()
        self.res1 = Resource(objective=self.lo0, url=dummy_resource_url)
        self.res1.full_clean()
        self.res1.save()
        self.ressub1 = ResourceSubmission(user=self.u1, resource=self.res1,
                                          type='v', comment=dummy_comment)
        self.ressub1.full_clean()
        self.ressub1.save()

    def test_1(self):
        """Basic creation of Resource, disallowing same URL+objective combination"""
        lo1 = self.lo0
        url1 = 'http://127.0.0.1/foo.html'
        r1 = Resource(objective=lo1, url=url1)
        self.assertEquals(r1.votes, 0)
        self.assertEquals(r1.inappropriate_flags, 0)
        r1.save()
        when_added = r1.when_added
        now = datetime.datetime.now()
        when_added = when_added.replace(tzinfo=None)
        self.assertTrue(now - when_added < datetime.timedelta(seconds=5))

        r2 = Resource(objective=lo1, url=url1)
        self.assertRaises(IntegrityError, lambda: r2.save())

    def test_bad_url(self):
        bad_urls = ('ftp://*****:*****@example.com/',
                    'http://*****:*****@example.com/',
                    'http://foo',
                    )
        for (bad_url, i) in zip(bad_urls, range(len(bad_urls))):
            r = Resource(objective=self.lo0, url=bad_url)
            self.assertRaises(ValidationError, lambda: r.full_clean())

    def test_duplicate_submission(self):
        lo1 = self.lo0
        url = 'http://www.google.com/'
        r = Resource(objective=lo1, url=url)
        r.full_clean()
        r.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        rs.full_clean()
        rs.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        self.assertRaises(ValidationError, lambda: rs.full_clean())

        # can't vote on a resource you submitted
        rs = ResourceSubmission(user=self.u1, resource=r, type='v')

        with self.assertRaisesRegexp(ValidationError, 'cannot vote.*submitted'):
            rs.full_clean()

        # okay to flag a resource you submitted as inappropriate

    def test_strings(self):
        lo2 = self.lo0
        url = 'http://www.google.com/'
        r = Resource(objective=lo2, url=url)
        r.full_clean()
        r.save()

        rs = ResourceSubmission(user=self.u1, resource=r, type='c')
        rs.full_clean()
        rs.save()

        self.assertTrue(' created ' in str(rs))
        self.assertEquals(rs.type_str(), 'Created')

        rs = ResourceSubmission(user=self.u1, resource=r, type='f')
        rs.full_clean()
        rs.save()

        self.assertTrue(' flagged ' in str(rs))
        self.assertEquals(rs.type_str(), 'Inappropriate')

        rs = ResourceSubmission(user=self.u2, resource=r, type='v')
        rs.full_clean()
        rs.save()

        self.assertTrue(' voted on ' in str(rs))
        self.assertEquals(rs.type_str(), 'Up-voted')

    def test_index(self):
        response = self.client.get("/resources/")
        self.assertContains(response, self.res1.url)

    def test_detail(self):
        detail_url = "/resources/" + str(self.res1.id) + "/"
        response = self.client.get(detail_url)
        self.assertContains(response, self.res1.url)

    def test_create_resource(self):
        login = self.client.login(username=self.u1.username, password=self.u1_password)
        self.assertTrue(login)
        create_url = '/resources/create/?objective=%s' % dummy_objective_id
        response = self.client.get(create_url, follow=True)
        self.assertContains(response, 'Submit a resource')
        self.assertContains(response, dummy_objective_id)
        response = self.client.post(create_url, {'url': 'http://www.example.com/',
                                                 'objective': dummy_objective_id}, follow=True)
        self.assertNotContains(response, 'form-group has-error')
        self.assertContains(response, 'http://www.example.com/')
        self.assertContains(response, dummy_objective_id)

    def test_comment_on_resource(self):
        login = self.client.login(username=self.u1.username, password=self.u1_password)
        self.assertTrue(login)
        comment_url = "/resources/" + str(self.res1.id) + "/comment/"
        response = self.client.get(comment_url, follow=True)
        self.assertContains(response, 'Comment on a resource')
        self.assertContains(response, self.res1.url)
        response = self.client.post(comment_url, {'resource': str(self.res1.id),
                                                  'type': 'v'}, follow=True)
        self.assertNotContains(response, 'form-group has-error')

    # This accesses the API endpoint in the test instance, not the one in
    # the real instance.
    def test_resource_api(self):
        response = self.client.get('/resources/api/resource/')
        self.assertContains(response, dummy_resource_url)
        objects = json.loads(response.content)['objects']
        for o in objects:
            response = self.client.get(o['objective'])
            self.assertContains(response, '/repo/api/learningobjective/')
            response = self.client.get(o['resource_uri'])
            self.assertContains(response, '/resources/api/resource/' + str(o['id']))

    def test_submission_api(self):
        response = self.client.get('/resources/api/resourcesubmission/')
        self.assertContains(response, dummy_comment)
        objects = json.loads(response.content)['objects']
        for o in objects:
            for k in ('resource', 'resource_uri'):
                response = self.client.get(o[k])
                self.assertEqual(response.status_code, 200)

    def test_rv_strings(self):
        now = datetime.datetime.utcnow().replace(tzinfo=utc)
        rv = ResourceVerification(url='http://127.0.0.1/abcd/',
                                  last_failure=now - datetime.timedelta(seconds=1))
        rv.full_clean()
        rv.save()
        self.assertEquals(rv.status_char(), 'I')
        self.assertEquals(rv.status_str(), 'Invalid')
        rv.last_success = now
        self.assertEquals(rv.status_char(), 'V')
        self.assertEquals(rv.status_str(), 'Valid')
        self.assertEquals(str(rv), 'Valid:' + rv.url)
        rv.last_failure = now + datetime.timedelta(seconds=1)
        self.assertEquals(rv.status_char(), 'I')
        rv.last_failure = None
        self.assertEquals(rv.status_char(), 'V')
예제 #59
0
def add():
    """add resource"""
    if not g.user.is_authenticated():
        return render_template('add.html')
    if request.method == 'GET':
        return render_template('add.html')
    resource_type = request.form['resource_type']
    tags = request.form.getlist('tags')
    url = request.form['url'].strip()
    resources_to_add = []

    sniffed_resources = sniff_test_resource(CONFIG, resource_type, url)

    if not sniffed_resources:
        msg = gettext("No resources detected")
        LOGGER.exception()
        flash(msg, 'danger')

    for (resource_type, resource_url,
         title, success, response_time,
         message, start_time, resource_tags,) in sniffed_resources:

        # sniffed_resources may return list of resource
        # types different from initial one
        # so we need to test each row separately
        resource = Resource.query.filter_by(resource_type=resource_type,
                                            url=url).first()
        if resource is not None:
            msg = gettext('Service already registered')
            flash('%s (%s, %s)' % (msg, resource_type, url), 'danger')

            if len(sniffed_resources) == 1 and 'resource_type' in request.args:
                return redirect(url_for('add', lang=g.current_lang))

        tags_to_add = []
        for tag in chain(tags, resource_tags):
            tag_obj = tag
            if not isinstance(tag, Tag):
                tag_obj = Tag.query.filter_by(name=tag).first()
                if tag_obj is None:
                    tag_obj = Tag(name=tag)
            tags_to_add.append(tag_obj)

        resource_to_add = Resource(current_user,
                                   resource_type,
                                   title,
                                   resource_url,
                                   tags=tags_to_add)

        resources_to_add.append(resource_to_add)
        probe_to_add = None
        checks_to_add = []

        # Always add a default Probe and Check(s)
        # from the GHC_PROBE_DEFAULTS conf
        if resource_type in CONFIG['GHC_PROBE_DEFAULTS']:
            resource_settings = CONFIG['GHC_PROBE_DEFAULTS'][resource_type]
            probe_class = resource_settings['probe_class']
            if probe_class:
                # Add the default Probe
                probe_obj = Factory.create_obj(probe_class)
                probe_to_add = ProbeVars(
                    resource_to_add, probe_class,
                    probe_obj.get_default_parameter_values())

                # Add optional default (parameterized)
                # Checks to add to this Probe
                checks_info = probe_obj.get_checks_info()
                checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL']
                for check_class in checks_info:
                    check_param_info = checks_param_info[check_class]
                    if 'default' in checks_info[check_class]:
                        if checks_info[check_class]['default']:
                            # Filter out params for Check with fixed values
                            param_defs = check_param_info['PARAM_DEFS']
                            param_vals = {}
                            for param in param_defs:
                                if param_defs[param]['value']:
                                    param_vals[param] =\
                                        param_defs[param]['value']
                            check_vars = CheckVars(
                                probe_to_add, check_class, param_vals)
                            checks_to_add.append(check_vars)

        result = run_test_resource(resource_to_add)

        run_to_add = Run(resource_to_add, result)

        DB.session.add(resource_to_add)
        # prepopulate notifications for current user
        resource_to_add.set_recipients('email', [g.user.email])

        if probe_to_add:
            DB.session.add(probe_to_add)
        for check_to_add in checks_to_add:
            DB.session.add(check_to_add)
            DB.session.add(run_to_add)

    try:
        DB.session.commit()
        msg = gettext('Services registered')
        flash('%s (%s, %s)' % (msg, resource_type, url), 'success')
    except Exception as err:
        DB.session.rollback()
        flash(str(err), 'danger')
        return redirect(url_for('home', lang=g.current_lang))

    if len(resources_to_add) == 1:
        return edit_resource(resources_to_add[0].identifier)
    return redirect(url_for('home', lang=g.current_lang))