def get(self): participant_categories = (Category.get_categories_for_meeting( Category.PARTICIPANT)) media_categories = Category.get_categories_for_meeting(Category.MEDIA) return render_template(self.template_name, participant_categories=participant_categories, media_categories=media_categories)
def post(self): custom_fields = (g.meeting.custom_fields.filter_by( custom_field_type=self.participant_type).order_by( CustomField.sort)) custom_fields = [ field for field in custom_fields if field.field_type.code != CustomField.EVENT ] meeting_categories = [ c.title.english.lower() for c in Category.get_categories_for_meeting(self.participant_type) ] countries = [ country[1].encode('utf-8') for country in get_all_countries() ] file_name = 'import_{}_list_{}.xlsx'.format(self.participant_type, g.meeting.acronym) file_path = app.config['UPLOADED_PRINTOUTS_DEST'] / file_name generate_import_excel(custom_fields, file_path, CustomField, meeting_categories, countries) return send_from_directory(app.config['UPLOADED_PRINTOUTS_DEST'], file_name, as_attachment=True)
def _clone_categories(self, meeting, categories): for category in categories: with db.session.no_autoflush: clone = copy_attributes(Category(), category) clone.tags = category.tags clone.title = copy_attributes(Translation(), category.title) clone.meeting = meeting db.session.add(clone) db.session.flush()
def _save_categories(self, meeting): for category_default in meeting.meeting_type.default_categories: category = copy_attributes(Category(), category_default) category.title = copy_attributes(Translation(), category_default.title) category.tags = category_default.tags category.meeting = meeting db.session.add(category) db.session.flush()
def get(self): participant_categories = (Category.get_categories_for_meeting( Category.PARTICIPANT)) media_categories = Category.get_categories_for_meeting(Category.MEDIA) total_delegates = sum( category.participants.filter_by(deleted=False).count() for category in participant_categories) return render_template( self.template_name, participant_categories=participant_categories, media_categories=media_categories, total_delegates=total_delegates, sex_field=(g.meeting.custom_fields.filter_by(slug='sex').scalar()), female_delegates=(g.meeting.participants.filter_by( sex=Participant.SEX_CHOICES[0][0]).count()), male_delegates=(g.meeting.participants.filter_by( sex=Participant.SEX_CHOICES[1][0]).count()), neutral_delegates=(g.meeting.participants.filter_by( sex=Participant.SEX_CHOICES[2][0]).count()))
def get(self): custom_field_id = request.args['id'] cf = CustomField.query.get_or_404(custom_field_id) if cf.field_type == CustomField.CATEGORY: query = Category.get_categories_for_meeting(Category.PARTICIPANT) return jsonify(data=[(i.id, unicode(i)) for i in query]) if cf.field_type == CustomField.COUNTRY: return jsonify(data=get_all_countries()) if cf.field_type == CustomField.CHECKBOX: return jsonify(data=ConditionForm.CHECKBOX_VALUES) if cf.field_type == CustomField.SELECT: query = cf.choices.all() return jsonify(data=[(unicode(i), unicode(i)) for i in query]) return abort(400)
def get(self, rule_type): custom_field_id = request.args['id'] cf = CustomField.query.get_or_404(custom_field_id) if cf.field_type == CustomField.CATEGORY: query = Category.get_categories_for_meeting(rule_type) return jsonify(data=[(i.id, unicode(i)) for i in query]) if cf.field_type == CustomField.COUNTRY: return jsonify(data=get_all_countries()) if cf.field_type == CustomField.CHECKBOX: return jsonify(data=ConditionForm.CHECKBOX_VALUES) if cf.field_type in (CustomField.SELECT, CustomField.RADIO): query = cf.choices.all() return jsonify(data=[(unicode(i), unicode(i)) for i in query]) return abort(400)
def save(self): categories_default = CategoryDefault.query.filter( CategoryDefault.id.in_(self.categories.data)) for category_default in categories_default: category = copy_attributes(Category(), category_default, exclude=('background', )) translation = Translation(english=category_default.title.english) db.session.add(translation) db.session.flush() category.title = translation category.tags = category_default.tags category.meeting = g.meeting filename = duplicate_uploaded_file(category_default.background, 'backgrounds') if filename: category.background = filename.basename() db.session.add(category) db.session.commit()
def custom_form_factory(form, field_types=None, field_slugs=None, excluded_field_types=None, registration_fields=False): fields = (CustomField.query.filter_by(meeting_id=g.meeting.id) .order_by(CustomField.sort)) if field_types: fields = fields.filter(CustomField.field_type.in_(field_types)) if field_slugs: fields = fields.filter(CustomField.slug.in_(field_slugs)) if excluded_field_types: fields = fields.filter( ~CustomField.field_type.in_(excluded_field_types)) if registration_fields: fields = fields.for_registration() if getattr(form, 'CUSTOM_FIELDS_TYPE', None): fields = fields.filter_by(custom_field_type=form.CUSTOM_FIELDS_TYPE) form_attrs = { '_custom_fields': OrderedMultiDict({c.slug: c for c in fields}), } for f in fields: attrs = {'label': unicode(CustomFieldLabel(f.label)), 'validators': [], 'render_kw': {}, 'description': f.hint} data = _CUSTOM_FIELDS_MAP[f.field_type.code] # overwrite data if _CUSTOM_FIELDS_MAP attribute is present on form form_fields_map = getattr(form, '_CUSTOM_FIELDS_MAP', None) if form_fields_map: try: data = form_fields_map[f.field_type.code] except KeyError: pass if f.required: attrs['validators'].append(DataRequired()) attrs['validators'].extend(data.get('validators', [])) if f.max_length: attrs['validators'].append(Length(max=f.max_length)) if f.field_type.code == CustomField.SELECT: query = CustomFieldChoice.query.filter_by(custom_field=f) attrs['choices'] = [(unicode(c.value), __(c.value.english)) for c in query] if not f.required: attrs['choices'] = [('', '---')] + attrs['choices'] if f.slug == 'title': attrs['choices'] = [choice for choice in attrs['choices'] if choice[0] in app.config['TITLE_CHOICES']] attrs['coerce'] = unicode if f.field_type.code == CustomField.LANGUAGE: attrs['choices'] = [i for i in Participant.LANGUAGE_CHOICES if i[0].lower() in app.config['TRANSLATIONS']] if not f.required: attrs['choices'] = [('', '---')] + attrs['choices'] attrs['coerce'] = unicode if f.field_type.code == CustomField.CATEGORY: query = Category.get_categories_for_meeting( form.CUSTOM_FIELDS_TYPE) if registration_fields: query = query.filter_by(visible_on_registration_form=True) attrs['choices'] = [(c.id, c) for c in query] attrs['coerce'] = int if f.field_type.code in (CustomField.MULTI_CHECKBOX, CustomField.RADIO): query = CustomFieldChoice.query.filter_by(custom_field=f) attrs['choices'] = [(unicode(c.value), c.value) for c in query] attrs['coerce'] = unicode if f.field_type.code == CustomField.IMAGE and f.photo_size and f.photo_size.code: attrs['render_kw']["data-photoSize"] = f.photo_size.code for coord in ("x1", "y1", "x2", "y2"): form_attrs['%s_%s_' % (f.slug, coord)] = HiddenField(default=0) # set field to form # _set_rules_for_custom_fields(f, attrs) field = data['field'](**attrs) setattr(field, 'field_type', f.field_type.code) form_attrs[f.slug] = field form_attrs['rules'] = Rule.get_rules_for_fields(fields) return type(form)(form.__name__, (form,), form_attrs)
def _get_query_for_category(self): query = Category.get_categories_for_meeting(g.rule_type) return [(str(c.id), unicode(c)) for c in query]
def _get_query_for_category(self): query = Category.get_categories_for_meeting(Category.PARTICIPANT) return [(str(c.id), unicode(c)) for c in query]
def read_participants_excel(custom_fields, rows, form_class, read_files=False): meeting_categories = {} for c in Category.get_categories_for_meeting( form_class.CUSTOM_FIELDS_TYPE): meeting_categories[c.title.english.lower()] = c.id countries = {} for code, name in get_all_countries(): countries[name.lower()] = code custom_fields = { custom_field.slug: custom_field for custom_field in custom_fields } Form = custom_form_factory(form_class) for row_num, row in enumerate(rows, start=2): participant_details = [] for slug, value in row.items(): value = value.strip() if not value: continue custom_field = custom_fields[slug] field_type = custom_field.field_type.code if field_type == CustomField.CATEGORY: value = meeting_categories.get(unicode(value).lower(), -1) elif field_type == CustomField.COUNTRY: value = countries.get(value.lower(), "invalid-country") elif field_type == CustomField.MULTI_CHECKBOX: value = [el.strip() for el in value.split(",")] elif field_type in (CustomField.IMAGE, CustomField.DOCUMENT): if read_files: resp = requests.get(value, stream=True) resp.raise_for_status() content_type = resp.headers.get( 'content-type', 'application/octet-stream') content_length = resp.headers.get('content-length', None) filename = parse_rfc6266_header( resp.headers.get("content-disposition", "")).get("filename") if not filename: # Attempt to guess the extension of the file ext = mimetypes.guess_extension(content_type) if ext: filename = str(uuid.uuid4()) + ext value = FileStorage( stream=io.BytesIO(resp.content), filename=filename, content_type=content_type, content_length=content_length, headers=resp.headers, ) else: # TODO: Add some form of validation to check the URLs are valid # A HEAD request could also be done in theory fast enough. pass if isinstance(value, list): # Multi checkbox values for val in value: participant_details.append((slug, val)) else: participant_details.append((slug, value)) form = Form(formdata=ImmutableMultiDict(participant_details), read_from_request=False) form.excel_row = row_num # Set the original value so the frontend can present it as such. for slug, value in row.items(): form[slug].excel_value = value.strip() yield form
def custom_form_factory(form, field_types=[], field_slugs=[], excluded_field_types=[], registration_fields=False): fields = (CustomField.query.filter_by(meeting_id=g.meeting.id) .order_by(CustomField.sort)) form_attrs = { '_custom_fields': OrderedMultiDict({c.slug: c for c in fields}), } if field_types: fields = fields.filter(CustomField.field_type.in_(field_types)) if field_slugs: fields = fields.filter(CustomField.slug.in_(field_slugs)) if excluded_field_types: fields = fields.filter( ~CustomField.field_type.in_(excluded_field_types)) if registration_fields: fields = fields.for_registration() if getattr(form, 'CUSTOM_FIELDS_TYPE', None): fields = fields.filter_by(custom_field_type=form.CUSTOM_FIELDS_TYPE) for f in fields: attrs = {'label': unicode(CustomFieldLabel(f.label)), 'validators': [], 'description': f.hint} data = _CUSTOM_FIELDS_MAP[f.field_type.code] # overwrite data if _CUSTOM_FIELDS_MAP attribute is present on form form_fields_map = getattr(form, '_CUSTOM_FIELDS_MAP', None) if form_fields_map: try: data = form_fields_map[f.field_type.code] except KeyError: pass if f.required: attrs['validators'].append(DataRequired()) attrs['validators'].extend(data.get('validators', [])) if f.max_length: attrs['validators'].append(Length(max=f.max_length)) if f.field_type.code == CustomField.SELECT: query = CustomFieldChoice.query.filter_by(custom_field=f) attrs['choices'] = [(unicode(c.value), __(c.value.english)) for c in query] if not f.required: attrs['choices'] = [('', '---')] + attrs['choices'] attrs['coerce'] = unicode if f.field_type.code == CustomField.LANGUAGE: attrs['choices'] = [i for i in Participant.LANGUAGE_CHOICES if i[0].lower() in app.config['TRANSLATIONS']] if not f.required: attrs['choices'] = [('', '---')] + attrs['choices'] attrs['coerce'] = unicode if f.field_type.code == CustomField.CATEGORY: query = Category.get_categories_for_meeting( form.CUSTOM_FIELDS_TYPE) if registration_fields: query = query.filter_by(visible_on_registration_form=True) attrs['choices'] = [(c.id, c) for c in query] attrs['coerce'] = int if f.field_type.code in (CustomField.MULTI_CHECKBOX,CustomField.RADIO): query = CustomFieldChoice.query.filter_by(custom_field=f) attrs['choices'] = [(unicode(c.value), c.value) for c in query] attrs['coerce'] = unicode # set field to form # _set_rules_for_custom_fields(f, attrs) field = data['field'](**attrs) setattr(field, 'field_type', f.field_type.code) form_attrs[f.slug] = field form_attrs['rules'] = Rule.get_rules_for_fields(fields) return type(form)(form.__name__, (form,), form_attrs)