def post(self):
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))

        conflict_id = self.request.get("conflict_id")
        try:
            conflict = Conflict.get_by_id(int(conflict_id))
        except (db.BadKeyError, StandardError) as e:
            self.error(500)
            print >> sys.stderr, str(e)
            return

        char = Character.gql("WHERE user = :1 AND conflict = :2", user,
                             conflict).get()

        if char is None:
            char = Character(user=user, conflict=conflict)

        if not char.finalized:
            char.name = self.request.get("char_name")
            char.intent = self.request.get("intent")
            char.finalized = True
            char.put()
        self.redirect('/conflict?conflict_id=%s' % conflict_id)
    def get(self):
        conflict_id = self.request.get("conflict_id")
        if not conflict_id:
            # TODO: Add overview page
            print "lolfukt"
            return

        try:
            conflict = Conflict.get_by_id(int(conflict_id))
            if conflict is None:
                raise RuntimeError("Can't find Conflict")
        except StandardError:
            self.error(404)
            return

        if all(char.finalized for char in conflict.characters):
            conflict.ready = True

        html_page = 'run_conflict.html' if conflict.ready else 'create_conflict.html'

        user = users.get_current_user()
        if user is None:
            user_char = None
        else:
            # May still be none if user is not in this conflict
            # TODO: Have conflict track their users so it can be validated
            user_char = Character.gql("WHERE user = :1 AND conflict = :2",
                                      user, conflict).get()

        html_path = os.path.join(os.path.dirname(__file__), html_page)
        template_values = {'conflict': conflict, 'user_char': user_char}
        self.response.out.write(template.render(html_path, template_values))
    def get(self):
        conflict_id = self.request.get("conflict_id")
        if not conflict_id:
            # TODO: Add overview page
            print "lolfukt"
            return

        try:
            conflict = Conflict.get_by_id(int(conflict_id))
            if conflict is None:
                raise RuntimeError("Can't find Conflict")
        except StandardError:
            self.error(404)
            return

        if all(char.finalized for char in conflict.characters):
            conflict.ready = True

        html_page = 'run_conflict.html' if conflict.ready else 'create_conflict.html'

        user = users.get_current_user()
        if user is None:
            user_char = None
        else:
            # May still be none if user is not in this conflict
            # TODO: Have conflict track their users so it can be validated
            user_char = Character.gql("WHERE user = :1 AND conflict = :2",
                                      user, conflict).get()

        html_path = os.path.join(os.path.dirname(__file__), html_page)
        template_values = {'conflict': conflict,
                           'user_char': user_char}
        self.response.out.write(template.render(html_path, template_values))
    def post(self):
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))

        conflict_id = self.request.get("conflict_id")
        try:
            conflict = Conflict.get_by_id(int(conflict_id))
        except (db.BadKeyError, StandardError) as e:
            self.error(500)
            print >>sys.stderr, str(e)
            return

        char = Character.gql("WHERE user = :1 AND conflict = :2",
                             user, conflict).get()

        if char is None:
            char = Character(user=user, conflict=conflict)

        if not char.finalized:
            char.name = self.request.get("char_name")
            char.intent = self.request.get("intent")
            char.finalized = True
            char.put()
        self.redirect('/conflict?conflict_id=%s' % conflict_id)
    def post(self):
        """
        Create a new conflict
        """
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))

        conflict = Conflict.new()
        Character(user=user, conflict=conflict).put()

        html_path = os.path.join(os.path.dirname(__file__),
                                 'create_conflict.html')
        template_values = {'conflict': conflict}
        self.response.out.write(template.render(html_path, template_values))