Skip to content

Commit

Permalink
Merge branch 'release/v0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
knktc committed Sep 9, 2018
2 parents 124fd07 + 6f84d25 commit e8110ba
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2018-09-09
### Added
- initial version(you can use this storage backend to upload and download file with **Qingstor**)
- add a demo site
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Django Qingstor Storage

A Django storage backend with **Qingstor**.

## Requirements

* Python3
* Django >= 2.0
* qingstor-sdk >= 2.2.6

## Installation

Clone code, and run following commands to install:

```bash
cd django_qingstor_storage
python setup.py install
```

*Using venv is highly recommended.*

*PYPI is on the way...*

## Settings

Edit your settings.py and set default(or other name) storage backend:

```python
# set storage backend
DEFAULT_FILE_STORAGE = 'django_qingstor_storage.backends.QinstorStorage'
```

And add Qingstor config in the settings.py:

```python
# Qingstor setting starts here
QINGSTOR_ACCESS_KEY_ID = 'YOUR_ACCESS_KEY_ID'
QINGSTOR_SECRET_ACCESS_KEY = 'YOUR_SECRET_ACCESS_KEY'
QINGSTOR_ZONE = 'YOUR_QINGSTOR_ZONE'
QINGSTOR_BUCKET = 'YOUR_QINGSTOR_BUCKET'
```

Also, you can set the Qingstor config by setting system environment variables with the following commands:

```bash
export QINGSTOR_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
export QINGSTOR_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
export QINGSTOR_ZONE=YOUR_QINGSTOR_ZONE
export QINGSTOR_BUCKET=YOUR_QINGSTOR_BUCKET
```

## Demo site

We also provide a demo site with Django admin. Just clone the code, edit settings.py in demo_site directory. And use the following commands to make it running:

```bash
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
```

Open your browser to visit <http://localhost:8000> .

## See Also

* Qinstor Python SDK(on github): <https://github.com/yunify/qingstor-sdk-python>
* Qinstor Python SDK docs: <https://docs.qingcloud.com/qingstor/sdk/python/qingstor_sdk.html>
* Qinstor Restful API: <https://docs.qingcloud.com/qingstor/api/>
Empty file added demo_site/demo_site/__init__.py
Empty file.
130 changes: 130 additions & 0 deletions demo_site/demo_site/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""
Django settings for demo_site project.
Generated by 'django-admin startproject' using Django 2.1.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ut$1j4go8f5)au1v0m)6_@=ijgt9so@+r0y8^&ds6kxz(j_hca'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'netdisk',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'demo_site.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'demo_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

# set storage backend
DEFAULT_FILE_STORAGE = 'django_qingstor_storage.backends.QinstorStorage'

# Qingstor setting starts here
QINGSTOR_ACCESS_KEY_ID = 'YOUR_ACCESS_KEY_ID'
QINGSTOR_SECRET_ACCESS_KEY = 'YOUR_SECRET_ACCESS_KEY'
QINGSTOR_ZONE = 'YOUR_QINGSTOR_ZONE'
QINGSTOR_BUCKET = 'YOUR_QINGSTOR_BUCKET'
23 changes: 23 additions & 0 deletions demo_site/demo_site/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""demo_site URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from netdisk import views

urlpatterns = [
path('', admin.site.urls),
path('download/<filename>/', views.download)
]
16 changes: 16 additions & 0 deletions demo_site/demo_site/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for demo_site project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo_site.settings')

application = get_wsgi_application()
15 changes: 15 additions & 0 deletions demo_site/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo_site.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Empty file added demo_site/netdisk/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions demo_site/netdisk/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

"""
use admin to upload file
@author:knktc
@contact:me@knktc.com
@create:2018-09-01 18:22
"""

from django.contrib import admin
from .models import File

__author__ = 'knktc'
__version__ = '0.1'


@admin.register(File)
class FileAdmin(admin.ModelAdmin):
list_display = ('filename', 'add_time', 'update_time',)
5 changes: 5 additions & 0 deletions demo_site/netdisk/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class NetdiskConfig(AppConfig):
name = 'netdisk'
24 changes: 24 additions & 0 deletions demo_site/netdisk/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.1 on 2018-09-02 08:10

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='File',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('filename', models.FileField(upload_to='')),
('note', models.TextField(blank=True, null=True)),
('add_time', models.DateTimeField(auto_now_add=True)),
('update_time', models.DateTimeField(auto_now=True)),
],
),
]
Empty file.
21 changes: 21 additions & 0 deletions demo_site/netdisk/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

"""
demo netdisk models
@author:knktc
@contact:me@knktc.com
@create:2018-09-01 18:11
"""

from django.db import models

__author__ = 'knktc'
__version__ = '0.1'


class File(models.Model):
filename = models.FileField()
note = models.TextField(blank=True, null=True)
add_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
3 changes: 3 additions & 0 deletions demo_site/netdisk/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
29 changes: 29 additions & 0 deletions demo_site/netdisk/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

"""
Use default storage to manipulate files
@author:knktc
@contact:me@knktc.com
@create:2018-09-01 18:22
"""

from django.http import HttpResponse, Http404
from django.core.files.storage import DefaultStorage
from django_qingstor_storage.backends import Qinstor404Exception as FileDoesNotExistException


STORAGE = DefaultStorage()


def download(request, **kwargs):
""" download file, no need to change codes when you change the storage backend
"""
filename = kwargs.get('filename')
try:
f = STORAGE.open(filename)
except FileDoesNotExistException:
raise Http404

response = HttpResponse(f.read())
return response
Loading

0 comments on commit e8110ba

Please sign in to comment.