How to print current username in Crystal-lang? - crystal-lang

I want to print current username using Crystal-lang, does anyone know how to help me?

Yes this is still somewhat missing in the standard library. There's an ongoing discussion about it here: https://github.com/crystal-lang/crystal/issues/7829
Meanwhile we can quickly bind getuid(2) or geteuid(2) ourselves and combine it with System::User:
require "system/user"
lib LibC
fun getuid : UidT
end
user = System::User.find_by id: LibC.getuid.to_s
pp user.username
https://carc.in/#/r/b63s

Related

Question about Regex to edit claim and remove part of email / username

Completely new to regex only read a few guides my problem is as follows. A 3rd party solution is being connected to our Adfs 2016 enviroment. We have run into a problem as the solution cannot handle long usernames and the Upn and email of our users are in the format of users initials 3 or 4 letters.department#ourcompany.com, so Dave Dibley Jr would be ddj.department#ourcompany.com
what i would like to do is use Regex to Cut everything after the initals from the claim any suggestions how to do this ?
You can use RegEx for string manipulation in the Claims Rules Language. Fx:
c:[type == “http://contoso.com/role”]
=> issue (Type = “http://contoso.com/role”, Value = RegExReplace(c.Value, “(?i)director”, “Manager“);
Pass through any role claims. If any of the claims contain the word “Director”, RegExReplace() will change it to “Manager”. For example, “Director of Finance” would pass through as “Manager of Finance”.
See https://social.technet.microsoft.com/wiki/contents/articles/16161.ad-fs-2-0-using-regex-in-the-claims-rule-language.aspx for more information.

Pattern match a variable using Lua

Evening all,
I'm looking at passing text from a monitoring system as a variable and then using the regex/LUA to look for duplicate usernames
Example line of text is:
Unathorised Change Profile Entries: (Audit trail entry) USERNAME - USERNAME
Any advice would be greatly appreciated.
Big thank you for the help above.
Here's my final working code:
a = alarm.get ()
str = a
if (string.match(a.message, "(%a+) %- %1$")) then
print("Matching!")
else
print("No match")
end

How to create a model without controller considering rails4 strong parameters

Cannot find anywhere the accepted way to create a model without going through controller considering that attr_accissible is no longer supported.
Is the below approach correct?
in my old code:
ModelName.create(course_id:680, user_id:25)
(raises mass_assignment error now that I have removed attr_accessible)
new code:
model = ModelName.new.tap do |m|
m.course_id = 680
m.user_id = 25
end
model.save!
(works but looks hacky)
Apparently, the below will not work because without_protection option is removed in Rails4
ModelName.create({course_id: 680, user_id: User.first.id}, without_protection: true)
Thanks to this question I've read about strong parameters 'Use outside of Controllers' - link but even if I do the following from my console:
raw_params = {course_id: Course.last.id, user_id: User.first.id}
parameters = ActionController::Parameters.new(raw_params)
ModelName.create(parameters.permit(:course_id, :user_id))
I get error
WARN -- : WARNING: Can't mass-assign protected attributes for ModelName: course_id, user_id
I read this question more carefully and found my answer
I had to add
config.active_record.whitelist_attributes = false
to my environments (development/test/production.rb), maybe because I still have the protected_attributes gem installed.
so now I can happily use
ModelName.create(course_id:680, user_id:25)
afterall.
I realise this question/answer is somewhat of a repeat of the aforementioned question - but I did find that question a bit tricky to understand, so I won't take this question down unless asked.

Localization of Date formats in Django

I am working on an app that should support different languages and flavours such as American, British and German.
Now looking at the documentation, e.g. there seem to be no direct support for American date format. It seems this part needs to be done by hand.
So I kept digging into the documentation and found this:
{{ value|date:"D d M Y" }}
This doesn't seem to work well with forms though, when I tried this in my code:
{{form.birth_date|date:"D d M Y"}}
It seems the formatting needs to be set right at the form field itself.
More research showed a similar question was asked in Stackoverflow in 2009:
This solution could work, but it looks quite engaging for such a simple problem. Django has come a long way since 2009, so I wonder if there is a better way of doing it right into ModelForm?
Let say we have the following code:
Model:
class ConversationHistory(models.Model):
contact_date = models.DateField(_(u"Conversation Date"))
Form:
class ConversationForm(ModelForm):
class Meta:
model = ConversationHistory
Could I just add the following to ModelForm:
widgets = { 'contact_date': forms.DateInput(format='M d, Y') }
is this the right way of localization? What if I wanted to change the format to Brtish English or German? So I have the feeling I am missing here something...
Many Thanks,
UPDATE:
I have now tried this:
widgets = { 'contact_date': forms.DateInput(format='SHORT_DATE_FORMAT') }
But the date shows as SHORT_DATE_FORMAT.
What am I missing?
You're looking for the x format. Or worst case you just I18Nize the format and let the translator figure it out.
I have been working on a solution for this with dojo for 2YRS!!! And I finally manged to find the the following works if you are using the dojango intergration.
someDate = DateField(widget=DateInput(attrs={'constraints':'{datePattern:\'dd/MM/yyyy\', strict:true}'))
forms.py
I don't know if that helps

Getting users latest tweet with Django

I want to create a function which grabs every users latest tweet from a specific group. So, if a user is in the 'authors' group, I want to grab their latest tweet and then finally cache the result for the day so we only do the crazy leg work once.
def latest_tweets(self):
g = Group.objects.get(name='author')
users = []
for u in g.user_set.all():
acc = u.get_profile().twitter_account
users.append('http://twitter.com/statuses/user_timeline/'+acc+'.rss')
return users
Is where I am at so far, but I'm at a complete loose end as to how I parse the RSS to get there latest tweet. Can anyone help me out here? If there is a better way to do this, any suggestions are welcome! I'm sure someone will suggest using django-twitter or other such libraries, but I'd like to do this manually if possible.
Cheers
why redo the stone?, you can download/install/import python-twitter and do something like:
tweet = twitter.Api().GetUserTimeline( u.get_profile().twitter_account )[0]
http://code.google.com/p/python-twitter/
an example: http://www.omh.cc/blog/2008/aug/4/adding-your-twitter-status-django-site/
Rss can be parsed by any xml parser. I've used the built-in module htmllib before for a different task and found it easy to deal with. If all you're doing is rss parsing though, I'd recommend feedparser. I haven't used it before, but it seems pretty straight forward.
If you go with python-twitter it is pretty simple. This is from memory so forgive me if I make a mistake here.
from django.core.cache import cache
import twitter
TWITTER_USER = 'username'
TWITTER_TIMEOUT = 3600
def latest_tweet(request):
tweet = cache.get('tweet')
if tweet:
return {"tweet":tweet}
api = twitter.Api()
tweets = api.GetUserTimeline(TWITTER_USER)
tweet = tweets[0]
tweet.date = datetime.strptime(
tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y"
)
cache.set( 'tweet', tweet, TWITTER_TIMEOUT )
return {"tweet": tweet}