Beispiel #1
0
    def post(self, id, *args, **kwargs):
        log.info('Posting a goal update for %s of %s' % (id,
            unicode(self.request.POST),))
        goal = Goal.get_by_id(int(id))
        if goal is None:
            self.response.write(dumps({'error': 'invalid id: %s' % (id,)}))
            return

        try:
            if 'name' in self.request.POST:
                goal.name = self.request.get('name')
            if 'latitude' in self.request.POST:
                goal.center.lat = float(self.request.get('latitude'))
            if 'longitude' in self.request.POST:
                goal.center.lon = float(self.request.get('longitude'))
            if 'radius' in self.request.POST:
                goal.radius = int(self.request.get('radius'))
            if 'expires' in self.request.POST:
                goal.expires = \
                    datetime.utcfromtimestamp(int(self.request.get('expires')))
            if 'count' in self.request.POST:
                goal.count = int(self.request.get('count'))
            if 'desired' in self.request.POST:
                goal.desired = self.request.get('desired') in \
                    ['yes','1','true'] and True or False
            goal.put()
            self.response.write(dumps({'updated': goal.key().id()}))
        except Exception, e:
            log.exception('Error updating a goal')
            self.response.write(dumps({'error': unicode(e)}))
Beispiel #2
0
 def get(self, id, *args, **kwargs):
     log.info('Getting a goal: %s' % (id,))
     goal = Goal.get_by_id(int(id))
     if goal is None:
         self.response.write(dumps({'error': 'invalid id: %s' % (id,)}));
         return
     self.response.write(dumps(goal.get_as_dict()))
Beispiel #3
0
def goal_view(goal_id=None):
    if goal_id:
        # ---- Delete a goal and redirect to http ref
        goal = Goal.get_by_id(goal_id) if goal_id else None
        if not goal:
            flash('The goal with the id {} could not be found'.format(goal_id), 'error')
        else:
            if goal.movement.get().get_current_cycle() >= goal.cycle:
                flash('You can only delete goals for future cycles', 'error')
            else:
                goal.key.delete()
                flash("Goal has been deleted", 'info')
        return redirect(request.referrer or url_for('index'))
    else:
        # ---- Create a new goal and redirect http ref
        form = GoalForm(request.form)
        if form.validate_on_submit():
            author = Persona.get_by_id(users.get_current_user().user_id())
            if not author:
                flash("You user account was not found", "error")
                return redirect(request.referrer)

            movement = Movement.get_by_id(int(form.movement_id.data))
            if not movement:
                flash("The movement '{}' was not found".format(form.movement_id.data), 'error')
                return redirect(request.referrer)

            if len(form.desc.data) > 500:
                # Remove non-ascii characters
                flash("Goals can have at most 500 characters. Your goal: {}".format(
                    "".join(i for i in form.desc.data if ord(i) < 128)), "error")
                return redirect(request.referrer)

            goal = Goal(
                movement=movement.key,
                author=author.key,
                cycle=int(form.cycle.data) if form.cycle.data else None,
                desc=form.desc.data
            )
            try:
                goal.put()
                goal_id = goal.key.id()
                flash("Goal successfully created", 'success')
                return redirect(request.referrer or url_for('index'))
            except CapabilityDisabledError:
                flash("Sorry, datastore is currently in read-only mode", 'error')
                return redirect(request.referrer or url_for('index'))
        else:
            flash("Invalid form submitted", "error")
            return redirect(request.referrer)