4
\$\begingroup\$

I am a newbie and I am using basic HTML to render and check. No JSON at the moment. With that said, I’m having a hard time conceptualizing organization of a group of matching series (e.g. a series that falls under a given number, say 1) into a single link that expands into the subseries (say, 1a, 1b, 1c...) for a detailed list; this second portion would be found on a separate page.

Ideally (here is a sketch):

HTML page showing all the series:

<a href= ‘MicroSeries 1’>

This link opens to a separate HTML page with the matching subseries:

Subseries 1a, 1b, 1c, 1d

<a href= 'MicroSeries 2'>

Same logic as above.

I think my idea is the way to attack the problem, but please feel free to recommend a better approach. I have a former question that reveals more of what I initially created: link. However, I believe I may have attacked the issue incorrectly. I also think I've confused myself pretty bad.

As of right now, having a single link that opens up to the subseries does not exist for each microseries without manual code (as you one can tell below).

Using: Python 2.7, Pyramid (JINJA2 templates), SQLAlchemy

Route (init.py):

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

View Code:

@view_config(route_name='assessments', request_method='GET', renderer='templates/unique_assessments.jinja2')
def view_unique_microseries_group(request):
 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_subseries'))
 return {'logged_in': logged_in_userid, 'unique_assessments': unique_assessments}
@view_config(route_name='view_subseries', request_method='GET', renderer='templates/assessments.jinja2')
def view_microseries_subseries(request):
 all_assessments = api.retrieve_assessments() #all assessments in a list
 series1 = []
 series2 = []
 series3 = []
 series4 = []
 series5 = []
 for x in all_assessments:
 if x.microseries ==1:
 series1.append(x)
 #print 'series1 ', series1
 elif x.microseries ==2:
 series2.append(x)
 #print 'series2', series2
 elif x.microseries ==3:
 series3.append(x)
 #print 'series3', series3
 elif x.microseries ==4:
 series4.append(x)
 #print 'series4', series4
 elif x.microseries ==5:
 series5.append(x)
 #print 'series5', series5
 else:
 raise HTTPNotFound
 return {'logged_in': logged_in_userid, 'series1': series1, 'series2': series2, 'series3':series3, 'series4':series4, 'series5':series5, 'all_assessments': all_assessments}

HTML for showing all subseries (assessments.jinja2):

<div class="container">
 <table class="table table-striped">
 <thead class='tablename'>
 <h2>Microseries subseries</h2>
 <tr>
 <td> </td>
 <td> SubSeries </td>
 </tr>
 </thead>
 <tbody>
 <tr>
 {% for a in series1 %} # needs to grab a matching subseries... maybe used hidden instead ? this does not work for each set
 {% if a in all_assessments %}
 <td><a href="{{ '/assessments/%s'%a.microseries|urlencode }}"><button type='button' class='btn outline btn-primary'>Play</button></a></td>
 <td>{{ a }}</td>
 </tr>
 {% endif %}
 {% endfor %}
 </tbody>
 </table>
</div>
asked Feb 18, 2016 at 20:36
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Especially when you're just starting out it would be actually good to use Python 3, but even if you're not doing that compatibility with it would be a good idea anyway, e.g. use print(x) instead of print x.

Next, the grouping code in view_microseries_subseries is clearly repetitive. Every time you encounter such a pattern where you create additional variables with a counter suffix (variable42 etc.) try to switch to a different representation like the dictionary in view_unique_microseries_group, or maybe a list of lists with direct addressing by index. I also don't get why specifically HTTPNotFound is raised.

With a bit of cleanup I'd suggest the following:

@view_config(route_name='view_subseries', request_method='GET', renderer='templates/assessments.jinja2')
def view_microseries_subseries(request):
 all_assessments = api.retrieve_assessments()
 series = [[] for _ in xrange(5)]
 for x in all_assessments:
 if not 1 <= x.microseries <= 5:
 raise HTTPNotFound
 series[x.microseries].append(x)
 return {'logged_in': logged_in_userid, 'series': series, 'all_assessments': all_assessments}

In the HTML, I don't quite understand why only series1 is used, while more than that is computed in the route handler.

Apart from that it looks okay to me.

answered Mar 1, 2016 at 21:11
\$\endgroup\$
1
  • \$\begingroup\$ Thank you ferada. I heard Python 3 was buggy so I didn't want to jump onto a language that is still being worked on. But it seems that Python 3 is not supported. I want to start using JSON and JS to check instead of static HTML as it feel limiting. \$\endgroup\$ Commented Mar 1, 2016 at 21:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.