Ejemplo n.º 1
0
def _edit_photo(meeting, person):
    response = {'status': 'error'}
    app = flask.current_app
    #delete current photo if exists
    photo = person.decoded_data.get('photo')
    original_photo = person.decoded_data.get('original_photo')
    if photo:
        try:
           path(photos.path(photo)).remove()
        except os.error:
            pass
    if original_photo:
        try:
           path(photos.path(original_photo)).remove()
        except os.error:
            pass

    photo_name = '%s.' % secure_filename(person.decoded_data['personal_last_name'])
    filename = photos.save(flask.request.files['photo'], name=photo_name)
    file_path = app.config['UPLOADED_PHOTOS_DEST'] / filename
    file_size = app.config['UPLOADED_PHOTO_RESOLUTION']
    new_file_path = sugar.optimize_photo(file_path, file_size)

    person.data['photo'] = new_file_path.name
    person.data.pop('original_photo', None)
    person.save()

    sugar.activity(meeting.id, 'edit-participant-photo', person.name, person.id)
    response['status'] = 'success'
    response['url'] = person.image_file
    return flask.json.dumps(response)
Ejemplo n.º 2
0
def form(meeting_id):
    app = flask.current_app
    meeting = schema.Meeting.get_or_404(meeting_id)

    # render form in local language
    lang = flask.request.args.get('lang', 'en')
    if lang not in ('en', 'fr', 'es'):
        flask.abort(400)
    sugar.set_lang(lang)

    meeting_model = sugar.get_object_or_404(models.Meeting, id=meeting_id)
    # you can define custom forms based on meeting types
    # in forms/custom_participant.py
    RegistrationForm = get_registration_form(meeting_model.data['info_type'])
    form = RegistrationForm(meeting=meeting_model)
    if meeting_model.is_photo_required is False:
        delattr(form, 'photo')
    event_form = EventRegistrationForm(meeting=meeting_model)
    question_form = QuestionRegistrationForm(meeting=meeting_model)

    if meeting['info']['online_registration'] == 'closed':
        return flask.render_template('participant/registration_closed.html',
                                     acronym=meeting['info'].get('acronym'))

    if flask.request.method == 'POST':

        if form.validate():

            person = models.Person()
            person.data = {}

            photo = form.data.get('photo')
            if photo:
                #use last name for the photo_name
                photo_name = '%s.' % secure_filename(form.data['personal_last_name'])
                filename = photos.save(photo, name=photo_name)
                file_path = app.config['UPLOADED_PHOTOS_DEST'] / filename
                file_size = app.config['UPLOADED_PHOTO_RESOLUTION']
                new_file_path = sugar.optimize_photo(file_path, file_size)
                filename = new_file_path.name
                original_filename = file_path.name
            else:
                filename = ''
                original_filename = ''

            form_data = dict(form.data, photo=filename,
                             uploaded_photo=original_filename)
            meeting_flags = getattr(form, 'meeting_flags_data', {})
            files_data = form.upload_files()
            if files_data:
                form_data.update(files_data)
            person.data.update(form_data.items())
            personal_category = int(person.data.pop('personal_category'))

            select_country = form.data['representing_country'] or form.data['personal_country'] or None
            if select_country:
                country = models.Country.select().where(
                    data__contains={'iso': select_country}).get()
                person.data['representing_region'] = country.data['region']

            person.data['printout_representing'] = person.rep(meeting,
                category_id=personal_category) or ''

            category = sugar.get_person_category_or_None(meeting_id, personal_category)
            person.data['room'] = category.data.get('room', '')
            person.save()
            person.data['_text_for_search'] = person.text_for_search
            person.save()
            sugar.activity(meeting_id, "add-participant", person.name, person.id)

            meeting_data = {}
            meeting_data.update(MEETING_FLAGS)
            meeting_data.update(meeting_flags)
            meeting_data['registered_date'] = str(datetime.now().replace(
                second=0, microsecond=0))

            person_meeting = models.PersonMeeting.create(
                person=person.id,
                meeting=meeting_id,
                category=personal_category,
                token=sugar.random_string(),
                data=meeting_data)

            event_form.save(person)
            question_form.save(person)

            events = _get_events(event_form.data['events'])

            if form.data['personal_email']:
                _send_mail_to_self_registering_participant(
                    meeting_model, events, form, lang)

            # language will change here to english
            sugar.set_lang("en")
            refresh()

            _send_mail_to_admin(meeting_model, events, form, person.id)

            phrase_lang = {"en": "E", "es": "S", "fr": "F"}.get(lang, "E")
            phrase = models.Phrase.select().where(
                        data__contains={'name': 'Registration confirmation'},
                        meeting=meeting_id).get()
            return flask.render_template(
                'participant/form/success.html',
                content=phrase.decoded_data['description_%s' % phrase_lang])
        else:
            log.warn("validation errors in participant form", extra={'errors': form.errors, 'form_data': form.data})

    return flask.render_template(
        form.TEMPLATE,
        meeting=meeting,
        meeting_model=meeting_model,
        form=form,
        lang={"en": "E", "es": "S", "fr": "F"}.get(lang, "E"),
        event_form=event_form,
        question_form=question_form)