Skip to content

Commit f3536d2

Browse files
committed
adding new sqlalchemy examples
1 parent 1719846 commit f3536d2

File tree

61 files changed

+764
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+764
-58
lines changed

content/pages/examples/django/django-extensions-plug-ins.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ Code from django-angular is shown on:
8080
[django-axes](https://github.com/jazzband/django-axes/)
8181
([project documentation](https://django-axes.readthedocs.io/en/latest/)
8282
and
83-
[PyPI package information](https://pypi.org/project/django-axes/)
83+
[PyPI package information](https://pypi.org/project/django-axes/))
8484
is a code library for [Django](/django.html) projects to track failed
8585
login attempts against a web application. The goal of the project is
8686
to make it easier for you to stop people and scripts from hacking your
8787
Django-powered website.
8888

8989
The code for django-axes is
90-
[open source under the MIT liense](https://github.com/jazzband/django-axes/blob/master/LICENSE)
90+
[open source under the MIT license](https://github.com/jazzband/django-axes/blob/master/LICENSE)
9191
and maintained by the group of developers known as
9292
[Jazzband](https://jazzband.co/).
9393

content/pages/examples/flask/flask-example-projects-code.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ The code is open sourced under the
2222
[MIT license](https://github.com/braintree/braintree_flask_example/blob/master/LICENSE).
2323

2424

25+
### CTFd
26+
[CTFd](https://github.com/CTFd/CTFd)
27+
([homepage](https://ctfd.io/)) is a
28+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
29+
built with [Flask](/flask.html). The application can be used
30+
as-is to run CTF events, or modified for custom rules for related
31+
scenarios. CTFd is open sourced under the
32+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
33+
34+
2535
### Datadog Flask example
2636
The [Datadog Flask app](https://github.com/DataDog/trace-examples/tree/master/python/flask)
2737
contains many examples of the [Flask](/flask.html) core functions

content/pages/examples/sqlalchemy/sqlalchemy-engine-engine.markdown

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,120 @@ meta: Example code for understanding how to use the Engine class from the sqlalc
1010
Engine is a class within the sqlalchemy.engine module of the SQLAlchemy project.
1111

1212

13-
## Example 1 from GINO
13+
## Example 1 from CTFd
14+
[CTFd](https://github.com/CTFd/CTFd)
15+
([homepage](https://ctfd.io/)) is a
16+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
17+
built with [SQLAlchemy](/sqlalchemy.html) and [Flask](/flask.html).
18+
The application can be used as-is to run CTF events, or the code can be
19+
modified for custom rules on hacking scenarios. CTFd is open sourced under the
20+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
21+
22+
[**CTFd / CTFd / __init__.py**](https://github.com/CTFd/CTFd/blob/master/./CTFd/__init__.py)
23+
24+
```python
25+
# __init__.py
26+
import datetime
27+
import os
28+
import sys
29+
import weakref
30+
from distutils.version import StrictVersion
31+
32+
import jinja2
33+
from flask import Flask, Request
34+
from flask_migrate import upgrade
35+
from jinja2 import FileSystemLoader
36+
from jinja2.sandbox import SandboxedEnvironment
37+
from werkzeug.middleware.proxy_fix import ProxyFix
38+
from werkzeug.utils import cached_property
39+
40+
from CTFd import utils
41+
from CTFd.plugins import init_plugins
42+
from CTFd.utils.crypto import sha256
43+
from CTFd.utils.initialization import (
44+
init_events,
45+
init_logs,
46+
init_request_processors,
47+
init_template_filters,
48+
init_template_globals,
49+
)
50+
from CTFd.utils.migrations import create_database, migrations, stamp_latest_revision
51+
from CTFd.utils.sessions import CachingSessionInterface
52+
from CTFd.utils.updates import update_check
53+
54+
__version__ = "3.0.0b2"
55+
56+
57+
class CTFdRequest(Request):
58+
@cached_property
59+
def path(self):
60+
return self.script_root + super(CTFdRequest, self).path
61+
62+
63+
## ... source file abbreviated to get to Engine examples ...
64+
65+
66+
}
67+
)
68+
app.jinja_loader = jinja2.ChoiceLoader([app.theme_loader, app.plugin_loader])
69+
70+
from CTFd.models import ( # noqa: F401
71+
db,
72+
Teams,
73+
Solves,
74+
Challenges,
75+
Fails,
76+
Flags,
77+
Tags,
78+
Files,
79+
Tracking,
80+
)
81+
82+
url = create_database()
83+
84+
app.config["SQLALCHEMY_DATABASE_URI"] = str(url)
85+
86+
db.init_app(app)
87+
88+
migrations.init_app(app, db)
89+
90+
if url.drivername.startswith("sqlite"):
91+
~~ from sqlalchemy.engine import Engine
92+
from sqlalchemy import event
93+
94+
~~ @event.listens_for(Engine, "connect")
95+
def set_sqlite_pragma(dbapi_connection, connection_record):
96+
cursor = dbapi_connection.cursor()
97+
cursor.execute("PRAGMA foreign_keys=ON")
98+
cursor.close()
99+
100+
db.create_all()
101+
stamp_latest_revision()
102+
else:
103+
upgrade()
104+
105+
from CTFd.models import ma
106+
107+
ma.init_app(app)
108+
109+
app.db = db
110+
app.VERSION = __version__
111+
112+
from CTFd.cache import cache
113+
114+
cache.init_app(app)
115+
app.cache = cache
116+
117+
reverse_proxy = app.config.get("REVERSE_PROXY")
118+
if reverse_proxy:
119+
120+
121+
## ... source file continues with no further Engine examples...
122+
123+
```
124+
125+
126+
## Example 2 from GINO
14127
[GINO](https://github.com/fantix/gino)
15128
([project documentation](https://python-gino.readthedocs.io/en/latest/)
16129
and

content/pages/examples/sqlalchemy/sqlalchemy-engine-url-make-url.markdown

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,117 @@ meta: Python example code that shows how to use the make_url callable from the s
1010
make_url is a callable within the sqlalchemy.engine.url module of the SQLAlchemy project.
1111

1212

13-
## Example 1 from flask-sqlalchemy
13+
## Example 1 from CTFd
14+
[CTFd](https://github.com/CTFd/CTFd)
15+
([homepage](https://ctfd.io/)) is a
16+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
17+
built with [SQLAlchemy](/sqlalchemy.html) and [Flask](/flask.html).
18+
The application can be used as-is to run CTF events, or the code can be
19+
modified for custom rules on hacking scenarios. CTFd is open sourced under the
20+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
21+
22+
[**CTFd / tests / helpers.py**](https://github.com/CTFd/CTFd/blob/master/./tests/helpers.py)
23+
24+
```python
25+
# helpers.py
26+
import datetime
27+
import gc
28+
import random
29+
import string
30+
import uuid
31+
from collections import namedtuple
32+
from unittest.mock import Mock, patch
33+
34+
import requests
35+
from flask.testing import FlaskClient
36+
~~from sqlalchemy.engine.url import make_url
37+
from sqlalchemy_utils import drop_database
38+
from werkzeug.datastructures import Headers
39+
40+
from CTFd import create_app
41+
from CTFd.cache import cache, clear_standings
42+
from CTFd.config import TestingConfig
43+
from CTFd.models import (
44+
Awards,
45+
ChallengeFiles,
46+
Challenges,
47+
Fails,
48+
Files,
49+
Flags,
50+
Hints,
51+
Notifications,
52+
PageFiles,
53+
Pages,
54+
Solves,
55+
Tags,
56+
Teams,
57+
Tokens,
58+
Tracking,
59+
Unlocks,
60+
Users,
61+
62+
63+
## ... source file abbreviated to get to make_url examples ...
64+
65+
66+
if isinstance(headers, dict):
67+
headers = Headers(headers)
68+
headers.extend(api_key_headers)
69+
kwargs["headers"] = headers
70+
return super(CTFdTestClient, self).open(*args, **kwargs)
71+
72+
73+
def create_ctfd(
74+
ctf_name="CTFd",
75+
ctf_description="CTF description",
76+
name="admin",
77+
email="admin@ctfd.io",
78+
password="password",
79+
user_mode="users",
80+
setup=True,
81+
enable_plugins=False,
82+
application_root="/",
83+
config=TestingConfig,
84+
):
85+
if enable_plugins:
86+
config.SAFE_MODE = False
87+
else:
88+
config.SAFE_MODE = True
89+
90+
config.APPLICATION_ROOT = application_root
91+
~~ url = make_url(config.SQLALCHEMY_DATABASE_URI)
92+
if url.database:
93+
url.database = str(uuid.uuid4())
94+
config.SQLALCHEMY_DATABASE_URI = str(url)
95+
96+
app = create_app(config)
97+
app.test_client_class = CTFdTestClient
98+
99+
if setup:
100+
app = setup_ctfd(
101+
app,
102+
ctf_name=ctf_name,
103+
ctf_description=ctf_description,
104+
name=name,
105+
email=email,
106+
password=password,
107+
user_mode=user_mode,
108+
)
109+
return app
110+
111+
112+
def setup_ctfd(
113+
app,
114+
ctf_name="CTFd",
115+
ctf_description="CTF description",
116+
117+
118+
## ... source file continues with no further make_url examples...
119+
120+
```
121+
122+
123+
## Example 2 from flask-sqlalchemy
14124
[flask-sqlalchemy](https://github.com/pallets/flask-sqlalchemy)
15125
([project documentation](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
16126
and
@@ -133,7 +243,7 @@ class _EngineConnector:
133243
```
134244

135245

136-
## Example 2 from GINO
246+
## Example 3 from GINO
137247
[GINO](https://github.com/fantix/gino)
138248
([project documentation](https://python-gino.readthedocs.io/en/latest/)
139249
and

content/pages/examples/sqlalchemy/sqlalchemy-example-projects-code.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ The project is open sourced under the
2727
[BSD 3-Clause "New" or "Revised" License](https://github.com/encode/databases/blob/master/LICENSE.md).
2828

2929

30+
# CTFd
31+
[CTFd](https://github.com/CTFd/CTFd)
32+
([homepage](https://ctfd.io/)) is a
33+
[capture the flag (CTF) hacking web app](https://cybersecurity.att.com/blogs/security-essentials/capture-the-flag-ctf-what-is-it-for-a-newbie)
34+
built with [SQLAlchemy](/sqlalchemy.html) and [Flask](/flask.html).
35+
The application can be used as-is to run CTF events, or the code can be
36+
modified for custom rules on hacking scenarios. CTFd is open sourced under the
37+
[Apache License 2.0](https://github.com/CTFd/CTFd/blob/master/LICENSE).
38+
39+
3040
### indico
3141
[indico](https://github.com/indico/indico)
3242
([project website](https://getindico.io/),

0 commit comments

Comments
 (0)