def change_pwd(request, pk): tmp = get_name() template = loader.get_template('users/changepwd.html') ins = get_object_or_404(User, pk=pk) if request.method == 'POST': form = PasswordChangeForm(ins, request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) message = 'Los datos se guardaron correctamente!' tpl = loader.get_template('messages/message.html') contextSuccess = { 'title': get_body(tmp[3], tmp[0]), 'uri': get_url('users'), 'message': message, } return HttpResponse(tpl.render(contextSuccess, request)) else: form = PasswordChangeForm(ins) context = { 'title': get_body(tmp[9], tmp[9]), 'form': form, 'uri': get_url('users'), } return HttpResponse(template.render(context, request))
def edit_level(request, pk): tmp = get_name() template = loader.get_template('levels/edit.html') ins = get_object_or_404(Level, pk=pk) if request.method == 'POST': form = LevelForm(request.POST, instance=ins) if form.is_valid(): form.save() message = 'Los datos se guardaron correctamente!' tpl = loader.get_template('messages/message.html') contextSuccess = { 'title': get_body(tmp[3], tmp[0]), 'uri': get_url('levels'), 'message': message, } return HttpResponse(tpl.render(contextSuccess, request)) # return HttpResponseRedirect(reverse('levels:show')) else: form = LevelForm(instance=ins) context = { 'title': get_body(tmp[3], tmp[0]), 'form': form, 'uri': get_url('levels'), } return HttpResponse(template.render(context, request))
def create_user(request): tmp = get_name() template = loader.get_template('users/add.html') if request.method == 'POST': form = CustomUserCreationForm(request.POST) form1 = ProfileUserForm(request.POST, request.FILES) if form.is_valid() and form1.is_valid(): user = form.save() profile = form1.save(commit=False) profile.user = user profile.save() message = 'Los datos se guardaron correctamente!' tpl = loader.get_template('messages/message.html') contextSuccess = { 'title': get_body(tmp[3], tmp[0]), 'uri': get_url('users'), 'message': message, } return HttpResponse(tpl.render(contextSuccess, request)) else: form = CustomUserCreationForm() form1 = ProfileUserForm() context = { 'title': get_body(tmp[3], tmp[0]), 'form': form, 'form1': form1, 'uri': get_url('users'), } return HttpResponse(template.render(context, request))
def edit_user(request, pk): tmp = get_name() template = loader.get_template('users/edit.html') ins = get_object_or_404(User, pk=pk) ins1 = get_object_or_404(Profile, user=pk) if request.method == 'POST': form = CustomUserChangeForm(request.POST, instance=ins) form1 = ProfileUserForm(request.POST, request.FILES, instance=ins1) if form.is_valid() and form1.is_valid(): form.save() form1.save() message = 'Los datos se guardaron correctamente!' tpl = loader.get_template('messages/message.html') contextSuccess = { 'title': get_body(tmp[3], tmp[0]), 'uri': get_url('users'), 'message': message, } return HttpResponse(tpl.render(contextSuccess, request)) else: form = CustomUserChangeForm(instance=ins) form1 = ProfileUserForm(instance=ins1) context = { 'title': get_body(tmp[3], tmp[0]), 'form': form, 'form1': form1, 'uri': get_url('users'), } return HttpResponse(template.render(context, request))
def login_view(request): tmp = get_name() template = loader.get_template('users/login.html') if not request.user.is_anonymous: return HttpResponseRedirect(reverse('users:show')) if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) profile = Profile.objects.get(user=user.id) profile.last_login = datetime.datetime.now() profile.save() return HttpResponseRedirect(reverse('users:home')) else: form = AuthenticationForm() text = 'El usuario esta inactivo' contextNoActive = { 'title': get_body(tmp[8], tmp[8]), 'text': text, 'form': form, } return HttpResponse(template.render(contextNoActive, request)) # return HttpResponse("Tu cuenta esta inactiva.") else: form = AuthenticationForm() text = 'El usuario o la contraseña son incorrectas' contextNoUser = { 'title': get_body(tmp[8], tmp[8]), 'text': text, 'form': form, } return HttpResponse(template.render(contextNoUser, request)) else: form = AuthenticationForm() text = '' context = { 'title': get_body(tmp[8], tmp[8]), 'form': form, 'text': text, } return HttpResponse(template.render(context, request))
def show_level(request): tmp = get_name() list_title = ['#', 'Descripción', 'Acciones'] template = loader.get_template('levels/show.html') object_list = Level.objects.all() context = { 'title': get_body(tmp[3], tmp[0]), 'object_list': object_list, 'uri': get_url('levels'), 'list_title': list_title, } return HttpResponse(template.render(context, request))
def show_user(request): tmp = get_name() list_title = ['#', 'Usuario', 'Email', 'Nombre', 'Apellido', 'Foto', 'Perfil', 'Estado', 'Ultimo login', 'Acciones'] template = loader.get_template('users/show.html') object_list = User.objects.all() context = { 'title': get_body(tmp[3], tmp[0]), 'object_list': object_list, 'uri': get_url('users'), 'list_title':list_title, } return HttpResponse(template.render(context, request))
def show_profile(request): tmp = get_name() list_title = ['#', 'Usuario', 'Descripción', 'Perfil', 'Foto', 'Acciones'] template = loader.get_template('profiles/show.html') object_list = Profile.objects.all() context = { 'title': get_body(tmp[7], tmp[4]), 'object_list': object_list, 'uri': get_url('users'), 'list_title':list_title, } return HttpResponse(template.render(context, request))
def delete_level(request, pk): tmp = get_name() template = loader.get_template('levels/delete.html') object_list = get_object_or_404(Level, pk=pk) if request.method == 'POST': object_list.delete() return HttpResponseRedirect(reverse('levels:show')) else: context = { 'title': get_body(tmp[3], tmp[0]), 'object_list': object_list, 'uri': get_url('levels'), } return HttpResponse(template.render(context, request))
def create_level(request): tmp = get_name() template = loader.get_template('levels/add.html') if request.method == 'POST': form = LevelForm(request.POST) if form.is_valid(): form.save() message = 'Los datos se guardaron correctamente!' tpl = loader.get_template('messages/message.html') contextSuccess = { 'title': get_body(tmp[3], tmp[0]), 'uri': get_url('levels'), 'message': message, } return HttpResponse(tpl.render(contextSuccess, request)) else: form = LevelForm() context = { 'title': get_body(tmp[3], tmp[0]), 'form': form, 'uri': get_url('levels'), } return HttpResponse(template.render(context, request))
def delete_profile(request, pk): tmp = get_name() template = loader.get_template('profiles/delete.html') object_list = get_object_or_404(Profile, user=pk) if request.method == 'POST': object_list.delete() return HttpResponseRedirect(reverse('users:showprofile')) else: context = { 'title': get_body(tmp[7], tmp[4]), 'object_list': object_list, 'uri': get_url('users'), } return HttpResponse(template.render(context, request))
def show_client(request): tmp = get_name() list_title = [ '#', 'Nombre', 'Apellido', 'Cedula', 'Email', 'Telefono', 'Dirección', 'Fecha cumpleaños', 'Compras', 'Acciones' ] template = loader.get_template('clients/show.html') object_list = Client.objects.all() context = { 'title': get_body(tmp[3], tmp[0]), 'object_list': object_list, 'uri': get_url('clients'), 'list_title': list_title, } return HttpResponse(template.render(context, request))
def create_level_ajax(request): tmp = get_name() template = loader.get_template('levels/modal.html') if request.method == 'POST' and request.is_ajax(): form = LevelForm(request.POST) if form.is_valid(): data = form.save() level_info = { 'id': data.id, 'description': data.description, } return JsonResponse({'level_info': level_info}, status=200) # else: # return JsonResponse({'message': 'Error message'}, status=500) else: form = LevelForm() context = { 'title': get_body(tmp[3], tmp[0]), 'form': form, } return HttpResponse(template.render(context, request))
def show_home(request): tmp = get_name() query1 = Sale.objects.all().aggregate(Sum('net')) query2 = Category.objects.count() query3 = Client.objects.count() query_init = Product.objects.all() query4 = query_init.count() query5 = query_init.order_by('-id')[:5] query6 = query_init.aggregate(Sum('sales')) query7 = query_init.order_by('-sales')[:5] template = loader.get_template('users/dashboard.html') context = { 'title': get_body(tmp[8], tmp[8]), 'total_sales': query1, 'total_category': query2, 'total_client': query3, 'total_product': query4, 'product_list': query5, 'product_sum': query6, 'product_list1': query7, } return HttpResponse(template.render(context, request))