{"action":"created","comment":{"url":"https://api.github.com/repos/nirhod/training/pulls/comments/181584074","pull_request_review_id":112237423,"id":181584074,"diff_hunk":"@@ -0,0 +1,124 @@\n+from sqlalchemy.ext.declarative import declarative_base\n+from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, Table\n+from sqlalchemy.orm import sessionmaker, relationship\n+from sqlalchemy.ext.declarative.api import DeclarativeMeta\n+import sqlalchemy\n+\n+NAME_LEN = 30\n+DEFAULT_VARCHAR_LEN = 256\n+\n+Base = declarative_base()\n+DB_PATH = './midas.db'\n+engine = create_engine(f'sqlite:///{DB_PATH}')\n+session = sessionmaker(bind=engine)()\n+\n+\n+class Query:\n+    \"\"\"\n+    This class is very close to Query class of sqlalchemy, but this class also supports the query API.\n+    \"\"\"\n+    def __init__(self, table_type: DeclarativeMeta, query: sqlalchemy.orm.query.Query):\n+        \"\"\"\n+        :param table_type: Terrorist or Organization or Event.\n+        :param query: Query obbject of sqlalchemy.\n+        \"\"\"\n+        self.type = table_type\n+        self.query = query\n+\n+    def __repr__(self):\n+        return f'<{self.query.count()} {self.type.__name__}s>'\n+\n+    def __getattr__(self, item):\n+        return getattr(self.query, item)\n+\n+    def __getitem__(self, item):\n+        return self.query.__getitem__(item)\n+\n+    def refine(self, *entities, **kwargs) -> 'Query':\n+        \"\"\"\n+        Same as filter and filter_by of sqlalchemy, but return Query object that supports the query API.\n+\n+        Example: Terrorist.get().refine(id=111) -> <1 Terrorists>\n+        \"\"\"\n+        if kwargs and isinstance(next(iter(kwargs.keys())), str):\n+            query = self.query.filter_by(**kwargs)\n+        else:\n+            query = self.query.filter(*entities, **kwargs)\n+        return Query(self.type, query)\n+\n+\n+class _UseQuery:\n+    \"\"\"\n+    Each table class that inherit from UseQuery will the support query API.\n+    \"\"\"\n+    @classmethod\n+    def get(cls) -> Query:\n+        \"\"\"\n+        :return: All the objects in db with type same as cls.\n+\n+        Example: Terrorist.get() -> <2 Terrorists>\n+        \"\"\"\n+        return Query(cls, session.query(cls))\n+\n+\n+association_table_event_terrorist = Table('association', Base.metadata\n+                                          , Column('terrorist_id', Integer, ForeignKey('Terrorists.id'))\n+                                          , Column('event_id', Integer, ForeignKey('Events.id')))\n+\n+\n+class Organization(Base, _UseQuery):\n+    \"\"\"\n+    Represents a terror organization.\n+    \"\"\"\n+    __tablename__ = 'Organizations'\n+\n+    id = Column(Integer, primary_key=True)\n+    prime_location = Column(String(DEFAULT_VARCHAR_LEN))\n+    name = Column(String(NAME_LEN))\n+\n+    def __repr__(self):\n+        return f'<Organization(id={self.id}, name={self.name}, prime_location={self.prime_location})>'\n+\n+    def __str__(self):\n+        return self.__repr__()\n+\n+\n+class Terrorist(Base, _UseQuery):\n+    \"\"\"\n+    Represents a Terrorist in an organization.\n+    \"\"\"\n+    __tablename__ = 'Terrorists'\n+\n+    id = Column(Integer, primary_key=True)\n+    name = Column(String(NAME_LEN))\n+    last_name = Column(String(NAME_LEN))\n+    role = Column(String(DEFAULT_VARCHAR_LEN))\n+    location = Column(String(DEFAULT_VARCHAR_LEN))\n+    organization = relationship('Organization', backref='terrorists')\n+    organization_id = Column(Integer, ForeignKey('Organizations.id'))\n+    events = relationship('Event', secondary=association_table_event_terrorist, backref='terrorists')\n+\n+    def __repr__(self):\n+        events = [event.id for event in self.events]\n+        return f'<Terrorist(id={self.id}, name={self.name}, last_name={self.last_name}, role={self.role}' \\\n+               f', location={self.location}, organization={self.organization_id}, events={events})>'\n+\n+    def __str__(self):\n+        return self.__repr__()\n+\n+\n+class Event(Base, _UseQuery):\n+    \"\"\"\n+    Represents a terror attack.\n+    \"\"\"\n+    __tablename__ = 'Events'\n+\n+    id = Column(Integer, primary_key=True)\n+    location = Column(String(DEFAULT_VARCHAR_LEN))\n+    date = Column(DateTime)\n+\n+    def __repr__(self):\n+        return f'<Event(id={self.id}, location={self.location})>'","path":"midas.py","position":121,"original_position":121,"commit_id":"0586ca306f4e837bc79a5d7c783652105471541e","original_commit_id":"0586ca306f4e837bc79a5d7c783652105471541e","user":{"login":"AlonSh","id":8265935,"avatar_url":"https://avatars0.githubusercontent.com/u/8265935?v=4","gravatar_id":"","url":"https://api.github.com/users/AlonSh","html_url":"https://github.com/AlonSh","followers_url":"https://api.github.com/users/AlonSh/followers","following_url":"https://api.github.com/users/AlonSh/following{/other_user}","gists_url":"https://api.github.com/users/AlonSh/gists{/gist_id}","starred_url":"https://api.github.com/users/AlonSh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlonSh/subscriptions","organizations_url":"https://api.github.com/users/AlonSh/orgs","repos_url":"https://api.github.com/users/AlonSh/repos","events_url":"https://api.github.com/users/AlonSh/events{/privacy}","received_events_url":"https://api.github.com/users/AlonSh/received_events","type":"User","site_admin":false},"body":"Missing date info","created_at":"2018-04-15T14:00:49Z","updated_at":"2018-04-15T14:16:51Z","html_url":"https://github.com/nirhod/training/pull/8#discussion_r181584074","pull_request_url":"https://api.github.com/repos/nirhod/training/pulls/8","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/nirhod/training/pulls/comments/181584074"},"html":{"href":"https://github.com/nirhod/training/pull/8#discussion_r181584074"},"pull_request":{"href":"https://api.github.com/repos/nirhod/training/pulls/8"}}},"pull_request":{"url":"https://api.github.com/repos/nirhod/training/pulls/8","id":181695599,"html_url":"https://github.com/nirhod/training/pull/8","diff_url":"https://github.com/nirhod/training/pull/8.diff","patch_url":"https://github.com/nirhod/training/pull/8.patch","issue_url":"https://api.github.com/repos/nirhod/training/issues/8","number":8,"state":"open","locked":false,"title":"Midas","user":{"login":"nirhod","id":37794897,"avatar_url":"https://avatars0.githubusercontent.com/u/37794897?v=4","gravatar_id":"","url":"https://api.github.com/users/nirhod","html_url":"https://github.com/nirhod","followers_url":"https://api.github.com/users/nirhod/followers","following_url":"https://api.github.com/users/nirhod/following{/other_user}","gists_url":"https://api.github.com/users/nirhod/gists{/gist_id}","starred_url":"https://api.github.com/users/nirhod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nirhod/subscriptions","organizations_url":"https://api.github.com/users/nirhod/orgs","repos_url":"https://api.github.com/users/nirhod/repos","events_url":"https://api.github.com/users/nirhod/events{/privacy}","received_events_url":"https://api.github.com/users/nirhod/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-04-15T12:10:44Z","updated_at":"2018-04-15T14:16:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"00e354052052c9c2bd16c9e9fea57324ef2e529f","assignee":null,"assignees":[],"requested_reviewers":[{"login":"ofir123","id":4647811,"avatar_url":"https://avatars1.githubusercontent.com/u/4647811?v=4","gravatar_id":"","url":"https://api.github.com/users/ofir123","html_url":"https://github.com/ofir123","followers_url":"https://api.github.com/users/ofir123/followers","following_url":"https://api.github.com/users/ofir123/following{/other_user}","gists_url":"https://api.github.com/users/ofir123/gists{/gist_id}","starred_url":"https://api.github.com/users/ofir123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ofir123/subscriptions","organizations_url":"https://api.github.com/users/ofir123/orgs","repos_url":"https://api.github.com/users/ofir123/repos","events_url":"https://api.github.com/users/ofir123/events{/privacy}","received_events_url":"https://api.github.com/users/ofir123/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/nirhod/training/pulls/8/commits","review_comments_url":"https://api.github.com/repos/nirhod/training/pulls/8/comments","review_comment_url":"https://api.github.com/repos/nirhod/training/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nirhod/training/issues/8/comments","statuses_url":"https://api.github.com/repos/nirhod/training/statuses/0586ca306f4e837bc79a5d7c783652105471541e","head":{"label":"nirhod:Midas","ref":"Midas","sha":"0586ca306f4e837bc79a5d7c783652105471541e","user":{"login":"nirhod","id":37794897,"avatar_url":"https://avatars0.githubusercontent.com/u/37794897?v=4","gravatar_id":"","url":"https://api.github.com/users/nirhod","html_url":"https://github.com/nirhod","followers_url":"https://api.github.com/users/nirhod/followers","following_url":"https://api.github.com/users/nirhod/following{/other_user}","gists_url":"https://api.github.com/users/nirhod/gists{/gist_id}","starred_url":"https://api.github.com/users/nirhod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nirhod/subscriptions","organizations_url":"https://api.github.com/users/nirhod/orgs","repos_url":"https://api.github.com/users/nirhod/repos","events_url":"https://api.github.com/users/nirhod/events{/privacy}","received_events_url":"https://api.github.com/users/nirhod/received_events","type":"User","site_admin":false},"repo":{"id":126945810,"name":"training","full_name":"nirhod/training","owner":{"login":"nirhod","id":37794897,"avatar_url":"https://avatars0.githubusercontent.com/u/37794897?v=4","gravatar_id":"","url":"https://api.github.com/users/nirhod","html_url":"https://github.com/nirhod","followers_url":"https://api.github.com/users/nirhod/followers","following_url":"https://api.github.com/users/nirhod/following{/other_user}","gists_url":"https://api.github.com/users/nirhod/gists{/gist_id}","starred_url":"https://api.github.com/users/nirhod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nirhod/subscriptions","organizations_url":"https://api.github.com/users/nirhod/orgs","repos_url":"https://api.github.com/users/nirhod/repos","events_url":"https://api.github.com/users/nirhod/events{/privacy}","received_events_url":"https://api.github.com/users/nirhod/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nirhod/training","description":null,"fork":false,"url":"https://api.github.com/repos/nirhod/training","forks_url":"https://api.github.com/repos/nirhod/training/forks","keys_url":"https://api.github.com/repos/nirhod/training/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nirhod/training/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nirhod/training/teams","hooks_url":"https://api.github.com/repos/nirhod/training/hooks","issue_events_url":"https://api.github.com/repos/nirhod/training/issues/events{/number}","events_url":"https://api.github.com/repos/nirhod/training/events","assignees_url":"https://api.github.com/repos/nirhod/training/assignees{/user}","branches_url":"https://api.github.com/repos/nirhod/training/branches{/branch}","tags_url":"https://api.github.com/repos/nirhod/training/tags","blobs_url":"https://api.github.com/repos/nirhod/training/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nirhod/training/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nirhod/training/git/refs{/sha}","trees_url":"https://api.github.com/repos/nirhod/training/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nirhod/training/statuses/{sha}","languages_url":"https://api.github.com/repos/nirhod/training/languages","stargazers_url":"https://api.github.com/repos/nirhod/training/stargazers","contributors_url":"https://api.github.com/repos/nirhod/training/contributors","subscribers_url":"https://api.github.com/repos/nirhod/training/subscribers","subscription_url":"https://api.github.com/repos/nirhod/training/subscription","commits_url":"https://api.github.com/repos/nirhod/training/commits{/sha}","git_commits_url":"https://api.github.com/repos/nirhod/training/git/commits{/sha}","comments_url":"https://api.github.com/repos/nirhod/training/comments{/number}","issue_comment_url":"https://api.github.com/repos/nirhod/training/issues/comments{/number}","contents_url":"https://api.github.com/repos/nirhod/training/contents/{+path}","compare_url":"https://api.github.com/repos/nirhod/training/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nirhod/training/merges","archive_url":"https://api.github.com/repos/nirhod/training/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nirhod/training/downloads","issues_url":"https://api.github.com/repos/nirhod/training/issues{/number}","pulls_url":"https://api.github.com/repos/nirhod/training/pulls{/number}","milestones_url":"https://api.github.com/repos/nirhod/training/milestones{/number}","notifications_url":"https://api.github.com/repos/nirhod/training/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nirhod/training/labels{/name}","releases_url":"https://api.github.com/repos/nirhod/training/releases{/id}","deployments_url":"https://api.github.com/repos/nirhod/training/deployments","created_at":"2018-03-27T07:24:23Z","updated_at":"2018-03-27T07:37:26Z","pushed_at":"2018-04-15T12:10:45Z","git_url":"git://github.com/nirhod/training.git","ssh_url":"git@github.com:nirhod/training.git","clone_url":"https://github.com/nirhod/training.git","svn_url":"https://github.com/nirhod/training","homepage":null,"size":24,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":6,"license":null,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"base":{"label":"nirhod:master","ref":"master","sha":"c301bb99e8b73b553d70db3623e17fe7a1bdb386","user":{"login":"nirhod","id":37794897,"avatar_url":"https://avatars0.githubusercontent.com/u/37794897?v=4","gravatar_id":"","url":"https://api.github.com/users/nirhod","html_url":"https://github.com/nirhod","followers_url":"https://api.github.com/users/nirhod/followers","following_url":"https://api.github.com/users/nirhod/following{/other_user}","gists_url":"https://api.github.com/users/nirhod/gists{/gist_id}","starred_url":"https://api.github.com/users/nirhod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nirhod/subscriptions","organizations_url":"https://api.github.com/users/nirhod/orgs","repos_url":"https://api.github.com/users/nirhod/repos","events_url":"https://api.github.com/users/nirhod/events{/privacy}","received_events_url":"https://api.github.com/users/nirhod/received_events","type":"User","site_admin":false},"repo":{"id":126945810,"name":"training","full_name":"nirhod/training","owner":{"login":"nirhod","id":37794897,"avatar_url":"https://avatars0.githubusercontent.com/u/37794897?v=4","gravatar_id":"","url":"https://api.github.com/users/nirhod","html_url":"https://github.com/nirhod","followers_url":"https://api.github.com/users/nirhod/followers","following_url":"https://api.github.com/users/nirhod/following{/other_user}","gists_url":"https://api.github.com/users/nirhod/gists{/gist_id}","starred_url":"https://api.github.com/users/nirhod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nirhod/subscriptions","organizations_url":"https://api.github.com/users/nirhod/orgs","repos_url":"https://api.github.com/users/nirhod/repos","events_url":"https://api.github.com/users/nirhod/events{/privacy}","received_events_url":"https://api.github.com/users/nirhod/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nirhod/training","description":null,"fork":false,"url":"https://api.github.com/repos/nirhod/training","forks_url":"https://api.github.com/repos/nirhod/training/forks","keys_url":"https://api.github.com/repos/nirhod/training/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nirhod/training/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nirhod/training/teams","hooks_url":"https://api.github.com/repos/nirhod/training/hooks","issue_events_url":"https://api.github.com/repos/nirhod/training/issues/events{/number}","events_url":"https://api.github.com/repos/nirhod/training/events","assignees_url":"https://api.github.com/repos/nirhod/training/assignees{/user}","branches_url":"https://api.github.com/repos/nirhod/training/branches{/branch}","tags_url":"https://api.github.com/repos/nirhod/training/tags","blobs_url":"https://api.github.com/repos/nirhod/training/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nirhod/training/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nirhod/training/git/refs{/sha}","trees_url":"https://api.github.com/repos/nirhod/training/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nirhod/training/statuses/{sha}","languages_url":"https://api.github.com/repos/nirhod/training/languages","stargazers_url":"https://api.github.com/repos/nirhod/training/stargazers","contributors_url":"https://api.github.com/repos/nirhod/training/contributors","subscribers_url":"https://api.github.com/repos/nirhod/training/subscribers","subscription_url":"https://api.github.com/repos/nirhod/training/subscription","commits_url":"https://api.github.com/repos/nirhod/training/commits{/sha}","git_commits_url":"https://api.github.com/repos/nirhod/training/git/commits{/sha}","comments_url":"https://api.github.com/repos/nirhod/training/comments{/number}","issue_comment_url":"https://api.github.com/repos/nirhod/training/issues/comments{/number}","contents_url":"https://api.github.com/repos/nirhod/training/contents/{+path}","compare_url":"https://api.github.com/repos/nirhod/training/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nirhod/training/merges","archive_url":"https://api.github.com/repos/nirhod/training/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nirhod/training/downloads","issues_url":"https://api.github.com/repos/nirhod/training/issues{/number}","pulls_url":"https://api.github.com/repos/nirhod/training/pulls{/number}","milestones_url":"https://api.github.com/repos/nirhod/training/milestones{/number}","notifications_url":"https://api.github.com/repos/nirhod/training/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nirhod/training/labels{/name}","releases_url":"https://api.github.com/repos/nirhod/training/releases{/id}","deployments_url":"https://api.github.com/repos/nirhod/training/deployments","created_at":"2018-03-27T07:24:23Z","updated_at":"2018-03-27T07:37:26Z","pushed_at":"2018-04-15T12:10:45Z","git_url":"git://github.com/nirhod/training.git","ssh_url":"git@github.com:nirhod/training.git","clone_url":"https://github.com/nirhod/training.git","svn_url":"https://github.com/nirhod/training","homepage":null,"size":24,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":6,"license":null,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nirhod/training/pulls/8"},"html":{"href":"https://github.com/nirhod/training/pull/8"},"issue":{"href":"https://api.github.com/repos/nirhod/training/issues/8"},"comments":{"href":"https://api.github.com/repos/nirhod/training/issues/8/comments"},"review_comments":{"href":"https://api.github.com/repos/nirhod/training/pulls/8/comments"},"review_comment":{"href":"https://api.github.com/repos/nirhod/training/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nirhod/training/pulls/8/commits"},"statuses":{"href":"https://api.github.com/repos/nirhod/training/statuses/0586ca306f4e837bc79a5d7c783652105471541e"}},"author_association":"OWNER"}}
{"actor":{"display_login":"AlonSh"}}