Exemple #1
0
def troika(troika_id):
    troika = get_troika(troika_id);
    access = False
    activate = False
    user = None
    if 'email' in session:
        user = get_user(session['email'])
        access = get_troika_access_right(user, troika)
        activate = activatable(user, troika, app.config.get('ACTIVATABLE_BEFORE_THREE'))
    
    troikaform = TroikaForm(language=troika.language)
    language_name = [item for item in troikaform.language.choices if item[0] == troika.language][0][1]

    address_text = troika.address
    if address_text is None:
        address_text = troika.address_addendum
    elif troika.address_addendum is not None:
        address_text += " - " + troika.address_addendum
    if address_text is None:
        address_text = _(u'Not set')

    entry = {'access': access,
             'activate': activate,
             'id': troika.id,
             'phase': troika.get_phase(),
             'title': troika.title,
             'description': Markup(markdown.markdown(troika.description)),
             'address': address_text,
             'language': language_name,
             'start_time': __get_formatted_datetime(troika.start_time,"%d.%m.%Y %H:%M") if troika.start_time is not None else _(u"Not set"),
             'end_time': __get_formatted_datetime(troika.end_time,"%d.%m.%Y %H:%M") if troika.end_time is not None else _(u"Not set"),
             'max_participants': troika.max_participants if troika.max_participants is not None else _(u"Not set"),
             'is_full': __is_full(troika),
             'participating': __participating(user, troika),
             'lead': __get_display_name(troika.lead) if troika.lead != None else None,
             'first_learner': __get_display_name(troika.first_learner) if troika.first_learner != None else None,
             'second_learner': __get_display_name(troika.second_learner) if troika.second_learner != None else None,
             'participants': [dict(id=participant.id, \
                                full_name=__get_display_name(participant)) \
                                for participant in troika.participants] if troika.participants is not None else None
             }
    inviteform_lead = InviteForm(role='0', troika_id = troika.id)
    inviteform_first = InviteForm(role='1', troika_id = troika.id)
    inviteform_second = InviteForm(role='2', troika_id = troika.id)
    inviteform_participant = InviteForm(role='3', troika_id = troika.id)
    # show the troika with the given id
    return render_template('troika.html', 
                           troika=entry,
                           inviteform_lead=inviteform_lead,
                           inviteform_first=inviteform_first,
                           inviteform_second=inviteform_second,
                           inviteform_participant=inviteform_participant)
Exemple #2
0
def edit_troika(troika_id):
    url = __check_login(url = url_for('edit_troika', troika_id = troika_id))
    if url: return redirect(url)

    troikaerrors = []
    troikaform = TroikaForm(prefix="troika")
    # Find out if the logged in user has rights to edit/delete
    troika = get_troika(troika_id);
    user = get_user(session['email'])
    access = get_troika_access_right(user, troika)
    if not access:
        flash(_(u'Only the creator or the lead can edit the Troika'), 'error')
        return redirect(url_for('troika', troika_id=troika_id))

    if request.method == 'GET':
        # Add default values
        troikaform.title.data = troika.title
        troikaform.description.data = troika.description
        troikaform.address.data = troika.address
        troikaform.address_addendum.data = troika.address_addendum
        troikaform.language.data = troika.language
        troikaform.start_date.data = troika.start_time
        troikaform.start_time_hours.data = __get_formatted_datetime(troika.start_time,"%H")
        troikaform.start_time_minutes.data = __get_formatted_datetime(troika.start_time,"%M")
        troikaform.duration.data = __get_duration(troika.start_time, troika.end_time)
        troikaform.max_participants.data = troika.max_participants
    elif troikaform.validate_on_submit():
        if request.form["action"] == _(u"Save Troika"):
            # Validate times
            if troikaform.start_date.data is not None and (troikaform.start_time_hours.data is None or troikaform.start_time_minutes.data is None or troikaform.duration.data is None):
                troikaerrors.append(_(u"When a date is set, you must also give values for hours, minutes and duration"))
            else:
                start_time = __get_start_datetime(troikaform.start_date.data, troikaform.start_time_hours.data, troikaform.start_time_minutes.data)
                if user.role != 'admin' and start_time is not None and start_time < datetime.now():
                    troikaerrors.append(_(u"Start time for the Troika must be in the future"))
                else:
                    
                    # Update info
                    troika.title = troikaform.title.data
                    troika.description = troikaform.description.data
                    troika.address = troikaform.address.data if troikaform.address.data != "" else None 
                    troika.address_addendum = troikaform.address_addendum.data if troikaform.address_addendum.data != "" else None
                    troika.language = troikaform.language.data
                    troika.start_time = start_time
                    troika.end_time = __get_end_datetime(troika.start_time, troikaform.duration.data)
                    troika.max_participants = troikaform.max_participants.data

                    # Activate automatically when all information is given                                        
                    if activation_ready(troika):
                        __process_activation(troika)
                        flash (_(u"Troika activated!"))

                    save_troika(troika)
                    flash(_(u'Troika saved'))
                    
                    return redirect(url_for('troika', troika_id=troika_id))
                       
        elif request.form["action"] == _(u"Delete Troika"):
            title = troika.title
            delete_troika(troika)
            flash(_(u"Troika \"%(title)s\" deleted", title=title))
            return redirect(url_for('troikas'))
    if troikaform.errors:
        for key, value in troikaform.errors.items():
            troikaerrors.append(key + ': ' + value[0])

    return render_template('edit_troika.html', 
                           troikaform=troikaform, troikaerrors=troikaerrors,
                           access=access)