def _get_persons_in_categories(meeting_id, categories, person_type, **kwargs): from playhouse.postgres_ext import R from peewee import EmptyResultException page = kwargs.pop('page', 1) limited = kwargs.pop('limited', True) filters = {'meeting_flags_verified': '1'} fields = [R('id', 'id'), R('data', 'data'), R('category', 'category')] order_by = ('category', 'ASC') if person_type == 'person': persons_query = models.Person.select(fields).order_by(order_by) else: persons_query = models.MediaPerson.select(fields).order_by(order_by) if limited: offset = (int(page) - 1) * PER_PAGE persons_query = persons_query.limit(PER_PAGE).offset(offset) if person_type == 'person': persons_query = sugar.append_persons_query( persons_query, meeting_id, categories=categories, filters=filters) else: persons_query = sugar.append_media_query( persons_query, meeting_id, categories=categories) try: total = persons_query.count() except EmptyResultException: total = 0 return persons_query, total
def media(meeting_id, printout_type='verified'): meeting = sugar.get_object_or_404(Meeting, id=meeting_id) categ = flask.request.args.get('category') page = int(flask.request.args.get('page', 1)) additional_params = "" categs, categ_ids = get_categories(meeting, categ, stat='Media') if categ: additional_params += '&category=' + categ order_by = [("category", "ASC"), ("(t1.data -> 'personal_last_name')", "ASC"),] fields = [R("t2.category", "categ_id"), R("t1.data", "data")] persons_query = (MediaPerson.select(fields).order_by(*order_by)) if not getattr(flask.g, 'pdf_compatible', None): persons_query = persons_query.paginate(page, PER_PAGE) persons_query = sugar.append_media_query(persons_query, meeting_id, categories=categ_ids) count = persons_query.count() paginator = sugar.Paginator(per_page=PER_PAGE, page=page, count=count) page_info = { 'today': datetime.now(), 'title': 'List of media participants', 'meeting_description': _get_meeting_description(meeting, 'E'), 'url': 'printouts.media', 'type': printout_type, 'print_url': flask.url_for('printouts.print_task_celery', meeting_id=meeting.id, view_name='media', printout_type=printout_type), 'excel_url': flask.url_for('printouts.media_excel', meeting_id=meeting.id, printout_type=printout_type), } categories_map = dict(map(lambda x: (int(x[0]), x[1]), categs)) return { 'page_info': page_info, 'persons': persons_query, 'paginator': paginator, 'events': events, 'count': count, 'meeting': meeting, 'categories': categs, 'categories_map': categories_map, 'filter_category': categ, 'additional_params': additional_params, 'export_excel': True, }
def media_excel(meeting_id, printout_type='verified'): meeting = sugar.get_object_or_404(Meeting, id=meeting_id) order_by = [("category", "ASC"), ("(t1.data -> 'personal_last_name')", "ASC"),] fields = [R("t2.category", "categ_id"), R("t1.data", "data")] rows = [] header = ('Name', 'Email', 'Category', 'Media', 'Position') persons_query = (MediaPerson.select(fields).order_by(*order_by)) persons_query = sugar.append_media_query(persons_query, meeting_id) for person in persons_query: rows.append((u'{0} {1}'.format(person.decoded_data['personal_last_name'].upper(), person.decoded_data['personal_first_name']), person.decoded_data['personal_email'], person.category_model(meeting.id, person.categ_id).data['name_E'], person.decoded_data['media'], person.decoded_data['position'])) return flask.Response(sugar.generate_excel(header, rows), mimetype='application/vnd.ms-excel', headers={'Content-Disposition': 'attachment; filename=media.xls'})