python - implement django user (Staff member) to handle some user's informations -
i developing website allows management of reservation of cars users. created first app manages user accounts such :
registration email confirmation / login / logout ...etc i've created special user have staff permissions
during registration, each new user must load pdf documents, these documents verified staff user
if uploaded documents good, member sends user pdf_contract signed electronically
my question is how display files uploaded users staff user, can check , send them pdf document.
my code:
views.py
from mysite.core.forms import signupform, profileform, documentform mysite.core.models import profile mysite.core.tokens import account_activation_token django.views.generic.list import listview django.views.generic.detail import detailview # create views here. def index(request): return render(request, 'index.html') @login_required def home(request): return render(request, 'home.html') def signup(request): if request.method == 'post': form = signupform(request.post) if form.is_valid(): user = form.save(commit=false) user.is_active = false user.save() current_site = get_current_site(request) subject = 'activate mysite account' message = render_to_string('account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) user.email_user(subject, message) return redirect('account_activation_sent') else: form = signupform() return render(request, 'signup.html', {'form': form}) def account_activation_sent(request): return render(request, 'account_activation_sent.html') def activate(request, uidb64, token): try: uid = force_text(urlsafe_base64_decode(uidb64)) user = user.objects.get(pk=uid) except (typeerror, valueerror, overflowerror, user.doesnotexist): user = none if user not none , account_activation_token.check_token(user, token): user.is_active = true user.profile.email_confirmed = true user.save() login(request, user) return render(request, 'email_confirmation_done.html') else: return render(request, 'account_activation_invalid.html') def email_confirmation_done(request): return render(request, 'email_confirmation_done.html' ) def complete_profile(request): if request.user.is_authenticated(): if request.method == 'post': form = profileform(request.post, instance=request.user.profile) if form.is_valid(): profile = form.save(commit=false) profile.profile_completed = true profile.save() return redirect('upload_files') else: form = profileform() return render(request, 'complete_profile.html', {'form': form}) else: return redirect('login') def upload_files(request): if request.user.is_authenticated(): if request.method == 'post': form = documentform(request.post, request.files, instance=request.user.profile) if form.is_valid(): form.save() return redirect('home') else: form = documentform() return render(request, 'upload_files.html', {'form': form}) else: return redirect('login')
models.py
from django.db import models django.contrib.auth.models import user django.db.models.signals import post_save django.dispatch import receiver class profile(models.model): user = models.onetoonefield(user, on_delete=models.cascade) birth_date = models.datefield(('date de naissance'), null=true, blank=true) birth_place = models.charfield(('lieu de naissance'), max_length=80 ) email_confirmed = models.booleanfield(('email confirmé'), default=false) adress = models.charfield(('adresse'), max_length=80) adress_complement = models.charfield(('complément d\'adresse'), max_length=80) city = models.charfield(('ville'), max_length=80) zip_code = models.integerfield(('code postal'), null=true, blank=true) drive_licence_num = models.charfield(('n° du permis de conduire'), max_length=80) social_secu_num = models.integerfield(('n° de sécurité sociale'), null=true, blank=true) phone_number = models.integerfield(('n° de téléphone'), null=true, blank=true) profile_completed = models.booleanfield(('profile completé'), default=false) #documents upload id_card = models.filefield(('carte nationale d\'identité'), upload_to='documents/cni') drive_licence = models.filefield(('permis de conduire'), upload_to='documents/permis_conduire') police_record = models.filefield(('casier judiciaire'), upload_to='documents/casier_judiciaire') carte_vitale = models.filefield(('carte vitale'), upload_to='documents/carte_vitale') medical_visit = models.filefield(('visite médicale'), upload_to='documents/medical_visit') rib = models.filefield(('relevé d\'identité bancaire (rib)'), upload_to='documents/rib') uploaded_at = models.datetimefield(('ajouté le'), auto_now_add=true) docs_are_checked = models.booleanfield(('documents verifés'), default=false) def __str__(self): return self.user.username @receiver(post_save, sender=user) def update_user_profile(sender, instance, created, **kwargs): if created: profile.objects.create(user=instance) instance.profile.save()
app/urls.py
from django.conf.urls import url, include django.contrib.auth import views auth_views . import views core_views urlpatterns = [ url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), url(r'^accounts/logout/$', auth_views.logout, {'next_page': 'login'}, name='logout'), url(r'^accounts/signup/$', core_views.signup, name='signup'), url(r'^account_activation_sent/$', core_views.account_activation_sent, name='account_activation_sent'), url(r'^accounts/activate/(?p<uidb64>[0-9a-za-z_\-]+)/(?p<token>[0-9a-za- z]{1,13}-[0-9a-za-z]{1,20})/$', core_views.activate, name='activate'), url(r'^accounts/email_confirmation_done/$', core_views.email_confirmation_done, name='email_confirmation_done'), url(r'^accounts/complete_profile/$', core_views.complete_profile, name='complete_profile'), url(r'^accounts/upload_files/$', core_views.upload_files, name='upload_files'),
forms.py
from django import forms django.contrib.auth.forms import usercreationform django.contrib.auth.models import user mysite.core.models import profile class signupform(usercreationform): email = forms.emailfield(max_length=254, help_text='required. inform valid email address.') class meta: model = user fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2',) def clean_email(self): email = self.cleaned_data.get("email") qs = user.objects.filter(email__iexact=email) if qs.exists(): raise forms.validationerror("cannot use email. it's registered") return email class profileform(forms.modelform): class meta: model = profile fields = ('birth_date','birth_place', 'adress', 'adress_complement', 'city', 'zip_code', 'drive_licence_num', 'social_secu_num', 'phone_number') help_texts = { 'birth_date': 'format aaaa/mm/jj', 'birth_place': 'indiquer pays si né à l\'étranger', 'adress': 'n°,rue,boulevard,nom, (ex: 01 rue de craiova)', 'adress_complement': 'res, bât, apprt, bp, chez...', } class documentform(forms.modelform): class meta: model = profile fields = ('id_card', 'drive_licence', 'police_record', 'carte_vitale', 'medical_visit', 'rib',) help_texts = { 'id_card': '*required recto / verso', }
Comments
Post a Comment