在Pyramid

时间:2016-02-23 16:19:17

标签: python view sqlalchemy frontend pyramid

我正在尝试找出访问数据库中microseries类(表)中找到的所有匹配Assessment的最佳方法。我正在尝试创建一个链接,将用户发送给匹配microseries的用户。我是新手,想要更好地理解前端工程的细节。我也没有在Stacks上看到类似的问题。

我还没有使用JSON或XML,所以这是一个静态HTML进程。我目前有一些访问评估的路线,例如:

config.add_route('assessments', '/assessments')
config.add_route('assessment', '/assessments/{id:\d+}')

在实现在microseries表中查找匹配Assessment并将用户发送到包含这些匹配系列的新页面的方法时,我希望更好地理解这些内容: routes如何工作,尤其是在访问类的属性时,例如Assessment.microseries

View代码的目标是传达上述方法。 Pyramid linksPyramid on Routes以及URL Dispatch

使用:Python 2.7,SQLAlchemy,Pyramid

评估表:

class Assessment(Base):
    __tablename__ = 'assessments'

    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True)
    text = Column(String(2000))
    microseries = Column(Integer)
    # more code

    def __init__(self, name, text, user, video, categories, microseries):
            # more code

用于与评估进行交互的API基于CRUD创建,检索,更新和删除。

查看代码: 这不是我需要做的事情,因为我没有将用户发送到匹配系列的链接形式,例如对于Link1的所有子系列,Link1会将用户发送到GET的新视图:1a,1b,1c ....

@view_config(route_name='assessments', request_method='GET', renderer='templates/unique_assessments.jinja2', permission='create')
def view_unique_microseries_group(request):
    logged_in_userid  = authenticated_userid(request)
    if logged_in_userid is None:
        raise HTTPForbidden()
    all_assessments = api.retrieve_assessments() #all assessments in a list
    assessments_by_microseries = {} #dictonary
    for x in all_assessments:
        if x.microseries in assessments_by_microseries:
            print("Already seen this microseries: %s" % x.microseries)
        else:
            assessments_by_microseries[x.microseries] = x
    unique_assessments = sorted(assessments_by_microseries.values()) #.values() method to get the, err, values of the dict.
    print 'unique_assessments:', unique_assessments
    #a = HTTPSeeOther(location=request.route_url('view_microseries'))
    return {'logged_in': logged_in_userid, 'unique_assessments': unique_assessments} #need to send some kind of list that can be shown on the template to send a user to the appropriately matching set of microseries 

0 个答案:

没有答案