def home(request): local_ip = socket.gethostbyname(socket.gethostname()) cpu = psutil.cpu_percent() memory = psutil.virtual_memory() memory_total = memory.total memory_used = memory.used try: location = Location.objects.get(IPAddress=local_ip) location.RunningState = True location.CPULoad = cpu location.TotalMemory = memory_total / 1024 / 1024 location.MemoryLoad = memory_used / 1024 / 1024 location.LastRefresh = datetime.now() location.save() except: location = Location( Name="", Latitude=0, Longitude=0, RunningState=True, IPAddress=local_ip, CPULoad=cpu, TotalMemory=memory_total, MemoryLoad=memory_used, LastRefresh=datetime.now() ) location.save() finally: user_id = request.session.get("user") user = None if user_id is not None: user = User.objects.get(pk=user_id) return render(request, "home.html", {'user': user})
def editProfile(request): title="Edit Profile" if request.method == 'POST': userform = ProfileForm(request.POST) # A form bound to the POST data adressform = AdressForm(request.POST) if userform.is_valid(): # All validation rules pass oldpassword = userform.cleaned_data['oldpassword'] password = userform.cleaned_data['password'] secpassword = userform.cleaned_data['secpassword'] email = userform.cleaned_data['email'] if adressform.is_valid(): publicadress = adressform.cleaned_data['publicAdress'] country = adressform.cleaned_data['country'] city = adressform.cleaned_data['city'] zipcode = adressform.cleaned_data['zipcode'] street = adressform.cleaned_data['street'] housenumber = adressform.cleaned_data['housenumber'] userprofile = request.user.profile l = Location(country=country, city=city, zipcode=zipcode, street=street, housenumber=housenumber) l.save() userprofile.adress = l userprofile.publicAdress = publicadress userprofile.save() else: user = request.user try: userform = ProfileForm({'email': user.email,}) except: userform = UserForm() try: adressform = AdressForm({ 'publicAdress': user.profile.publicAdress, 'country': user.profile.adress.country, 'city': user.profile.adress.city, 'zipcode': user.profile.adress.zipcode, 'street': user.profile.adress.street, 'housenumber': user.profile.adress.housenumber}) except: adressform = AdressForm() context = util.generateContext(request, contextType = 'RequestContext', userform=userform, adressform=adressform, title=title) return render_to_response('usermanag/edit.html', context)
def handle(self, *args, **options): if len(args) > 0: raise CommandError('need exactly zero arguments') f = open('{0}/cleaned_list'.format(os.getcwd()), "r") lines = f.readlines() f.close() length = len(lines) print "Generating 10 States" for i in range(10): s = State() s.name = lines[randrange(1, length)-1] s.temporary = choice([True, False]) s.save() print "Generating 10 Categories" for i in range(10): c = Category() c.name = lines[randrange(1, length)-1] c.save() print "Generating 10 Conditions" for i in range(10): c = Condition() c.name = lines[randrange(1, length)-1] c.save() print "Generating 4000 Hardwares" for i in range(4000): h = Hardware() h.name = lines[randrange(1, length)-1] h.state = choice(State.objects.all()) h.condition = choice(Condition.objects.all()) h.category = choice(Category.objects.all()) h.description = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nibh justo, ornare semper pharetra id, laoreet quis ligula. Mauris non sem elit, nec ornare lectus. Duis sit amet dolor eleifend tellus congue auctor eget a augue. Fusce aliquam, ipsum sit amet imperdiet viverra, neque nisl aliquet arcu, quis tristique libero diam a sem. Curabitur quis elementum enim. Cras id nulla a arcu sollicitudin condimentum vestibulum ac risus. In tincidunt hendrerit mollis. Nunc sed nulla nibh, aliquam aliquam neque. Aenean eget cursus dui. Ut a purus neque. Proin eu eros elit. Suspendisse nisl enim, sagittis vel vulputate eu, pretium vitae ligula. Morbi posuere luctus dolor, sit amet tempus dolor vestibulum id. In ornare, risus eget mattis varius, dui tellus dapibus ligula, vel adipiscing risus elit non risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. """ h.owner = choice(User.objects.all()) if choice([True, False]): h.location = h.owner.get_profile().location else: l = Location() l.latitude = uniform(7.5 ,13.6) l.longitude = uniform(47.5, 53.8) l.save() h.location = l h.save()
def handle(self, *args, **options): if len(args) > 0: raise CommandError('need exactly zero arguments') f = open('{0}/cleaned_list'.format(os.getcwd()), "r") lines = f.readlines() f.close() length = len(lines) print "Generating 200 User" for i in range(400): u = User.objects.create_user(lines[randint(0, length-1)].strip()+str(randint(1,100))+str(randint(1,100))) u.save() profile = u.get_profile() l = Location() l.latitude = uniform(7.5 ,13.6) l.longitude = uniform(47.5, 53.8) l.save() profile.location = l profile.displayLocation = choice([True, False]) profile.save()
def settings(request): """displays the settings for an account""" user = request.user profile = user.get_profile() accounts = SocialAccount.objects.filter(user=user) accountlist = [] for account in accounts: accountlist.append(account.provider) apps = SocialApp.objects.all() context = {} if request.method == 'POST': # If the form has been submitted... if request.POST["type"] == "location": lform = LocationForm(request.POST) mform = UserSettingsForm() if lform.is_valid(): # All validation rules pass if profile.location==None: location = Location() location.save() profile.location = location profile.save() else: location = profile.location location.city = lform.cleaned_data['city'] location.street = lform.cleaned_data['street'] location.postcode = lform.cleaned_data['postcode'] profile.displayLocation = lform.cleaned_data['displayLocation'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": try: places = g.geocode(location.street + ", " + location.postcode + " " + location.city, exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] location.save() except geocoders.google.GQueryError, e: messages.add_message(request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?") else: location.latitude = None location.longitude = None location.save() profile.save() else: lform = LocationForm() mform = UserSettingsForm(request.POST) mform.user = request.user if mform.is_valid(): if mform.cleaned_data['email'] != user.email: set_mail(user, mform.cleaned_data['email']) if mform.cleaned_data['displayname'] != profile.displayname: profile.displayname = mform.cleaned_data['displayname'] profile.save() user.save()
def hardwareEdit(request, id=None): user = request.user profile = user.get_profile() if user.email != "" and profile.mail_confirmed: if id==None: #Create new hardware if request.method == 'POST': # If the form has been submitted... form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass h = Hardware() h.name = form.cleaned_data['name'] h.description = form.cleaned_data['description'] h.condition = form.cleaned_data['condition'] h.category = form.cleaned_data['category'] h.state = get_object_or_404(State, id=form.cleaned_data['state']) h.owner = user if form.cleaned_data['ownlocation']: location = Location() location.city = form.cleaned_data['city'] location.street = form.cleaned_data['street'] location.postcode = form.cleaned_data['postcode'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city'] places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] else: location.latitude = None location.longitude = None location.save() h.location = location else: h.location = profile.location if h.state.temporary and form.cleaned_data['lendlength'] != None: h.lendlength = form.cleaned_data['lendlength'] * form.cleaned_data['lendlengthtype'] h.save() messages.add_message(request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.") context = {'form':form, 'hardware':h} return render_to_response('hardware/imageform.html', context, RequestContext(request)) else: form = HardwareForm() context = {'form':form, 'edit':False} return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) else: #edit existing hardware hardware = get_object_or_404(Hardware, id=id) if user == hardware.owner: if request.method == 'POST': form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): hardware.name = form.cleaned_data['name'] hardware.description = form.cleaned_data['description'] hardware.condition = form.cleaned_data['condition'] hardware.category = form.cleaned_data['category'] hardware.state = get_object_or_404(State, id=form.cleaned_data['state']) hardware.owner = user if form.cleaned_data['ownlocation']: location = Location() location.city = form.cleaned_data['city'] location.street = form.cleaned_data['street'] location.postcode = form.cleaned_data['postcode'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city'] places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] else: location.latitude = None location.longitude = None location.save() hardware.location = location else: hardware.location = profile.location hardware.save() print hardware.id messages.add_message(request, messages.SUCCESS, "Hardware wurde erfolgreich bearbeitet.") return HttpResponseRedirect(reverse(displayHardware, args=[hardware.id, hardware.name])) # Redirect after POST else: form = HardwareForm() form.initial["name"] = hardware.name form.initial["description"] = hardware.description form.initial["condition"] = hardware.condition form.initial["category"] = hardware.category form.initial["state"] = hardware.state.id images = MultiuploaderImage.objects.filter(hardware=hardware.id) context = {'form':form, 'hardware':hardware, 'edit':True, 'images':images} return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) else: return HttpResponseForbidden(loader.get_template("403.html").render(RequestContext({}))) else: return render_to_response('hardware/hardwareform.html', {"invalidmail":True} , RequestContext(request))
def hardwareEdit(request, id=None): user = request.user profile = user.get_profile() if user.email != "" and profile.mail_confirmed: if id == None: # Create new hardware if request.method == "POST": # If the form has been submitted... form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass h = Hardware() h.name = form.cleaned_data["name"] h.description = form.cleaned_data["description"] h.condition = form.cleaned_data["condition"] h.category = form.cleaned_data["category"] h.state = get_object_or_404(State, id=form.cleaned_data["state"]) h.owner = user if form.cleaned_data["ownlocation"]: location = Location() location.city = form.cleaned_data["city"] location.street = form.cleaned_data["street"] location.postcode = form.cleaned_data["postcode"] g = geocoders.Google() if location.city != "" or location.street != "" or location.street != "": searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data["city"] try: places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] location.save() except geocoders.google.GQueryError, e: messages.add_message( request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?", ) else: location.latitude = None location.longitude = None location.save() h.location = location else: h.location = profile.location if h.state.temporary and form.cleaned_data["lendlength"] != None: h.lendlength = form.cleaned_data["lendlength"] h.lendlengthtype = form.cleaned_data["lendlengthtype"] h.save() messages.add_message( request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.", ) context = {"form": form, "hardware": h} return render_to_response("hardware/imageform.html", context, RequestContext(request)) else: form = HardwareForm() context = {"form": form, "edit": False} return render_to_response("hardware/hardwareform.html", context, RequestContext(request)) else: # edit existing hardware hardware = get_object_or_404(Hardware, id=id) if user == hardware.owner: if request.method == "POST": form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): hardware.name = form.cleaned_data["name"] hardware.description = form.cleaned_data["description"] hardware.condition = form.cleaned_data["condition"] hardware.category = form.cleaned_data["category"] hardware.state = get_object_or_404(State, id=form.cleaned_data["state"]) hardware.owner = user if form.cleaned_data["ownlocation"]: location = Location() location.city = form.cleaned_data["city"] location.street = form.cleaned_data["street"] location.postcode = form.cleaned_data["postcode"] g = geocoders.Google() if location.city != "" or location.street != "" or location.street != "": searchstring = ( location.street + ", " + location.postcode + " " + form.cleaned_data["city"] ) places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] else: location.latitude = None location.longitude = None location.save() hardware.location = location else: hardware.location = profile.location if hardware.state.temporary and form.cleaned_data["lendlength"] != None: hardware.lendlength = form.cleaned_data["lendlength"] hardware.lendlengthtype = form.cleaned_data["lendlengthtype"] hardware.save() messages.add_message(request, messages.SUCCESS, "Hardware wurde erfolgreich bearbeitet.") return HttpResponseRedirect( reverse(displayHardware, args=[hardware.id, hardware.slug]) ) # Redirect after POST else: form = HardwareForm() form.initial["name"] = hardware.name form.initial["description"] = hardware.description form.initial["condition"] = hardware.condition form.initial["category"] = hardware.category form.initial["state"] = hardware.state.id form.initial["lendlength"] = hardware.lendlength form.initial["lendlengthtype"] = hardware.lendlengthtype images = MultiuploaderImage.objects.filter(hardware=hardware.id) context = {"form": form, "hardware": hardware, "edit": True, "images": images} return render_to_response("hardware/hardwareform.html", context, RequestContext(request)) else: return HttpResponseForbidden(loader.get_template("403.html").render(RequestContext({})))
def test_can_create_and_save_unicode_location(self): l = Location(name=u"ó", company=self.c) l.save()
def test_can_serialize_unicode_location(self): l = Location(name=u"ó", company=self.c) l.save() s = LocationSerializer(instance=l)
def hardwareEdit(request, id=None): user = request.user profile = user.get_profile() if user.email != "" and profile.mail_confirmed: if id==None: #Create new hardware if request.method == 'POST': # If the form has been submitted... form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass h = Hardware() h.name = form.cleaned_data['name'] h.description = form.cleaned_data['description'] h.condition = form.cleaned_data['condition'] h.category = form.cleaned_data['category'] h.state = get_object_or_404(State, id=form.cleaned_data['state']) h.owner = user if form.cleaned_data['ownlocation']: location = Location() location.city = form.cleaned_data['city'] location.street = form.cleaned_data['street'] location.postcode = form.cleaned_data['postcode'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city'] try: places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] location.save() except geocoders.google.GQueryError, e: messages.add_message(request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?") context = {'form':form, 'edit':False} return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) except: messages.add_message(request, messages.ERROR, u"Es gab einen Fehler beim überprüfen des Ortes. Hast du dich vielleicht vertippt?") context = {'form':form, 'edit':False} return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) else: location.latitude = None location.longitude = None location.save() h.location = location else: h.location = profile.location if h.state.temporary and form.cleaned_data['lendlength'] != None: h.lendlength = form.cleaned_data['lendlength'] h.lendlengthtype = form.cleaned_data['lendlengthtype'] h.save() messages.add_message(request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.") context = {'form':form, 'hardware':h} return render_to_response('hardware/imageform.html', context, RequestContext(request))
def settings(request): """displays the settings for an account""" user = request.user profile = user.get_profile() accounts = SocialAccount.objects.filter(user=user) accountlist = [] for account in accounts: accountlist.append(account.provider) apps = SocialApp.objects.all() context = {} if request.method == 'POST': # If the form has been submitted... if request.POST["type"] == "location": lform = LocationForm(request.POST) mform = UserSettingsForm() if lform.is_valid(): # All validation rules pass if profile.location == None: location = Location() location.save() profile.location = location profile.save() else: location = profile.location location.city = lform.cleaned_data['city'] location.street = lform.cleaned_data['street'] location.postcode = lform.cleaned_data['postcode'] profile.displayLocation = lform.cleaned_data['displayLocation'] g = geocoders.Google() if location.city != "" or location.street != "" or location.street != "": try: places = g.geocode(location.street + ", " + location.postcode + " " + location.city, exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] location.save() except geocoders.google.GQueryError, e: messages.add_message( request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?" ) else: location.latitude = None location.longitude = None location.save() profile.save() else: lform = LocationForm() mform = UserSettingsForm(request.POST) mform.user = request.user if mform.is_valid(): if mform.cleaned_data['email'] != user.email: set_mail(user, mform.cleaned_data['email']) if mform.cleaned_data['displayname'] != profile.displayname: profile.displayname = mform.cleaned_data['displayname'] profile.save() user.save()
return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) else: #edit existing hardware hardware = get_object_or_404(Hardware, id=id) if user == hardware.owner: if request.method == 'POST': form = HardwareForm(request.POST) # A form bound to the POST data if form.is_valid(): hardware.name = form.cleaned_data['name'] hardware.description = form.cleaned_data['description'] hardware.condition = form.cleaned_data['condition'] hardware.category = form.cleaned_data['category'] hardware.state = get_object_or_404(State, id=form.cleaned_data['state']) hardware.owner = user if form.cleaned_data['ownlocation']: location = Location() location.city = form.cleaned_data['city'] location.street = form.cleaned_data['street'] location.postcode = form.cleaned_data['postcode'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city'] try: places = g.geocode(urllib.quote(searchstring), exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] except geocoders.google.GQueryError, e: messages.add_message(request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?") context = {'form':form, 'hardware':hardware, 'edit':True} return render_to_response('hardware/hardwareform.html', context, RequestContext(request)) except:
def create_property(request): current_user = request.user if request.method == 'POST': form = Property_form(request.POST, request.FILES) if form.is_valid(): # Change this to make it user specific d = Dashboard.objects.get(user=current_user.id) # Enter property info on dashboard num = form.cleaned_data['num'] street = form.cleaned_data['street'] post_code = form.cleaned_data['post_code'] suburb = form.cleaned_data['suburb'] price = form.cleaned_data['price'] num_guests = form.cleaned_data['num_guests'] num_rooms = form.cleaned_data['num_rooms'] desc = form.cleaned_data['description'] # get full address, longitude and latitude try: geo_location = Nominatim(timeout=10) geo_location = geo_location.geocode( str(num) + " " + street + " " + suburb + " " + str(post_code), "NSW") longitude = float(geo_location.longitude) latitude = float(geo_location.latitude) except AttributeError: messages.error(request, "Cannot find location, try again") return HttpResponseRedirect('/create_property') l = Location(num=num, street=street, post_code=post_code, suburb=suburb, longitude=longitude, latitude=latitude) l.save() p = Property( dashboard=d, location=l, price=price, num_guests=num_guests, num_rooms=num_rooms, description=desc, free_parking=form.cleaned_data['free_parking'], pool=form.cleaned_data['pool'], gym=form.cleaned_data['gym'], spa=form.cleaned_data['spa'], ramp=form.cleaned_data['ramp'], travelator=form.cleaned_data['travelator'], elevator=form.cleaned_data['elevator'], # property Type property_type=form.cleaned_data['property_type'], # amenities kitchen=form.cleaned_data['kitchen'], airconditioning=form.cleaned_data['airconditioning'], bathroom=form.cleaned_data['bathroom'], tv=form.cleaned_data['tv']) p.save() # create image models and save them to db for f in request.FILES.getlist('image'): img = image(property=p, image=f) img.save() return HttpResponseRedirect('/dashboard') else: form = Property_form() return render(request, "main/property_form.html", {'form': form})
def settings(request): """displays the settings for an account""" user = request.user profile = user.get_profile() accounts = SocialAccount.objects.filter(user=user) accountlist = [] for account in accounts: accountlist.append(account.provider) apps = SocialApp.objects.all() context = {} if request.method == 'POST': # If the form has been submitted... if request.POST["type"] == "location": lform = LocationForm(request.POST) mform = UserSettingsForm() if lform.is_valid(): # All validation rules pass if profile.location==None: location = Location() location.save() profile.location = location profile.save() else: location = profile.location location.city = lform.cleaned_data['city'] location.street = lform.cleaned_data['street'] location.postcode = lform.cleaned_data['postcode'] profile.displayLocation = lform.cleaned_data['displayLocation'] g = geocoders.Google() if location.city!= "" or location.street!="" or location.street!="": places = g.geocode(location.street + ", " + location.postcode + " " + location.city, exactly_one=False) location.latitude = places[0][1][0] location.longitude = places[0][1][1] else: location.latitude = None location.longitude = None location.save() profile.save() print profile.location.city else: lform = LocationForm() mform = UserSettingsForm(request.POST, request.FILES) if mform.is_valid(): print "valid" if 'avatar' in request.FILES: profile.avatar = request.FILES["avatar"] profile.save() print "file saved" if mform.cleaned_data['email'] != user.email: set_mail(user, mform.cleaned_data['email']) if mform.cleaned_data['displayname'] != profile.displayname: profile.displayname = mform.cleaned_data['displayname'] profile.save() user.save() else: lform = LocationForm() mform = UserSettingsForm() if not profile.mail_confirmed: context["mailconfirm"] = "We sent you a confirmation link. Please check your mail." context.update({"apps":apps, "accountlist":accountlist, 'profile':profile, 'lform':lform, 'mform':mform}) map, showmap = create_map(profile.location) context["map"] = map context["showmap"] = showmap return render_to_response('users/usersettings.html', context, RequestContext(request))
from datetime import datetime import pytz from django.utils import timezone from main.models import User, Property, Booking, \ Experience, Location, Rating, Review, Gallery user1 = User( firstName="Raymond", lastName="Yau", email="*****@*****.**", ) user1.save() location1 = Location( city='Glasgow', country='UK', imgUrl="https://via.placeholder.com/300/0000FF/808080?text=GlasgowCityImage" ) location1.save() location2 = Location( city='Edinburgh', country='UK', imgUrl= "https://via.placeholder.com/300/0000FF/808080?text=EdinburghCityImage") location2.save() location3 = Location( city='Highclere',
# Initialize database # MeetUp from main.models import Location, Person, Event, UserEventData from django.utils import timezone from datetime import datetime import pytz # Create some locations # Andy's House l1 = Location(latitude=40.451891, longtitude=-79.950388) l1.save() # Create some users u1 = Person(first_name='Andy', last_name='Wang', email='*****@*****.**', phone='(412)478-2331', sign_up_time=timezone.now(), location=l1, location_last_updated=timezone.now()) u1.save() # Create some events # e1 = Event(name='MeetUp meeting', # time=datetime(2014, 7, 27, 14, 00, 00, 00 pytz.EST), # location_name='Gates 7th floor', # location=Location(latitude=40.444046, logntitude=-79.944414), # )