def edit(request, account, tzoffset, date, key): user = get_login_user(request) tzdelta = datetime.timedelta(minutes=int(tzoffset)) if account == user.email(): target = user else: target = users.User(email=account) target = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() event = db.get(key) if not event: return HttpResponse('Calendar event does not exist for key "%s"'%(key,)) start_time, end_time = event.start_time - tzdelta, event.end_time - tzdelta event.start_time, event.end_time = start_time, end_time if request.method == 'POST': form = EventForm(request.POST, instance=event) if form.is_valid(): event1 = form.save(commit=False) if user.email() == account: event.start_time = event1.start_time + tzdelta event.end_time = event1.end_time + tzdelta event.subject, event.description = event1.subject, event1.description event.put() return HttpResponseRedirect(get_frame_url(account, tzoffset, date)) else: form = EventForm(instance=event) return render_to_response('experts/calendaredit.html', {'user': user, 'account': account, 'date': event.start_time.strftime('%A, %d %b'), 'form': form, 'is_my_calendar': bool(user.email() == account), 'person': target.name if user.email() == account else user.name, 'start_time': start_time.strftime('%A, %d %b %I:%M %p '), 'end_time': end_time.strftime('%A, %d %b %I:%M %p ') })
def index(request, account): user = get_login_user(request) now = datetime.datetime.now() return render_to_response('experts/calendar.html', { 'user': user, 'account': account, 'today': now.strftime('%Y-%m-%d') })
def index(request): user = get_login_user(request) if not user.is_active or user.name: popular_topics = _get_popular_topics() featured_experts = _get_featured_experts() return render_to_response('experts/index.html', {'user': user, 'popular_topics': popular_topics, 'featured_experts': featured_experts}) else: return HttpResponseRedirect('/experts/%s/profile?continue=%s'%(user.email(), get_url(request)))
def index(request): user = get_login_user(request) if not user.is_active or user.name: popular_topics = _get_popular_topics() featured_experts = _get_featured_experts() return render_to_response( 'experts/index.html', { 'user': user, 'popular_topics': popular_topics, 'featured_experts': featured_experts }) else: return HttpResponseRedirect('/experts/%s/profile?continue=%s' % (user.email(), get_url(request)))
def index(request, account): user = get_login_user(request) stream = str(random.randint(100000000, 999999999)) token = channel.create_channel(stream) is_my_office = bool(user.email() == account) if is_my_office: profile = user else: target = users.User(email=account) profile = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() return render_to_response('experts/talk.html', {'user': user, 'profile': profile, 'account': account, 'stream': stream, 'token': token, 'is_my_office': is_my_office})
def index(request): user = get_login_user(request) error_message = status = '' q = request.GET.get('q', '') limit = int(request.GET.get('limit', '10')) offset = int(request.GET.get('offset', '0')) if ':' not in q: tags = [x for x in q.split(' ') if x] if tags: query = 'SELECT * FROM User WHERE ' + ' AND '.join( ['tags = :%d' % (i + 1, ) for i, x in enumerate(tags)]) + ' ORDER BY rating DESC' result = db.GqlQuery(query, *tags).fetch(limit, offset) result = [search_result(u) for u in result] else: result = [] else: attr, value = [x.strip() for x in q.split(':', 1)] if attr not in ('name', 'email', 'phone'): error_message = 'Invalid attribute "%s", must be one of name, email or phone.' % ( attr, ) else: if attr == 'name': query, arg = 'SELECT * FROM User WHERE name = :1', value elif attr == 'email': query, arg = 'SELECT * FROM User WHERE account = :1', users.User( email=value) elif attr == 'phone': query, arg = 'SELECT * FROM User WHERE phone_number = :1', value result = db.GqlQuery(query, arg).fetch(limit, offset) result = [search_result(u) for u in result] if not result: error_message = 'No match found. Please enter case sensitive exact value instead of "%s"' % ( value, ) return render_to_response( 'experts/search.html', { 'user': user, 'status': status, 'error_message': error_message, 'query': q, 'result': result })
def edit(request, account, tzoffset, date, key): user = get_login_user(request) tzdelta = datetime.timedelta(minutes=int(tzoffset)) if account == user.email(): target = user else: target = users.User(email=account) target = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() event = db.get(key) if not event: return HttpResponse('Calendar event does not exist for key "%s"' % (key, )) start_time, end_time = event.start_time - tzdelta, event.end_time - tzdelta event.start_time, event.end_time = start_time, end_time if request.method == 'POST': form = EventForm(request.POST, instance=event) if form.is_valid(): event1 = form.save(commit=False) if user.email() == account: event.start_time = event1.start_time + tzdelta event.end_time = event1.end_time + tzdelta event.subject, event.description = event1.subject, event1.description event.put() return HttpResponseRedirect(get_frame_url(account, tzoffset, date)) else: form = EventForm(instance=event) return render_to_response( 'experts/calendaredit.html', { 'user': user, 'account': account, 'date': event.start_time.strftime('%A, %d %b'), 'form': form, 'is_my_calendar': bool(user.email() == account), 'person': target.name if user.email() == account else user.name, 'start_time': start_time.strftime('%A, %d %b %I:%M %p '), 'end_time': end_time.strftime('%A, %d %b %I:%M %p ') })
def index(request): user = get_login_user(request) error_message = status = "" q = request.GET.get("q", "") limit = int(request.GET.get("limit", "10")) offset = int(request.GET.get("offset", "0")) if ":" not in q: tags = [x for x in q.split(" ") if x] if tags: query = ( "SELECT * FROM User WHERE " + " AND ".join(["tags = :%d" % (i + 1,) for i, x in enumerate(tags)]) + " ORDER BY rating DESC" ) result = db.GqlQuery(query, *tags).fetch(limit, offset) result = [search_result(u) for u in result] else: result = [] else: attr, value = [x.strip() for x in q.split(":", 1)] if attr not in ("name", "email", "phone"): error_message = 'Invalid attribute "%s", must be one of name, email or phone.' % (attr,) else: if attr == "name": query, arg = "SELECT * FROM User WHERE name = :1", value elif attr == "email": query, arg = "SELECT * FROM User WHERE account = :1", users.User(email=value) elif attr == "phone": query, arg = "SELECT * FROM User WHERE phone_number = :1", value result = db.GqlQuery(query, arg).fetch(limit, offset) result = [search_result(u) for u in result] if not result: error_message = 'No match found. Please enter case sensitive exact value instead of "%s"' % (value,) return render_to_response( "experts/search.html", {"user": user, "status": status, "error_message": error_message, "query": q, "result": result}, )
def frame(request, account, tzoffset, date): user = get_login_user(request) error_message = status = '' tzdelta = datetime.timedelta(minutes=int(tzoffset)) now = datetime.datetime.now() - tzdelta if date != 'now': yy, mm, dd = map(int, date.split('-', 2)) now = datetime.datetime(year=yy, month=mm, day=dd, hour=now.hour, minute=now.minute, second=now.second) c = MyCalendar(now) text = c.formatmonth(now.year, now.month) if account: if account == user.email(): target = user else: target = users.User(email=account) target = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() if request.method == 'POST' and 'add_event' in request.POST: subject0, time0, duration0, desc0 = [ request.POST.get(x) for x in ('subject', 'time', 'duration', 'description') ] start_time = now.replace(hour=int(time0.split(':')[0]) + (12 if time0.endswith('pm') else 0), minute=int(time0.split(' ')[0].split(':')[1]), second=0, microsecond=0) + tzdelta end_time = start_time + datetime.timedelta(hours=1) # return HttpResponse('start_time=' + str(start_time) + ' end_time=' + str(end_time) # + ' provider=' + account.email() + ' user='******'Added "%s" at %s' % (event.subject, event.start_time.strftime('%I:%M %p')) if request.method == 'GET' and 'delete' in request.GET: key = request.GET.get('delete') event = db.get(key) if event and (event.owner == user.email() or event.visitor == user.email()): status = 'Deleted "%s" at %s' % (event.subject, (event.start_time - tzdelta).strftime('%I:%M %p')) event.delete() else: error_message = 'Cannot delete event with key "%s"' % (key, ) start_time = now.replace(hour=0, minute=0, second=1, microsecond=0) + tzdelta end_time = now.replace(hour=23, minute=59, second=59, microsecond=0) + tzdelta events = db.GqlQuery( 'SELECT * FROM Event WHERE owner = :1 AND start_time >= :2 AND start_time < :3 ORDER BY start_time', account, start_time, end_time).fetch(100) appointments = [] for event in events: is_my_event = bool(event.owner == user.email() or event.visitor == user.email()) description = '%s<br/>%s' % (event.subject or '', event.description or '') if is_my_event else 'Busy' start_time, end_time = (event.start_time - tzdelta).strftime('%I:%M %p'), ( event.end_time - tzdelta).strftime('%I:%M %p') appointments.append({ 'key': event.key(), 'time': '%s-%s' % (start_time[:-3] if start_time[-3:] == end_time[-3:] else start_time, end_time), 'description': description, 'is_my_event': is_my_event, 'person': event.visitor if event.owner == user.email() else event.owner }) prev = now.replace(day=1, month=now.month - 1 if now.month > 1 else 12, year=now.year if now.month > 1 else now.year - 1) next = now.replace(day=1, month=now.month + 1 if now.month < 12 else 1, year=now.year if now.month < 12 else now.year + 1) start_time = now.replace(day=1, hour=0, minute=0, second=1, microsecond=0) + tzdelta end_time = start_time.replace( day=1, month=now.month + 1 if now.month < 12 else 1, year=now.year if now.month < 12 else now.year + 1) + tzdelta events = db.GqlQuery( 'SELECT * FROM Event WHERE owner = :1 AND start_time >= :2 AND start_time < :3 ORDER BY start_time', account, start_time, end_time).fetch(1000) by_day = {} for event in events: start_time = event.start_time - tzdelta if start_time.day not in by_day: by_day[start_time.day] = [] by_day[start_time.day].append(event) for day, event_list in by_day.iteritems(): pattern = '>%d</div>' % (day, ) text = text.replace( pattern, '>%d<br/>%s</div>' % (day, ', '.join([(event.start_time - tzdelta).strftime('%H:%M') for event in event_list]))) return render_to_response( 'experts/calendarframe.html', { 'user': user, 'account': account, 'status': status, 'error_message': error_message, 'calendar': text, 'appointments': appointments, 'date': now.strftime('%A, %d %b, %I:%M %p'), 'availability': target.availability, 'is_my_calendar': bool(user.email() == account), 'prev': get_frame_url(account, tzoffset, prev), 'next': get_frame_url(account, tzoffset, next) })
def command_safe(request, account, command): user = get_login_user(request) is_my_office = bool(user.email() == account) if request.method == 'POST': # for stream in db.GqlQuery('SELECT * FROM ClientStream'): # logging.info(' stream: ' + str(stream)) input = json.loads(request.raw_post_data) if command == 'end': stream = get_stream(input['clientId']) if stream: if stream.visitor and stream.visitor != user.email(): return HttpResponseForbidden() # do not allow removal by others cleanup_stream(stream) if not is_my_office: update_visitors(account, removed=[stream]) elif command == 'accept' or command == 'reject': if not is_my_office: # logging.info('found accept/reject without is_my_office') return HttpResponseForbidden() mine = get_stream(input['clientId']) yours = get_stream(input['targetId']) # logging.info('command=%r\n mine=%r\n yours=%r\n input=%r'%(command, mine, yours, input)) # disconnect previous participant when accepting a new one if mine and command == 'accept' and mine.play and (not yours or mine.play != yours.publish): # logging.info('deleting previous partner %r'%(mine.play,)) previous = get_stream_by_publish(mine.play) if previous: previous.play = None previous.put() send_message(previous, get_disconnect_message()) mine.play = None # now connect mine and yours streams if mine and yours: if command == 'accept' and not mine.play and not yours.play: # logging.info('connecting %r and %r'%(mine.clientId, yours.clientId)) mine.play, yours.play = yours.publish, mine.publish elif command == 'reject' and (mine.play == yours.publish or yours.play == mine.publish): # logging.info('disconnecting %r and %r'%(mine.clientId, yours.clientId)) mine.play = yours.play = None mine.put() yours.put() if command == 'accept': send_message(mine, get_connect_message(yours)) send_message(yours, get_connect_message(mine)) elif command == 'reject': send_message(mine, get_disconnect_message()) send_message(yours, get_disconnect_message()) else: return HttpResponseServerError('Some data on the server is not valid') elif command == 'send': # logging.info(' send input = %r'%(input,)) sender, senderName, text = get_stream(input['senderId']), input['senderName'], input['text'] if sender and sender.visitor and sender.visitor != user.email(): # logging.info('sender is invalid %r, user=%r'%(sender, user.email())) return HttpResponseForbidden() # do not allow by others if 'receiver' in input: # send an inline message to the stream, and on error send back error message receiver = get_stream_by_publish(input['receiver']) if receiver: # logging.info(' sending message to receiver %r'%(receiver,)) send_message(receiver, get_send_message(sender.clientId, senderName, text)) else: # logging.info(' did not find receiver') send_message(sender, get_send_error_message('Did not send message because you are not connected')) if 'owner' in input and input['owner'] == account: # put a message to all streams of this user receivers = get_streams_of_owner(input['owner']) sent_count = 0 for receiver in receivers: input['senderName'] += ' (not connected)' send_message(receiver, get_send_message(sender.clientId, senderName + ' (not connected)', text)) sent_count += 1 # put offline message if it could not be sent to existing streams if sent_count == 0: msg = OfflineMessage(sender=user.email(), senderName=senderName + ' (offline message)', receiver=input['owner'], text=text) msg.put() # also send message to google chat if possible if not is_my_office: send_message_to_google_chat(account, senderName + ' says ' + text) elif command == 'publish': # first delete any expired stream for stream in get_streams_expired(): cleanup_stream(stream) if not stream.is_owner: data = get_userlist_message(removed=[stream]) for owner in get_streams_of_owner(stream.owner): send_message(owner, data) stream = get_stream(input['clientId']) if not stream: # create new Stream object stream = ClientStream(clientId=input['clientId'], visitor=user.email(), name=input['name'], publish=input['url'], owner=account, is_owner=is_my_office) stream.put() # first update user list of owner if not is_my_office: data = get_userlist_message(added=[stream]) found = False for owner in get_streams_of_owner(account): send_message(owner, data) found = True # then send to google chat if account user is not online here if not found: contact = user.email() if user.email() else '@' + stream.clientId msg = '%s visited your video office on %s GMT. Send your reply starting with %s to this person.'%(user.name, datetime.datetime.now(), contact) send_message_to_google_chat(account, msg) else: stream.publish, stream.name = input['url'], input['name'] stream.modified_on = datetime.datetime.now() stream.put() # so that last modified is updated # logging.info('sending userlist and chathistory in response %r %r %r'%(is_my_office, user.email(), account)) if is_my_office: # return user list also visitors = get_streams_of_visitors(account) messages = get_messages_of_owner(account) response = {'userlist': [x.get_object() for x in visitors], 'chathistory': [x.get_object() for x in messages]} if response['chathistory']: db.delete([x for x in messages]) return HttpResponse(json.dumps(response)) return HttpResponse('')
def index(request, account): user = get_login_user(request) if user.email() == account: status = '' if request.method == 'POST': old_tags = user.tags if user and user.tags else [] form = ProfileForm(request.POST, instance=user) if form.is_valid(): old_has_chat = user.has_chat user = form.save(commit=False) if user.tags: user.tags = [x.lower() for x in user.tags if x] user.put() _update_tags_counts(old_tags, user.tags) status = 'Successfully saved the user profile' if not old_has_chat and user.has_chat: if account.endswith('@gmail.com'): xmpp.send_invite(account) status = 'Successfully saved the user profile. Please accept chat invitation from [email protected]' if 'continue' in request.GET: return HttpResponseRedirect(request.GET.get('continue')) else: if not user.name and user.account: user.name = user.account.nickname() form = ProfileForm(instance=user) return render_to_response('experts/myprofile.html', {'user': user, 'account': account, 'status': status, 'form': form, 'website': user.website}) else: status = error_message = '' target = users.User(email=account) profile = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() if profile and user.account and request.method == 'POST' and 'rating' in request.POST: rating, description = int(request.POST.get('rating')), request.POST.get('review') if description == 'Write review here!': description = '' review = db.GqlQuery('SELECT * FROM Review WHERE for_user = :1 AND by_user = :2', profile, user).get() if review: old_rating = review.rating review.rating = rating review.description = description if old_rating != rating: if profile.rating_count == 0: profile.rating_count = 1 profile.rating = (profile.rating*profile.rating_count - old_rating + rating)/profile.rating_count else: review = Review(for_user=profile, by_user=user, rating=rating, description=description) profile.rating = (profile.rating*profile.rating_count + rating)/(profile.rating_count + 1) profile.rating_count += 1 review.put() profile.put() if profile: rating, rating_percent = '%.1f'%(profile.rating or 0.0,), int((profile.rating or 0.0)*20) reviews = db.GqlQuery('SELECT * FROM Review WHERE for_user = :1', profile).fetch(100) for review in reviews: review.rating_percent = int(review.rating * 20) allow_review = bool(user.account) else: rating, rating_percent, reviews, allow_review = 0, 0, [], False return render_to_response('experts/profile.html', {'user': user, 'account': account, 'profile': profile, 'status': status, 'error_message': error_message, 'reviews': reviews, 'rating': rating, 'rating_percent': rating_percent, 'allow_review': allow_review})
def frame(request, account, tzoffset, date): user = get_login_user(request) error_message = status = '' tzdelta = datetime.timedelta(minutes=int(tzoffset)) now = datetime.datetime.now() - tzdelta if date != 'now': yy, mm, dd = map(int, date.split('-', 2)) now = datetime.datetime(year=yy, month=mm, day=dd, hour=now.hour, minute=now.minute, second=now.second) c = MyCalendar(now) text = c.formatmonth(now.year, now.month) if account: if account == user.email(): target = user else: target = users.User(email=account) target = db.GqlQuery('SELECT * FROM User WHERE account = :1', target).get() if request.method == 'POST' and 'add_event' in request.POST: subject0, time0, duration0, desc0 = [request.POST.get(x) for x in ('subject', 'time', 'duration', 'description')] start_time = now.replace(hour=int(time0.split(':')[0])+(12 if time0.endswith('pm') else 0), minute=int(time0.split(' ')[0].split(':')[1]), second=0, microsecond=0) + tzdelta end_time = start_time + datetime.timedelta(hours=1) # return HttpResponse('start_time=' + str(start_time) + ' end_time=' + str(end_time) # + ' provider=' + account.email() + ' user='******'Added "%s" at %s'%(event.subject, event.start_time.strftime('%I:%M %p')) if request.method == 'GET' and 'delete' in request.GET: key = request.GET.get('delete') event = db.get(key) if event and (event.owner == user.email() or event.visitor == user.email()): status = 'Deleted "%s" at %s'%(event.subject, (event.start_time - tzdelta).strftime('%I:%M %p')) event.delete() else: error_message = 'Cannot delete event with key "%s"'%(key,) start_time = now.replace(hour=0, minute=0, second=1, microsecond=0) + tzdelta end_time = now.replace(hour=23, minute=59, second=59, microsecond=0) + tzdelta events = db.GqlQuery('SELECT * FROM Event WHERE owner = :1 AND start_time >= :2 AND start_time < :3 ORDER BY start_time', account, start_time, end_time).fetch(100) appointments = [] for event in events: is_my_event = bool(event.owner == user.email() or event.visitor == user.email()) description = '%s<br/>%s'%(event.subject or '', event.description or '') if is_my_event else 'Busy' start_time, end_time = (event.start_time - tzdelta).strftime('%I:%M %p'), (event.end_time - tzdelta).strftime('%I:%M %p') appointments.append({'key': event.key(), 'time': '%s-%s'%(start_time[:-3] if start_time[-3:] == end_time[-3:] else start_time, end_time), 'description': description, 'is_my_event': is_my_event, 'person': event.visitor if event.owner == user.email() else event.owner }) prev = now.replace(day=1, month=now.month-1 if now.month > 1 else 12, year=now.year if now.month > 1 else now.year - 1) next = now.replace(day=1, month=now.month+1 if now.month < 12 else 1, year=now.year if now.month < 12 else now.year + 1) start_time = now.replace(day=1, hour=0, minute=0, second=1, microsecond=0) + tzdelta end_time = start_time.replace(day=1, month=now.month+1 if now.month < 12 else 1, year=now.year if now.month < 12 else now.year + 1) + tzdelta events = db.GqlQuery('SELECT * FROM Event WHERE owner = :1 AND start_time >= :2 AND start_time < :3 ORDER BY start_time', account, start_time, end_time).fetch(1000) by_day = {} for event in events: start_time = event.start_time - tzdelta if start_time.day not in by_day: by_day[start_time.day] = [] by_day[start_time.day].append(event) for day, event_list in by_day.iteritems(): pattern = '>%d</div>'%(day,) text = text.replace(pattern, '>%d<br/>%s</div>'%(day, ', '.join([(event.start_time - tzdelta).strftime('%H:%M') for event in event_list]))) return render_to_response('experts/calendarframe.html', {'user': user, 'account': account, 'status': status, 'error_message': error_message, 'calendar': text, 'appointments': appointments, 'date': now.strftime('%A, %d %b, %I:%M %p'), 'availability': target.availability, 'is_my_calendar': bool(user.email() == account), 'prev': get_frame_url(account, tzoffset, prev), 'next': get_frame_url(account, tzoffset, next) })
def index(request, account): user = get_login_user(request) now = datetime.datetime.now() return render_to_response('experts/calendar.html', {'user': user, 'account': account, 'today': now.strftime('%Y-%m-%d')})