示例#1
0
def create_troika():
    url = __check_login('create_troika')
    if url: return redirect(url)
    
    troikaerrors = []
    troikaform = TroikaForm(prefix="troika")
    if troikaform.validate_on_submit():
        user = get_user(session['email'])
        # 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 start_time is not None and start_time < datetime.now():
                troikaerrors.append(_(u"Start time for the Troika must be in the future"))
            else:                
                troika = Troika(created = datetime.now(),
                        title=troikaform.title.data,
                        description=troikaform.description.data,
                        country='FI', region=None, area_id=None, campus_id=None,
                        address=troikaform.address.data if troikaform.address.data != "" else None,
                        address_addendum=troikaform.address_addendum.data if troikaform.address_addendum.data != "" else None,
                        language=troikaform.language.data, 
                        start_time=start_time,
                        end_time=__get_end_datetime(start_time, troikaform.duration.data),
                        max_participants=troikaform.max_participants.data, 
                        creator=user)
                if troikaform.creator_role.data == 'lead':
                    troika.lead = user
                else:
                    troika.first_learner = user
                save_troika(troika)
                flash(_(u"Troika Created"))
                return redirect(url_for('troika', troika_id=troika.id))
    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='create')
示例#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)