Browse Source

Set uploader to current user in HistoryUpload (#57)

master
Kevin Mok 6 years ago
parent
commit
93e646565a
  1. 4
      login/forms.py
  2. 2
      login/models.py
  3. 14
      login/utils.py
  4. 13
      login/views.py

4
login/forms.py

@ -4,5 +4,5 @@ from .models import HistoryUpload
class HistoryUploadForm(forms.ModelForm):
class Meta:
model = HistoryUpload
fields = ('user_id', 'document', )
# widgets = { 'user_id': forms.HiddenInput() }
fields = ('user', 'document', )
widgets = { 'user': forms.HiddenInput() }

2
login/models.py

@ -22,6 +22,6 @@ class User(models.Model):
return self.id
class HistoryUpload(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
document = models.FileField(upload_to='history/')
uploaded_at = models.DateTimeField(auto_now_add=True)

14
login/utils.py

@ -13,7 +13,6 @@ def get_user_context(user_obj):
"""
return { 'user_id': user_obj.id, 'user_secret': user_obj.secret, }
# generate_random_string {{{ #
def generate_random_string(length):
@ -67,3 +66,16 @@ def create_user(refresh_token, access_token, access_expires_in):
# }}} create_user #
def get_scan_context(request):
"""Get context for rendering scan page.
:request:
:returns: Context with upload form and user info.
"""
context = { 'user_id': request.session['user_id'],
'user_secret': request.session['user_secret'], }
# set hidden user field to current user
context['form'] = HistoryUploadForm(initial=
{ 'user': User.objects.get(id=request.session['user_id']) })
return context

13
login/views.py

@ -79,10 +79,7 @@ def callback(request):
request.session['user_id'] = user_obj.id
request.session['user_secret'] = user_obj.secret
context = get_user_context(user_obj)
context['form'] = HistoryUploadForm()
return render(request, 'login/scan.html', context)
return render(request, 'login/scan.html', get_scan_context(request))
# }}} callback #
@ -105,14 +102,8 @@ def admin_graphs(request):
def upload_history(request):
if request.method == 'POST':
form = HistoryUploadForm(request.POST, request.FILES)
form.fields['user_id'].initial = User.objects.get(id=request.session['user_id'])
if form.is_valid():
form.save()
# Redirect to the document list after POST
return redirect('graphs:display_history_table')
else:
form = HistoryUploadForm()
# return redirect('graphs:display_history_table')
return render(request, 'login/scan.html', context)
return render(request, 'login/scan.html', get_scan_context(request))
Loading…
Cancel
Save