daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from boto3 import session
  2. from botocore.client import Config
  3.  
  4. ACCESS_ID = '-----'
  5. SECRET_KEY = '-----'
  6.  
  7. # Initiate session
  8. session = session.Session()
  9. client = session.client('s3',
  10.                         region_name='nyc3',
  11.                         endpoint_url='https://nyc3.digitaloceanspaces.com',
  12.                         aws_access_key_id=ACCESS_ID,
  13.                         aws_secret_access_key=SECRET_KEY)
  14.  
  15. # Upload file to Space 'from folder/file','space to put','name it will become'
  16.  
  17.  
  18. def run(upfile):
  19.     client.upload_file(upfile, 'yl2-test', 'test/storage-test')
  20.    
  21. from django.shortcuts import render
  22. from django.shortcuts import redirect
  23. from django.contrib.auth import authenticate, login
  24. from django.http import HttpResponseRedirect
  25.  
  26. from .forms import CharacterForm, SignUpForm
  27.  
  28. from scripts.test_upload import run
  29.  
  30.  
  31. def index(request):
  32.     return render(request, 'YL2/index.html')
  33.  
  34.  
  35. def new_char(request):
  36.     # return render(request, 'YL2/new_char.html')
  37.     # if this is a POST request we need to process the form data
  38.     if request.method == 'POST':
  39.         # create a form instance and populate it with data from the request:
  40.         form = CharacterForm(request.POST, request.FILES)
  41.         # check whether it's valid:
  42.         if form.is_valid():
  43.             # process the data in form.cleaned_data as required
  44.             run(request.FILES['char_file'])
  45.             run(request.FILES['char_image'])
  46.             # redirect to a (success page) new URL:
  47.             return HttpResponseRedirect('')
  48.  
  49.     # if a GET (or any other method) we'll create a blank form
  50.     else:
  51.         form = CharacterForm()
  52.  
  53.     return render(request, 'YL2/new_char.html', {'form': form})
  54.  
  55.  
  56. def log_in_page(request):
  57.     username = request.POST['username']
  58.     password = request.POST['password']
  59.     user = authenticate(request, username=username, password=password)
  60.     if user is not None:
  61.         login(request, user)
  62.         return redirect('index')
  63.     else:
  64.         return HttpResponseRedirect('Login error. Try again.')
  65.  
  66.  
  67. def SignUp(request):
  68.     if request.method == 'POST':
  69.         form = SignUpForm(request.POST)
  70.         if form.is_valid():
  71.             form.save()
  72.             username = form.cleaned_data.get('username')
  73.             raw_password = form.cleaned_data.get('password1')
  74.             user = authenticate(username=username, password=raw_password)
  75.             login(request, user)
  76.             return redirect('index')
  77.     else:
  78.         form = SignUpForm()
  79.     return render(request, 'signup.html', {'form': form})
  80.    
  81. from django.db import models
  82. from django.contrib.auth.models import User
  83. from django.db.models.signals import post_save
  84. from django.dispatch import receiver
  85.  
  86.  
  87. class UserProfile(models.Model):
  88.     # -----
  89.  
  90.  
  91. class Character(models.Model):
  92.     user = models.ForeignKey(User, on_delete=models.CASCADE)
  93.     char_name = models.CharField(max_length=100)
  94.     char_image = models.ImageField()
  95.     char_file = models.FileField(max_length=100)
  96.     # upload_to='uploads/'
  97.  
  98. @receiver(post_save, sender=User)
  99. def create_user_profile(sender, instance, created, **kwargs):
  100.     if created:
  101.         UserProfile.objects.create(user=instance)
  102.  
  103.  
  104. @receiver(post_save, sender=User)
  105. def save_user_profile(sender, instance, **kwargs):
  106.     instance.userprofile.save()
  107.    
  108. from django import forms
  109. from django.contrib.auth.forms import UserCreationForm
  110. from django.contrib.auth.models import User
  111.  
  112.  
  113. class CharacterForm(forms.Form):
  114.     char_name = forms.CharField(label='Character Name', max_length=100)
  115.     char_image = forms.ImageField(label='Character Image')
  116.     char_file = forms.FileField(label='Character File')
  117.  
  118.  
  119. class SignUpForm(UserCreationForm):
  120.     email = forms.EmailField(max_length=254, help_text='Required. Input a valid email address.')
  121.  
  122.     class Meta:
  123.         model = User
  124.         fields = ('username', 'email', 'password1', 'password2', )
  125.    
  126. <h1>New Character for: {{ user.username }}</h1>
  127.  
  128.  
  129. <form enctype="multipart/form-data" action="/new_char/" method="post">
  130.     {% csrf_token %}
  131.     {{ form.as_ul }}<br><br>
  132.     <input type="submit" value="Submit" />
  133. </form>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top