I would like to display user images in my web app from a users contacts. I have the following:
qry = gdata.contacts.client.ContactsQuery(max_results=10)
feed = gd_client.get_contacts(query=qry)
for cont in feed.entry:
self.response.out.write(cont.name)
photo = cont.GetPhotoLink().href
self.response.out.write("<img src='"+photo+"'>")
I have no idea why this isn't working as the links it produces seem right per this guide https://developers.google.com/google-apps/contacts/v3/#retrieving_a_contacts_photo
https://www.google.com/m8/feeds/photos/media/currentlyautheduser%40gmail.com/27e1e1d70bb51465
and the gd_client seems to be authed correctly (I can see the contacts other details...)
the code for my contacts client is as following
def AuthedContactsClient():
current_user = users.GetCurrentUser()
gd_client = gdata.contacts.client.ContactsClient(source=SETTINGS['APP_NAME'])
gd_client.ssl=True
access_token_key = 'access_token_%s' % current_user.user_id()
gd_client.auth_token = gdata.gauth.ae_load(access_token_key)
return gd_client
Any ideas? Thanks!
Related
I am developing a flask web application currently. However, I do not know how I can get the uploaded images from the user into the SQLite database and retrieve it for later use (such as display it on the homepage and other pages).
I am very new to web development so I am not familar with many programming languages yet. I have seen other pages talking about the use of php, may I know if that is really needed? Are there other ways of doing so in flask?
Would appreciate it if someone is able to guide me.
Thank you!
You can base64 encode an image and use a data: url
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
if you simple paste
data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
in your address bar, you will see a small red dot
In an app this looks like this:
# FLASK_APP=mini_app flask run
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
image = db.Column(db.String)
def create_app(config_filename=None, host="localhost"):
app = Flask("demo_app")
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
db.init_app(app)
with app.app_context():
db.create_all()
user = User(name="test", image="iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==")
db.session.add(user)
db.session.commit()
return app
host = "0.0.0.0"
app = create_app(host=host)
#app.route("/image/<string:user_name>")
def show_profile(user_name):
user = User.query.filter(User.name==user_name).one_or_none()
if user:
return f'User: {user.name}<br/>Pic:<img src="data:image/png;base64, {user.image}"/>'
return "Not Found", 404
if __name__ == "__main__":
app.run(host=host)
then go to
http://localhost:5000/image/test
I am making an app on Google App Engine and I have managed to store data on it's Datastore. However, I am not able to output any of my queries from the Datastore. My code is as follows.
class twitternames(db.Model):
retweetersids = db.IntegerProperty(required = True)
retweetersnames = db.StringProperty(required = True)
user_input = db.StringProperty(required= True)
datetimeadded = db.DateTimeProperty(auto_now_add = True)
q = twitternames.all()
q.filter ("retweetersnames=", "eriklikestorawr")
for p in q.run():
self.response.write(p.retweetersids)
I am trying to give retweetersnames and get retweetersids. The following is a snapshot of stored data on the datastore.
Your help much appreciated.
Thank you
Managed to solve it. I was using post request instead of get.
I have the following GQL database model:
class Post(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
And this is the POST request used to store content to the database
def post(self):
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
a = Post(subject = subject, content = content)
a.put()
self.redirect("/")
else:
error = "subject and content can neither be empty"
self.Render_NewPost(subject, content, error)
If I POST content to the database it works alright since i don't get the error. However I don't get the contents to show on the page it is suppose to show.
I'm interested in knowing the command line instruction I can use to check the database to be sure if the contents been posted are actually in the database or not so I can know where to figure out the problem of the content not showing on my Homepage as I hoped.
Thanks
Wait 30 seconds and refresh the page /. You may be running into the issue of "eventual consistency", where the data will "eventually" be put into the physical datastore you've queried, but may not be there yet.
I'm trying to create a user with the DNN 7 services framework. I've taken my working code from my custom registration module and modified to work within a DNN webapi function.
When I get to the UserController.CreateUser call in the code below I receive a
"\"There was an error generating the XML document.\""
exception. My user makes it into the aspnet_Users table and the DNN users table but does not make it into the DNN userportals table. Any ideas would be appreciated.
private void CreateUser()
{
//Update DisplayName to conform to Format
UpdateDisplayName();
User.Membership.Approved = PortalSettings.UserRegistration == (int)Globals.PortalRegistrationType.PublicRegistration;
var user = User;
CreateStatus = UserController.CreateUser(ref user);
I finally found the issue. I was not setting the portal ID for my new users and DNN was excepting out when it was adding them to a portal. All it took was User.PortalId = 0 before the CreateUser call.
I have found by trial and error that the minimum needed to create a viable DNN user is:
UserInfo uiNewUser = new UserInfo();
uiNewUser.Username = "<myUsername>";
uiNewUser.Displayname = "<myDisplayname>";
uiNewUser.Email = "<myUserEmail>";
UserMembership newMembership = new UserMembership(uiNewUser);
newMembership.Password = "<myUserPassword>";
uiNewUser.Membership = newMembership;
uiNewUser.PortalID = <myPortalID>;
DotNetNuke.Security.Membership.UserCreateStatus uStatus;
uStatus = DotNetNuke.Security.Membership.MembershipProvider.Instance().CreateUser(ref uiNewUser);
RoleInfo newRole = RoleController.Instance.GetRoleByName(uiNewUser.PortalID, "Registered Users");
RoleController.Instance.AddUserRole(uiNewUser.PortalID, uiNewUser.UserID, newRole.RoleID, (RoleStatus)1, false, DateTime.MinValue, DateTime.MaxValue);
If any of these are missed out, parts of the user are created in the database, but the user may not be visible in the Admin list of users, or an Exception may be generated. Other details can be added later.
I have a simple model:
class News(models.Model):
title = models.CharField(max_length=255, verbose_name=_("title"))
content = models.TextField(default='', verbose_name=_("content"))
panel = models.CharField(max_length=50, default='', verbose_name=_("panel"))
created = TzDateTimeProperty(auto_now_add=True, verbose_name=_("date created"))
lastmodified = TzDateTimeProperty(auto_now=True, verbose_name=_("date modified"))
I want to get the 5 recent news records, and I know that with Google App Engine DB queryset I can get 5 recent records in the following easy way:
results = News.all().filter(panel = panel).order('created').fetch(5)
With Django running in Google App Engine I would need to do:
results = News.objects.filter(panel = panel).order_by('created')[:5]
But it will throw exception if there are no news records. I could wrap it within catch exception, but what is the proper and optimized way to limit query results within Django?
You can do something like this
results = News.objects.filter(panel = panel).order_by('created')
if results is not None:
new_results = results[:5]