Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. """ I have followed the orm tutorial and I extended it a little , too see if I understand the relationship() part.
  2.  
  3. If someone could take a look at this and maby give me and example of how I use this in a session.add(User) and query it would be most helpful.
  4. """
  5.  
  6. """Sqlalchemy imports"""
  7. from sqlalchemy import create_engine
  8. from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
  9. from sqlalchemy.ext.declarative import declarative_base
  10. from sqlalchemy.orm import mapper, relationship, backref, sessionmaker
  11. from sqlalchemy.dialects.mysql import DATETIME, TIMESTAMP
  12.  
  13. """system imports"""
  14. from datetime import datetime
  15.  
  16.  
  17.  
  18. """load the databse"""
  19. db = 'mysql+mysqldb://user:>password>@localhost/database'
  20. engine = create_engine(db, echo=True)
  21.  
  22. Base = declarative_base()
  23. metadata = Base.metadata
  24.  
  25.  
  26. """build a session"""
  27. Session = sessionmaker()
  28. Session.configure(bind=engine)
  29. session = Session()
  30.  
  31.  
  32.  
  33.  
  34. class User(Base):
  35.     """Registrate members
  36.    """
  37.     __tablename__ = 'users'
  38.    
  39.     id = Column(Integer, primary_key=True)
  40.     nick = Column(String(50))
  41.     password = Column(String(10))
  42.     created = Column(TIMESTAMP())
  43.     modified = Column(TIMESTAMP())
  44.     active = Column(Integer)
  45.    
  46.     timezones = relationship('Timezone', order_by='Timezone.id')
  47.     groups = relationship('Groups', order_by='Groups.id')
  48.     userhosts = relationship('Userhosts', order_by='Userhosts.id')
  49.    
  50.     def __init__(self, nick, password, created, modified, timezoneGmt, country, active):
  51.         self.nick = nick
  52.         self.password = password
  53.         self.created = created
  54.         self.modified = modified
  55.         self.active = active
  56.        
  57.     def __repr__(self):
  58.         return "<User('%s', '%s', '%s', '%s', '%s', '%s', '%s')>" % (self.nick, self.password, self.created, self.modified, self.timezoneGmt, self.country,  self.active)    
  59.  
  60.  
  61. class Timezone(Base):
  62.     """reason for this table is , we dont need to store timezones and countries twice or more"""
  63.     __tablename__ = 'timezones'
  64.    
  65.     id = Column(Integer, primary_key=True)
  66.     timezoneGmt = Column(String(3))
  67.     country = Column(String(20))
  68.     user_id = Column(Integer, ForeignKey('users.id'))
  69.    
  70.     def __init__(self, timezoneGmt, country):    
  71.         self.timezoneGmt = timezoneGmt
  72.         self.country = country
  73.  
  74.     def __repr__(self):
  75.         return "<Timezone('%s', '%s')>" % (self.timezoneGmt, self.country)
  76.  
  77.  
  78.  
  79. class Groups(Base):
  80.     """Bind users.id to a group for define levels of security in ircbot.
  81.    """
  82.     __tablename__ = 'groups'
  83.    
  84.     id = Column(Integer, primary_key=True)
  85.     group = Column(String(20))
  86.     user_id = Column(Integer, ForeignKey('users.id'))
  87.    
  88.     def __init__(self, group):
  89.         self.group = group
  90.        
  91.     def __repr__(self):
  92.         return "<Groups('%s')>" % self.group
  93.  
  94.  
  95.  
  96.        
  97. class Userhosts(Base):
  98.  
  99.     """A user in a ircserver will have more than one /userhost everytime a user get a new userhost they will have to do a login and new userhost will end up in this table. My thought were , this is the same as the Address table in the ormtutorial example"""
  100.  
  101.     __tablename__ = 'userhosts'
  102.    
  103.     id = Column(Integer, primary_key=True)
  104.     host = Column(String(200))
  105.     user_id = Column(Integer, ForeignKey('users.id'))
  106.    
  107.     def __init__(self, host):
  108.         self.host = host
  109.        
  110.     def __repr__(self):
  111.         return "<Userhosts('%s')>" % self.host
  112.        
  113.  
  114.  
  115. class Help(Base):
  116.    
  117.     __tablename__ = 'help'
  118.    
  119.     id = Column(Integer, primary_key=True)
  120.     command = Column(String(20))
  121.     argument = Column(String(20))
  122.     helptext = Column(String(200))
  123.    
  124.     def __init__(self, command, argument, helptext):
  125.         self.command = command
  126.         self.argument = argument
  127.         self.helptext = helptext
  128.        
  129.     def __repr__(self):
  130.         return "<Help('%s', '%s', '%s')>" % (self.command, self.argument, self.helptext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement