Commit fe636f31 authored by nano's avatar nano

started implementation of fiex

parent 81e04104
Pipeline #40 passed with stage
in 55 seconds
from django.contrib import admin
from django.contrib.auth.models import User
from .models import Exchange, File
# Register your models here.
class ExchangeAdmin(admin.ModelAdmin):
def has_view_or_change_permission(self, request, obj=None):
if obj is not None and obj.created_by != request.user:
return False
return True
def has_delete_permission(self, request, obj=None):
if obj is not None and obj.created_by != request.user:
return False
return True
fieldsets = [
(None, {'fields' : ['password','created_by']}),
('Beschreibung', {'fields' : ['description','creation_date']}),
]
list_filter = ['created_by']
admin.site.register(Exchange,ExchangeAdmin)
# Generated by Django 2.1.3 on 2018-11-20 20:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fiexapp', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='exchange',
name='password',
field=models.CharField(max_length=100, null=True, verbose_name='password required to access the exchange'),
),
migrations.AddField(
model_name='exchange',
name='token',
field=models.CharField(default='test', max_length=100, verbose_name='access token'),
preserve_default=False,
),
]
# Generated by Django 2.1.3 on 2018-11-20 20:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fiexapp', '0002_auto_20181120_2053'),
]
operations = [
migrations.AlterField(
model_name='exchange',
name='token',
field=models.CharField(max_length=100, unique=True, verbose_name='access token'),
),
]
# Generated by Django 2.1.3 on 2018-11-20 21:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fiexapp', '0003_auto_20181120_2059'),
]
operations = [
migrations.AlterField(
model_name='exchange',
name='token',
field=models.CharField(default='u9cbld5r30fz', editable=False, max_length=100, unique=True, verbose_name='access token'),
),
]
# Generated by Django 2.1.3 on 2018-11-20 21:27
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('fiexapp', '0004_auto_20181120_2113'),
]
operations = [
migrations.AddField(
model_name='exchange',
name='created_by',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
migrations.AlterField(
model_name='exchange',
name='token',
field=models.CharField(default='2paxgmvrir4v', editable=False, max_length=100, unique=True, verbose_name='access token'),
),
]
import string
import random
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password, is_password_usable
def gen_random_token(length=12, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _i in range(length))
def hash_password(password):
return make_password(password)
class Exchange(models.Model):
description = models.CharField('description',max_length=300)
"""
An exchange contains files that can be downloaded by anyone knowing the unique token - and if set - the required password
Also the upload of files is possible -if allowed
"""
DEFAULT_TOKEN_LENGTH = 12
"""
safety
"""
token = models.CharField('access token', max_length=100,unique=True, default=gen_random_token(DEFAULT_TOKEN_LENGTH),editable=False)
# TODO securely safe password!
password = models.CharField('password required to access the exchange', max_length=100, null=True)
created_by = models.ForeignKey(User,on_delete=models.CASCADE)
"""
description
"""
description = models.CharField('description', max_length=300)
creation_date = models.DateTimeField('date created')
class File(models.Model):
"""
An instance of the file class represents a file on the webserver.
Each file belongs to exactly on Exchange.
"""
path = models.CharField('path stored', max_length=200)
description = models.CharField('description of file', max_length=300,null=True)
Exchange = models.ForeignKey(Exchange, on_delete=models.CASCADE)
\ No newline at end of file
description = models.CharField('description of file', max_length=300, null=True)
Exchange = models.ForeignKey(Exchange, on_delete=models.CASCADE)
import re
from django.test import TestCase
from fiexapp.models import Exchange,File
from fiexapp.models import Exchange,File, gen_random_token, hash_password
from django.utils import timezone
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
# Create your tests here.
class ExchangeTestCase(TestCase):
def setUp(self):
self.test_user = User.objects.create_superuser(username='test',email='test@example.com',password='verysecretpass')
self.exchange_description = "Django Test Instance"
self.exchange_creation_date = timezone.now()
Exchange.objects.create(description=self.exchange_description,creation_date=self.exchange_creation_date)
Exchange.objects.create(description=self.exchange_description,creation_date=self.exchange_creation_date,created_by=self.test_user)
def test_object_creation(self):
test_exchange_object = Exchange.objects.get(description=self.exchange_description,creation_date=self.exchange_creation_date)
self.assertEqual(test_exchange_object.description,self.exchange_description)
self.assertEqual(test_exchange_object.creation_date, self.exchange_creation_date)
\ No newline at end of file
self.assertEqual(test_exchange_object.creation_date, self.exchange_creation_date)
def test_gen_random_token(self):
letters_token = "abc123"
random_token_regex_test = re.compile("^([" + letters_token + "]+)$")
length_token = 10
random_token = gen_random_token(length_token,letters_token)
self.assertEqual(len(random_token),length_token)
self.assertTrue(re.match(random_token_regex_test,random_token))
def test_hash_password(self):
password = "very1secretpass!"
hash = hash_password(password)
self.assertTrue(check_password(password,hash))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment