Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from functools import update_wrapper
- from django.conf.urls import url
- from django.contrib import admin
- from django.utils import timezone
- import hashlib
- from django.contrib import messages
- from .models import Users, App_types, Teams, account_types, Configuration
- from actions import export_as_csv_action
- from django.http import HttpResponse, HttpResponseRedirect
- from django.template import RequestContext
- from django.shortcuts import render
- from .forms import NameForm
- from django.core.mail import send_mail
- import datetime
- class taxidrivers_Admin(admin.ModelAdmin):
- change_list_template = 'change_list.html'
- quick_add_template = 'quick_driver_add.html'
- list_display = ('get_code_id', 'get_driver_name','is_active', 'last_update', 'position', 'speed', 'account_type', 'latency', 'client', 'client_version', 'driver_state', 'expiration_date')
- search_fields = ('name', 'surname', 'code_id')
- list_filter = ('last_update','account_type', 'driver_state', 'client', 'client_version','entry_date','expiration_date')
- actions = ['activate_pro','activate_basic']
- #fieldsets = (
- # (None, {
- # 'fields': ('name', 'surname', 'last_update', 'position')
- # }),
- # ('Advanced options', {
- # 'classes': ('collapse',),
- # 'fields': ('speed', 'latency'),
- # }),
- #)
- #filter_horizontal = ('client',)
- def get_code_id(self, obj):
- if obj.code_id == -1:
- id = -1
- else:
- id = obj.code_id + 200
- return id
- get_code_id.short_description = 'CODE ID' # Set the Header label of the results in list
- get_code_id.admin_order_field = 'code_id'
- def is_expired(self, obj):
- if obj.expiration_date > timezone.now():
- return True
- else:
- return False
- is_expired.short_description = 'Expired'
- is_expired.boolean = True
- def get_driver_name(self, obj):
- return u'%s %s ' % (obj.surname, obj.name)
- get_driver_name.short_description = 'Driver Name' # Set the Header label of the results in list
- get_driver_name.admin_order_field = 'surname' # If the get_code_id expression is complex this variable tells which property to use for sorting
- def is_active(self, obj):
- if obj.code_id == -1:
- return False
- else:
- return True
- is_active.boolean = True
- def get_urls(self):
- def wrap(view):
- def wrapper(*args, **kwargs):
- return self.admin_site.admin_view(view)(*args, **kwargs)
- wrapper.model_admin = self
- return update_wrapper(wrapper, view)
- urls = super(taxidrivers_Admin,self).get_urls()
- info = self.model._meta.app_label, self.model._meta.model_name
- my_urls = [
- url(r'^quick_add_driver/$', wrap(self.quick_add_driver), name='%s_%s_quick_add_driver' % info),
- url(r'^download_as_xls/$', self.download_as_xls_view),
- ]
- return my_urls + urls
- def quick_add_driver(self, request):
- if request.method == 'POST':
- # create a form instance and populate it with data from the request:
- form = NameForm(request.POST)
- # check whether it's valid:
- if form.is_valid():
- driver = form.save(commit=False)
- all_drivers = Users.objects.order_by('code_id').all()
- tmp_code_id = 0
- for e in all_drivers:
- if(e.code_id == -1):
- continue
- if(tmp_code_id == e.code_id):
- tmp_code_id += 1
- else:
- driver.code_id = tmp_code_id
- break
- m = hashlib.md5()
- m.update(driver.real_password)
- driver.password = m.hexdigest()
- driver.save()
- send_email(form.data['client'], driver, 'ACTIVATE')
- if '_save' in form.data:
- return HttpResponseRedirect('../')
- elif '_addanother' in form.data:
- form = NameForm()
- elif '_continue' in form.data:
- form = form
- # process the data in form.cleaned_data as required
- # ...
- # redirect to a new URL:
- #return HttpResponseRedirect('../')
- # if a GET (or any other method) we'll create a blank form
- else:
- form = NameForm()
- context = {'title': 'Add TaxiDriver',
- 'opts': Users._meta,
- 'change': False,
- 'add': True,
- 'obj': None,
- 'is_popup': False,
- 'save_as': True,
- 'has_delete_permission': False,
- 'has_add_permission': True,
- 'has_change_permission': True,
- 'form': form,
- }
- return render(request, self.quick_add_template, context)
- def activate_basic(self, request, queryset):
- all_drivers = Users.objects.order_by('code_id').all()
- for td in queryset:
- if td.code_id == -1:
- tmp_code_id = 0
- for e in all_drivers:
- if e.code_id == -1:
- continue
- if tmp_code_id == e.code_id:
- tmp_code_id += 1
- else:
- td.code_id = tmp_code_id
- break
- td.client = App_types.objects.get(pk=0) # For Basic
- td.expiration_date = datetime.datetime.now() + datetime.timedelta(days=35) # Activate for 35 days
- td.save()
- send_email('0', td, 'ACTIVATE')
- else:
- messages.add_message(request, messages.WARNING, 'TaxiDriver (%s) %s %s is already active' % (td.code_id + 200, td.name, td.surname))
- return
- self.message_user(request, "TaxiDrivers Successfully activated as BASIC")
- # queryset.update(status='p')
- activate_basic.short_description = "Activate TaxiDrivers for BASIC"
- def activate_pro(self, request, queryset):
- all_drivers = Users.objects.order_by('code_id').all()
- for td in queryset:
- if td.code_id == -1:
- tmp_code_id = 0
- for e in all_drivers:
- if e.code_id == -1:
- continue
- if tmp_code_id == e.code_id:
- tmp_code_id += 1
- else:
- td.code_id = tmp_code_id
- break
- td.client = App_types.objects.get(pk=1) # For PRO
- td.expiration_date = datetime.datetime.now() + datetime.timedelta(days=35) # Activate for 35 days
- td.save()
- send_email('1', td, 'ACTIVATE')
- else:
- messages.add_message(request, messages.WARNING, 'TaxiDriver (%s) %s %s is already active' % (td.code_id + 200, td.name, td.surname))
- return
- self.message_user(request, "TaxiDrivers Successfully activated as PRO")
- activate_pro.short_description = "Activate TaxiDrivers for PRO"
- def download_as_xls_view(self, request):
- """Generates an xls file
- """
- response = HttpResponse(mimetype='application/ms-excel')
- response['Content-Disposition'] = \
- 'attachment; filename=Report.xls'
- # do something
- return response
- def send_email(client_type, driver, action):
- mail_addresses = []
- if action == 'NEW':
- title = u'Νέος Οδηγός'
- if action == 'ACTIVATE':
- title = u'Ενεργοποίηση Οδηγού'
- if client_type == '0':
- title += u' Basic (%s %s) ' % (driver.surname, driver.name)
- link = u'\n\nLink Taxiplus Basic: http://basic.taxiplus.gr'
- elif client_type == '1':
- title += u' Pro (%s %s) ' % (driver.surname, driver.name)
- link = u'\n\nLink Taxiplus Pro: http://pro.taxiplus.gr'
- elif client_type == '2':
- title = u'Νέος Χρήστης Contacts (%s %s) ' % (driver.surname, driver.name)
- link = u'\n\nLink Taxiplus Contacts: http://api.taxiplus.gr/updates/TaxiplusContacts.apk'
- elif client_type == '3':
- title = u'Νέος Πελάτης (%s %s) ' % (driver.surname, driver.name)
- link = u'\n\nLink Taxiplus Customer: http://api.taxiplus.gr/updates/TaxiplusCustomer.apk'
- body = u'Στοιχεία Οδηγού:\n------------------------------\nΌνομα: %s\nΕπώνυμο: %s\nΟμάδα: %s\nUsername: %s\nPassword: %s\n\n%s\n\n\nΜε εκτίμηση,\nTaxiplus Team' % (driver.name, driver.surname, driver.team.team_name, driver.username, driver.real_password, link)
- su_email = Users.objects.get(pk=0).email.split(',')[0].split(':')[1].strip()
- mail_addresses.append(su_email) # Get the email address of the SU of the system
- taxiplus_support_mail = Configuration.objects.get(parameter='TAXIPLUS_SUPPORT_EMAIL').value.strip()
- mail_addresses.append(taxiplus_support_mail) # Get the email address of TAXIPLUS_SUPPORT
- # Send Mail to Team Leader
- team_leader_mail = Users.objects.get(pk=Teams.objects.get(pk=driver.team.id).leader.id).email.split(',')[0].split(':')[1].strip() # Get the email address of the team leader of the driver
- if team_leader_mail != taxiplus_support_mail:
- mail_addresses.append(team_leader_mail)
- driver_email = driver.email.split(',')[0].split(':')[1].strip()
- if driver_email:
- mail_addresses.append(driver_email) # Add the email address of the driver
- send_mail(
- title,
- body,
- 'support@taxiplus.gr',
- mail_addresses,
- fail_silently=False,
- )
- admin.site.register(Users, taxidrivers_Admin)
- admin.site.register(App_types)
- admin.site.register(Teams)
- admin.site.register(account_types)
- admin.site.register(Configuration)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement