From The Compiler, 6 Years ago, written in Python.
Embed
  1. #!/usr/bin/python3
  2. # vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :
  3.  
  4. # Copyright (C) 2012 Florian Bruhin <me@the-compiler.org>
  5.  
  6. # NAME is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # NAME is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with NAME.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. import sys
  20. import bottle
  21. import sqlalchemy
  22. import sqlalchemy.ext.declarative
  23. from bottle import route, put, post, request
  24. from bottle.ext.sqlalchemy import SQLAlchemyPlugin
  25. from sqlalchemy import Integer, Column, Sequence, String, ForeignKey, Text, UniqueConstraint
  26. from sqlalchemy.orm import sessionmaker
  27. from sqlalchemy.exc import SQLAlchemyError
  28.  
  29.  
  30. Base = sqlalchemy.ext.declarative.declarative_base()
  31. engine = sqlalchemy.create_engine(
  32.     'mysql+oursql://npc:CENSORED@localhost/npc',
  33.     echo=True, pool_recycle=3600
  34. )
  35. create_session = sessionmaker(bind=engine)
  36.  
  37. class Votes(Base):
  38.     __tablename__ = 'votes'
  39.     vote_id = Column(Integer, Sequence('vote_id_seq'),  primary_key=True)
  40.     ip = Column(String(39))
  41.     task_id = Column(Integer)
  42.     lang_id = Column(Integer)
  43.     __table_args__ = (UniqueConstraint('ip', 'task_id', name='unique_vote'),)
  44.  
  45. class Tasks(Base):
  46.     __tablename__ = 'tasks'
  47.     task_id = Column(Integer, Sequence('task_id_seq'),  primary_key=True)
  48.     name = Column(String(255))
  49.     desc = Column(Text())
  50.  
  51. class Langs(Base):
  52.     __tablename__ = 'langs'
  53.     lang_id = Column(Integer, Sequence('lang_id_seq'), primary_key=True)
  54.     name = Column(String(255))
  55.  
  56. @route('/getlangs')
  57. def getlangs(db):
  58.     langs = db.query(Langs)
  59.     result = "".join(["<li>{}</li>".format(lang.name) for lang in langs])
  60.     return "<ul>{}</ul>".format(result)
  61.  
  62. @route('/gettasks')
  63. def gettasks(db):
  64.     tasks = db.query(Tasks)
  65.     langs = db.query(Langs)
  66.     result=''
  67.     for task in tasks:
  68.         result+="<li>Name: {} / Desc: {}</li><br />".format(task.name, task.desc)
  69.         for lang in langs:
  70.             result+='<a href="/vote?lang={}&task={}">{}</a> '.format(lang.lang_id,
  71.                 task.task_id, lang.name)
  72.     Return "<ul>{}</ul>".format(result)
  73.  
  74. @route('/vote')
  75. def vote():
  76.     ip = request.environ.get('REMOTE_ADDR')
  77.     langid = request.query.lang
  78.     taskid = request.query.task
  79.     newvote = Votes(ip=ip, lang_id=langid, task_id=taskid)
  80.     session = create_session()
  81.     try:
  82.         session.add(newvote)
  83.         session.commit()
  84.     except sqlalchemy.exc.IntegrityError:
  85.         session.rollback()
  86.         return 'already voted'
  87.     except SQLAlchemyError:
  88.         session.rollback()
  89.         return 'sql-fail'
  90.     else:
  91.         return 'ok'
  92.     finally:
  93.         session.close()
  94.  
  95. # e.g. example.com/addlang?lang=python
  96. @route('/addlang')
  97. def addlang(db):
  98.     newlang = Langs(name=request.query.lang)
  99.     db.add(newlang)
  100.  
  101. @route('/addtask')
  102. def addtask_get():
  103.     return ('<form method="post" action=""><input name="name" />'
  104.             '<textarea name="desc"></textarea><input type="submit /></form>')
  105.  
  106. @post('/addtask')
  107. def addtask_post(db):
  108.     newtask = Tasks(name=request.forms.name, desc=request.forms.desc)
  109.     db.add(newtask)
  110.     return 'ok'
  111.  
  112. @route('/getvotes')
  113. def getvotes(db):
  114.     tasks = db.query(Tasks)
  115.     langs = db.query(Langs)
  116.     result=''
  117.     for task in tasks:
  118.         result+='<h1>Task {}: {}</h1>'.format(task.task_id, task.name)
  119.         result+='<ul>'
  120.         for lang in langs:
  121.             count = db.query(Votes).filter_by(
  122.                 task_id=task.task_id, lang_id=lang.lang_id).count()
  123.             result+='<li>{} - {}</li>'.format(lang.name, count)
  124.         result+='</ul>'
  125.     return result
  126.  
  127. bottle.install(SQLAlchemyPlugin(engine, Base.metadata, create=True))
  128.  
  129. if __name__ == '__main__':
  130.     bottle.debug(True)
  131.     bottle.run(reloader=True)
  132.