Пример #1
0
    def edit (self, user_id=None):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # Edit form for given user.
        return "ADMIN: Edit form for given user."
Пример #2
0
    def index (self):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # Present menu of administrative activities
        return """
Пример #3
0
    def delete (self, event_id=None):
        # Verify user is logged-in admin
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        pagetext = "<p><a href=\"/admin/events/processdelete/" + str(event_id) + "\">Confirm event deletion</a></p>\n"
        return pageutils.generate_page ("Confirm Deletion", pagetext)
Пример #4
0
    def processdelete (self, event_id=None):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # Verify we have an event_id
        if (event_id == None):
            return pageutils.generate_page ("No Event Id Specified", "Nothing to delete!")

        try:
            # Connect to the database and delete the given discussion.
            dbconnection = pgdb.connect (database_connect_fields)
            dbcursor = dbconnection.cursor()
            dbcursor.execute ("DELETE FROM events WHERE event_id=%s",
                              [str(event_id)])
            dbconnection.commit()

            # Close the database cursor and connection.
            dbcursor.close()
            dbconnection.close()
        except:
            return pageutils.generate_page ("Database Error", "Deletion failed!")

        return pageutils.generate_page ("Successful Deletion",
                                        "The specified event has been deleted.")
Пример #5
0
    def index (self):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # Present listing of all users.
        return "ADMIN: Present listing of all users."
Пример #6
0
    def index (self):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # No reason to present listing of events, as this is available
        # from the main public view.
        return "ADMIN: Select admin options from a particular event."
Пример #7
0
    def new (self, edit=False, title=None, slug=None, display=None, body=None, article_id=None):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # Form to create new article.
        pagecontents = ""
        if (edit):
            pagecontents += "<form action=\"/admin/articles/processedit\" method=\"post\">"
        else:
            pagecontents += "<form action=\"/admin/articles/processnew\" method=\"post\">"
        pagecontents += "<b>Title</b>:"
        pagecontents += "<br>"
        if (edit):
            pagecontents += "<input type=\"text\" name=\"title\" value=\"" + str(title) + "\">"
        else:
            pagecontents += "<input type=\"text\" name=\"title\">"
        pagecontents += "<br><br>\n"
        pagecontents += "<b>Slug</b>:"
        pagecontents += "<br>"
        if (edit):
            pagecontents += "<input type=\"text\" name=\"slug\" value=\"" + str(slug) + "\">"
        else:
            pagecontents += "<input type=\"text\" name=\"slug\">"
        pagecontents += "<br><br>"
        pagecontents += "<b>Display Mode</b>:"
        pagecontents += "<br>"
        pagecontents += "<select name=\"display\">\n"
        pagecontents += "<option value=\"0\">Do not display</option>\n"
        pagecontents += "<option value=\"1\">Display with no comments</option>\n"
        pagecontents += "<option value=\"2\" selected=\"selected\">Display with comments (default)</option>\n"
        pagecontents += "</select>\n"
        pagecontents += "<br><br>"
        pagecontents += "<b>Body</b>:"
        pagecontents += "<br>"
        if (edit):
            pagecontents += "<textarea cols=80 rows=10 name=\"body\">" + str(body) +"</textarea>\n"
        else:
            pagecontents += "<textarea cols=80 rows=10 name=\"body\"></textarea>\n"
        pagecontents += "<br><br>"
        if (edit):
            pagecontents += "<input type=\"hidden\" name=\"article_id\" value=\"" + str(article_id) + "\">"
        if (edit):
            pagecontents += "<input type=\"submit\" value=\"Submit Changes\">"
        else:
            pagecontents += "<input type=\"submit\" value=\"Create New Article\">"
        pagecontents += "</form>"

        if (edit):
            return pageutils.generate_page ("Edit Article", pagecontents)
        else:
            return pageutils.generate_page ("Create New Article", pagecontents)
Пример #8
0
    def processnew (self, title=None, slug=None, display=None, body=None, article_id=None,
                    edit=False):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        # If we got to this page through the /admin/articles/new form, all fields
        # should be filled in.  If they aren't, something unexpected happened, and
        # we shouldn't continue processing the form.
        if (title == None or slug == None or display == None or body == None):
            return pageutils.generate_page ("Invalid Input for New Article",
                                            "Invalid Input for New Article!")
        else:
            # Remove any leading or trailing spaces.
            title = string.strip (title)
            slug = string.strip (slug)
            body = string.strip (body)
            display = string.strip (display)
            author_id = pageutils.get_user_id()

            try:
                # Connect to the database and insert the values.
                dbconnection = pgdb.connect (database_connect_fields)
                dbcursor = dbconnection.cursor()
                if (edit):
                    if (article_id == None):
                        return pageutils.generate_page ("No Article Id Specified",
                                                        "No Article Id Specified")
                    dbcursor.execute ("UPDATE articles SET title=%s, slug=%s, body=%s, display=%d WHERE article_id=%d",
                                      [title, slug, body, int(display), int(article_id)])
                else:
                    dbcursor.execute ("INSERT INTO articles (title, author_id, slug, body, display, creation_date) " +
                                  "VALUES (%s, %s, %s, %s, %s, current_timestamp)",
                                  [title, author_id, slug, body, display])
                dbconnection.commit()

                # Close the database cursor and connection.
                dbcursor.close()
                dbconnection.close()
            except:
                return pageutils.generate_page ("Invalid SQL Query", "Invalid SQL Query!")
        
        raise cherrypy.HTTPRedirect ("/admin/articles/")
Пример #9
0
    def index (self):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")

        description = None
        results = None
        # Try to connect to the database.
        try:
            dbconnection = pgdb.connect (database_connect_fields)
            dbcursor = dbconnection.cursor()
            dbcursor.execute ("SELECT * FROM articles")
            
            # Get the cursor description and results from the query.
            description = dbcursor.description
            results = dbcursor.fetchall()
            
            # Close the database cursor and connection.
            dbcursor.close()
            dbconnection.close()
        except:
            pass

        pagecontents = "<p><a href=\"/admin/articles/new\">Create New Article</a></p>\n"
        pagecontents += "<h3>Article Listing</h3>\n"
        pagecontents += "<ul>\n"
        if (results == []):
            pagecontents += "<li>No articles found in database.</li>\n"
        for result in results:
            try:
                title = result[sqlutils.getfieldindex ("title", description)]
                slug = result[sqlutils.getfieldindex ("slug", description)]
                article_id = result[sqlutils.getfieldindex ("article_id", description)]
                pagecontents += "<li><a href=\"/admin/articles/edit/" + slug + "\">" + title + "</a>\n"
                pagecontents += "[<a href=\"/admin/articles/delete/" + article_id + "\">Delete</a>]</li>\n"
            except:
                pass
        pagecontents += "</ul>\n"

        # Present listing of all articles.
        return pageutils.generate_page ("Articles Administration", pagecontents)
Пример #10
0
    def edit (self, article_slug = None):
        # Verify user is logged-in admin.
        if (not pageutils.is_admin_p()):
            raise cherrypy.HTTPRedirect ("/")
        
        # Verify we have an article to work with.
        if (article_slug == None):
            raise cherrypy.HTTPRedirect ("/articles/")

        description = None
        results = None
        # Try to connect to the database.
        try:
            dbconnection = pgdb.connect (database_connect_fields)
            dbcursor = dbconnection.cursor()
            dbcursor.execute ("SELECT * FROM articles WHERE slug=%s", [article_slug])
            
            # Get the cursor description and results from the query.
            description = dbcursor.description
            results = dbcursor.fetchone()
            
            # Close the database cursor and connection.
            dbcursor.close()
            dbconnection.close()
        except:
            pass

        if (results == None):
            return pageutils.generate_page ("Invalid Article Specified", "Invalid Article Specified")

        # Obtain the article title from the database results.
        title = ""
        try:
            title = results[sqlutils.getfieldindex ("title", description)]
        except:
            pass

        # Obtain the article body from the database results.
        body = ""
        try:
            body = results[sqlutils.getfieldindex ("body", description)]
        except:
            pass

        # Obtain the article display value.
        display = ""
        try:
            display = str(results[sqlutils.getfieldindex ("display", description)])
        except:
            pass

        # Obtain the article_id.
        article_id = ""
        try:
            article_id = str(results[sqlutils.getfieldindex ("article_id", description)])
        except:
            pass

        slug = article_slug

        return self.new (edit=True, title=title, body=body, display=display, slug=slug, article_id=article_id)
Пример #11
0
    def index (self, discussion_id=None):
        # If discussion_id is None, display main discussion table of contents.
        # Else, display specified discussion.
        # Available to all, logged in or not.
        
        # Toplevel index.
        if (discussion_id == None):
            description = None
            results = None
            author_description = None
            author_results = []

            # Get discussion listing from database
            try:
                # Try to connect to the database.
                dbconnection = pgdb.connect (database_connect_fields)
                dbcursor = dbconnection.cursor()
                dbcursor.execute ("SELECT * FROM discussions WHERE refers_to IS null ORDER BY creation_date")
                
                # Get the cursor description and results from the query.
                description = dbcursor.description
                results = dbcursor.fetchall()

                # Get and store the user (author) data.
                for result in results:
                    dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
                                      [str(result[sqlutils.getfieldindex("author_id", description)])])
                    if (dbcursor.description <> None):
                        author_description = dbcursor.description
                    author_results.append (dbcursor.fetchone())

                # Close the database cursor and connection.
                dbcursor.close()
                dbconnection.close()
            except:
                return pageutils.generate_page ("Database Error",
                                                "<div class=\"error\">Can't get discussion data.</div>\n")

            # Build the page.
            pagetext = ""
            if (pageutils.is_logged_in_p()):
                pagetext += "<a href=\"/discussions/new\">Start New Discussion</a>\n"
            else:
                pagetext += "<a href=\"/login\">Log In</a> to start a new discussion</a>\n"
            pagetext += "<ul>\n"
            for result in results:
                pagetext += "<li>\n"
                pagetext += ("<a href=\"/discussions/" +
                             str(result[sqlutils.getfieldindex ("discussion_id", description)]) +
                             "\">" + result[sqlutils.getfieldindex ("subject", description)] + "</a> (posted by ")
                for author in author_results:
                    if author == None:
                        continue
                    if author[0] == result[sqlutils.getfieldindex ("author_id", description)]:
                        pagetext += author[sqlutils.getfieldindex ("name", author_description)]
                pagetext += " on " + result[sqlutils.getfieldindex ("creation_date", description)] + ")\n"
                pagetext += "</li>\n"
            pagetext += "</ul>\n"
            return pageutils.generate_page ("Discussions", pagetext)
        
        # Display a specific discussion.
        else:
            # Make sure we have a potentially-valid discussion id.
            try:
                discussion_id = str(int(discussion_id))
            except:
                return pageutils.generate_page ("Invalid Discussion",
                                                "<div class=\"error\">Can't present the requested discussion.</div>\n")
            
            description = None
            results = None
            reply_results = []
            author_description = None
            author_results = []

            # Get discussion listing from database
            try:
                # Try to connect to the database.
                dbconnection = pgdb.connect (database_connect_fields)
                dbcursor = dbconnection.cursor()
                dbcursor.execute ("SELECT * FROM discussions WHERE discussion_id=%s", [discussion_id])
                description = dbcursor.description
                results = dbcursor.fetchone()

                # Get the user (author) data for the main discussion.
                dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
                                  [str(results[sqlutils.getfieldindex("author_id", description)])])
                author_description = dbcursor.description
                author_results.append (dbcursor.fetchone())

                # Get any comments/replies for this discussion thread.
                dbcursor.execute ("SELECT * FROM discussions WHERE refers_to=%s", [discussion_id])
                reply_results = dbcursor.fetchall()
                

                # Get and store the user (author) data.
                for result in reply_results:
                    dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
                                      [str(result[sqlutils.getfieldindex("author_id", description)])])
                    author_results.append (dbcursor.fetchone())

                # Close the database cursor and connection.
                dbcursor.close()
                dbconnection.close()
            except:
                return pageutils.generate_page ("Database Error",
                                                "<div class=\"error\">Can't get discussion data.</div>\n")
            
            # Build page.
            pagetitle = results[sqlutils.getfieldindex("subject", description)]
            pagetext = "<p>" + results[sqlutils.getfieldindex("body", description)] + "</p>\n"
            for author in author_results:
                if author == None:
                    continue
                # Find the author info to display.
                if author[0] == results[sqlutils.getfieldindex ("author_id", description)]:
                    pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
                    pagetext += (" on " + results[sqlutils.getfieldindex ("creation_date", description)] +
                                 "</i></p>\n")
                    break
            if (pageutils.is_admin_p()):
                pagetext += ("<p>[<a href=\"/admin/discussions/delete/" +
                             str(results[sqlutils.getfieldindex("discussion_id", description)]) +
                             "\">Delete Discussion</a>]</p>")
            pagetext += "<hr width=\"50%\">\n"
            pagetext += "<h3>Replies</h3>\n"
            # Do we have any replies to show?
            if (reply_results <> None):
                for result in reply_results:
                    pagetext += "<p>"
                    pagetext += result[sqlutils.getfieldindex ("body", description)]
                    for author in author_results:
                        if author == None:
                            continue
                        # Find the author info to display.
                        if author[0] == result[sqlutils.getfieldindex ("author_id", description)]:
                            pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
                            pagetext += (" on " +
                                         result[sqlutils.getfieldindex ("creation_date", description)] +
                                         "</i></p>\n")
                            break
                    pagetext += "</p>\n"
                    # If the user is admin, post link to delete the reply.
                    if (pageutils.is_admin_p()):
                        pagetext += ("<p>[<a href=\"/admin/discussions/delete/" +
                                     str(result[sqlutils.getfieldindex ("discussion_id", description)]) +
                                     "\">Delete Reply</a>]</p>\n")
                    pagetext += "<hr width=50%>\n"
            # If user is logged in, post link to add a reply.
            if (pageutils.is_logged_in_p()):
                pagetext += "<p><a href=\"/discussions/reply/" + discussion_id + "\">Add a reply</a></p>\n"
            else:
                pagetext += "<p><a href=\"/login\">Log in</a> to add a reply</a></p>\n"
            
            # Generate page
            return pageutils.generate_page (pagetitle, pagetext)
Пример #12
0
    def index(self, event_id=None):
        # If event_id is None, display main event table of contents.
        # Else, display specified event details.
        # Available to all, logged in or not.

        # Build table of contents.
        if event_id == None:
            description = None
            results = None

            # Get event listing from database
            try:
                # Try to connect to the database.
                dbconnection = pgdb.connect(database_connect_fields)
                dbcursor = dbconnection.cursor()
                dbcursor.execute("SELECT * FROM events ORDER BY start_date")

                # Get the cursor description and results from the query.
                description = dbcursor.description
                results = dbcursor.fetchall()

                # Close the database cursor and connection.
                dbcursor.close()
                dbconnection.close()
            except:
                return pageutils.generate_page("Database Error", '<div class="error">Can\'t get event data.</div>\n')
            # Build the page.
            pagetext = ""
            if pageutils.is_logged_in_p():
                pagetext += '<a href="/events/new">Add New Event</a>\n'
            else:
                pagetext += '<a href="/login">Log In</a> to add a new event</a>\n'
            pagetext += "<ul>\n"
            most_recent_month = None
            most_recent_year = None
            for result in results:
                start_date = result[sqlutils.getfieldindex("start_date", description)]
                end_date = result[sqlutils.getfieldindex("end_date", description)]
                if most_recent_year <> pageutils.get_year(start_date):
                    most_recent_year = pageutils.get_year(start_date)
                    pagetext += "</ul><h2>" + str(most_recent_year) + "</h2><ul>\n"
                if most_recent_month <> pageutils.get_month(start_date):
                    most_recent_month = pageutils.get_month(start_date)
                    pagetext += "</ul><h3>" + most_recent_month + "</h3><ul>\n"
                pagetext += (
                    "<li>"
                    + '<a href="/events/'
                    + str(result[sqlutils.getfieldindex("event_id", description)])
                    + '">'
                    + result[sqlutils.getfieldindex("title", description)]
                    + "</a> ("
                    + str(pageutils.get_month(start_date))
                    + " "
                    + str(pageutils.get_day(start_date))
                )
                if result[sqlutils.getfieldindex("end_date", description)] <> None:
                    pagetext += " - " + pageutils.get_month(end_date) + " " + str(pageutils.get_day(end_date))
                pagetext += ")</li>\n"
            pagetext += "</ul>\n"
            return pageutils.generate_page("Events", pagetext)

        # Show specific event.
        else:
            description = None
            result = None

            # Get event listing from database
            try:
                # Try to connect to the database.
                dbconnection = pgdb.connect(database_connect_fields)
                dbcursor = dbconnection.cursor()
                dbcursor.execute("SELECT * FROM events WHERE event_id=%s", [str(event_id)])

                # Get the cursor description and results from the query.
                description = dbcursor.description
                result = dbcursor.fetchone()

                # Close the database cursor and connection.
                dbcursor.close()
                dbconnection.close()
            except:
                return pageutils.generate_page("Database Error", '<div class="error">Can\'t get event data.</div>\n')
            # Build the page.
            pagetext = ""
            start_date = result[sqlutils.getfieldindex("start_date", description)]
            end_date = result[sqlutils.getfieldindex("end_date", description)]
            pagetext += "<h3>" + pageutils.get_month(start_date) + " " + str(pageutils.get_day(start_date))
            if end_date <> None:
                pagetext += " - " + pageutils.get_month(end_date) + " " + str(pageutils.get_day(end_date))
                pagetext += ", " + pageutils.get_year(end_date)
            else:
                pagetext += ", " + pageutils.get_year(start_date)
            pagetext += "</h3>\n"
            pagetext += "<p>" + result[sqlutils.getfieldindex("description", description)]
            pagetitle = result[sqlutils.getfieldindex("title", description)]

            if pageutils.is_admin_p():
                pagetext += (
                    '<p>[<a href="/admin/events/delete/'
                    + str(result[sqlutils.getfieldindex("event_id", description)])
                    + '">Delete Event</a>]</p>'
                )

            return pageutils.generate_page(pagetitle, pagetext)
Пример #13
0
    def index (self, article_slug=None):
        # If article_slug is None, display main article table of contents.
        # Else, display specified article.
        # Available to all, logged in or not.

        if (article_slug == None):
            article_slug = "map"

        description = None
        results = None
        comments_description = None
        comments_results = None
        author_results = []
        author_description = None
       
        # Get article, comment, and comment-author data from database.
        try:
            # Try to connect to the database.
            dbconnection = pgdb.connect (database_connect_fields)
            dbcursor = dbconnection.cursor()
            dbcursor.execute ("SELECT * FROM articles WHERE slug=%s", [article_slug])
            # Get the cursor description and results from the query.
            description = dbcursor.description
            results = dbcursor.fetchone()

            # Get any comments for the article.
            if (results <> None):
                dbcursor.execute ("SELECT * FROM articles WHERE refers_to=%s",
                                  [str(results[sqlutils.getfieldindex ("article_id", description)])])
                comments_description = dbcursor.description
                comments_results = dbcursor.fetchall()
                # Store the user info for the author of the comment, for use when we display the comment.
                for result in comments_results:
                    dbcursor.execute ("SELECT * FROM users WHERE user_id=%s",
                                      [str(result[sqlutils.getfieldindex ("author_id", comments_description)])])
                    author_description = dbcursor.description
                    author_results.append (dbcursor.fetchone())

            # Close the database cursor and connection.
            dbcursor.close()
            dbconnection.close()
        except:
            pass

        if (results == None):
            # The "welcome" article is the main front page.  We need to have this page.
            if (article_slug == "welcome"):
                return pageutils.generate_page ("Welcome",
                                                "You will need to create an article with the slug: welcome")
            # We also need a "map" page.
            elif (article_slug == "map"):
                return pageutils.generate_page ("Map",
                                                "You will need to create an article with the slug: map")
            else:
                raise cherrypy.HTTPRedirect ("/404")

        # Obtain the article title from the database results.
        pagetitle = ""
        try:
            pagetitle += results[sqlutils.getfieldindex ("title", description)]
        except:
            pagetitle = "Database Error."

        # Obtain the article body from the database results.
        pagetext = ""
        try:
            pagetext += results[sqlutils.getfieldindex ("body", description)]
        except:
            pagetext += "<p>Database Error.</p>"

        # Do we want to show comments on this page?
        try:
            if (int(results[sqlutils.getfieldindex ("display", description)]) > 1):
                pagetext += "<hr><h3>User Comments</h3>"
                # Do we have any comments to show?
                if (comments_results <> None):
                    for result in comments_results:
                        pagetext += "<p>"
                        pagetext += result[sqlutils.getfieldindex ("body", comments_description)]
                        for author in author_results:
                            if author == None:
                                continue
                            # Find the author info to display.
                            if author[0] == result[sqlutils.getfieldindex ("author_id", comments_description)]:
                                pagetext += "<p><i>posted by " + author[sqlutils.getfieldindex ("name", author_description)]
                                pagetext += " on " + result[sqlutils.getfieldindex ("creation_date", comments_description)] + "</i></p>\n"
                                break
                        # If the user is admin, post link to delete the comment.
                        if (pageutils.is_admin_p()):
                            pagetext += ("<p>[<a href=\"/admin/articles/delete/" +
                                         str(result[sqlutils.getfieldindex ("article_id", comments_description)]) +
                                         "\">Delete Comment</a>]</p>\n")
                        pagetext += "</p>"
                        pagetext += "<hr width=50%>\n"
                        # If user is logged in, post link to add a comment.
                if (pageutils.is_logged_in_p()):
                    pagetext += "<p><a href=\"/articles/comment/" + article_slug + "\">Add a comment</a></p>\n"
                else:
                    pagetext += "<p><a href=\"/login\">Log in</a> to add a comment</a></p>\n"
        except:
            pass

        # Build the whole page and return it.
        return pageutils.generate_page (pagetitle, pagetext)